diff options
| author | Jacob Rief <jacob.rief@gmail.com> | 2024-10-30 17:04:46 +0100 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-11-08 13:16:44 +0100 |
| commit | 40bfd7b09aa0907b143e96f0b055538f476e544e (patch) | |
| tree | 5462ede0e1641f9590777bd73d287975637c5076 | |
| parent | 042b381e2e37c0c37b8a8f6cc9947f1a2ebfa0dd (diff) | |
Fixed #35011, Refs #28900 -- Added tests for QuerySet.union() with multiple models and DateTimeField annotations.
Ticket was resolved by 65ad4ade74dc9208b9d686a451cd6045df0c9c3a as part of #28900.
| -rw-r--r-- | tests/queries/test_qs_combinators.py | 38 |
1 files changed, 37 insertions, 1 deletions
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( [ |
