From 40bfd7b09aa0907b143e96f0b055538f476e544e Mon Sep 17 00:00:00 2001 From: Jacob Rief Date: Wed, 30 Oct 2024 17:04:46 +0100 Subject: [PATCH] Fixed #35011, Refs #28900 -- Added tests for QuerySet.union() with multiple models and DateTimeField annotations. Ticket was resolved by 65ad4ade74dc9208b9d686a451cd6045df0c9c3a as part of #28900. --- tests/queries/test_qs_combinators.py | 38 +++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py index 2f6e93cde8..ba44b5ed87 100644 --- a/tests/queries/test_qs_combinators.py +++ b/tests/queries/test_qs_combinators.py @@ -1,7 +1,9 @@ import operator +from datetime import datetime from django.db import DatabaseError, NotSupportedError, connection from django.db.models import ( + DateTimeField, Exists, F, IntegerField, @@ -10,12 +12,13 @@ from django.db.models import ( Transform, Value, ) -from django.db.models.functions import Mod +from django.db.models.functions import Cast, Mod from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature from django.test.utils import CaptureQueriesContext from .models import ( Annotation, + Article, Author, Celebrity, ExtraInfo, @@ -440,6 +443,39 @@ class QuerySetSetOperationTests(TestCase): [("c1", -10, "cb"), ("rn1", 10, "rn")], ) + def test_union_multiple_models_with_values_list_and_datetime_annotations(self): + gen_x = datetime(1966, 6, 6) + Article.objects.create(name="Bellatrix", created=gen_x) + column_names = ["name", "created", "order"] + qs1 = Article.objects.annotate(order=Value(1)).values_list(*column_names) + + gen_y = datetime(1991, 10, 10) + ReservedName.objects.create(name="Rigel", order=2) + qs2 = ReservedName.objects.annotate( + created=Cast(Value(gen_y), DateTimeField()) + ).values_list(*column_names) + + expected_result = [("Bellatrix", gen_x, 1), ("Rigel", gen_y, 2)] + self.assertEqual(list(qs1.union(qs2).order_by("order")), expected_result) + + def test_union_multiple_models_with_values_and_datetime_annotations(self): + gen_x = datetime(1966, 6, 6) + Article.objects.create(name="Bellatrix", created=gen_x) + column_names = ["name", "created", "order"] + qs1 = Article.objects.values(*column_names, order=Value(1)) + + gen_y = datetime(1991, 10, 10) + ReservedName.objects.create(name="Rigel", order=2) + qs2 = ReservedName.objects.values( + *column_names, created=Cast(Value(gen_y), DateTimeField()) + ) + + expected_result = [ + {"name": "Bellatrix", "created": gen_x, "order": 1}, + {"name": "Rigel", "created": gen_y, "order": 2}, + ] + self.assertEqual(list(qs1.union(qs2).order_by("order")), expected_result) + def test_union_in_subquery(self): ReservedName.objects.bulk_create( [