summaryrefslogtreecommitdiff
path: root/tests/annotations/tests.py
diff options
context:
space:
mode:
authorChristian Klus <christianklus@gmail.com>2020-10-27 13:13:10 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-29 11:33:52 +0100
commitb0a6798de59151163b94d1c0ccf0d010a3d46ac2 (patch)
tree22b065c839f286b63cdcbadd8d1c15c20b0788b7 /tests/annotations/tests.py
parent72a17c919bf9071ca34b00cd72b0084e5c6da8f6 (diff)
[3.0.x] Fixed #32152 -- Fixed grouping by subquery aliases.
Regression in 42c08ee46539ef44f8658ebb1cbefb408e0d03fe. Thanks Simon Charette for the review. Backport of 4ac2d4fa42e1659f328c35b6b8d4761b3419c11a from master
Diffstat (limited to 'tests/annotations/tests.py')
-rw-r--r--tests/annotations/tests.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index bbd35fbd4f..2ed3474f5d 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -10,7 +10,7 @@ from django.db.models import (
Q, Subquery, Sum, Value, When,
)
from django.db.models.expressions import RawSQL
-from django.db.models.functions import Length, Lower
+from django.db.models.functions import ExtractYear, Length, Lower
from django.test import TestCase, skipUnlessDBFeature
from .models import (
@@ -635,6 +635,25 @@ class NonAggregateAnnotationTestCase(TestCase):
datetime.date(2008, 11, 3),
])
+ @skipUnlessDBFeature('supports_subqueries_in_group_by')
+ def test_annotation_subquery_and_aggregate_values_chaining(self):
+ qs = Book.objects.annotate(
+ pub_year=ExtractYear('pubdate')
+ ).values('pub_year').annotate(
+ top_rating=Subquery(
+ Book.objects.filter(
+ pubdate__year=OuterRef('pub_year')
+ ).order_by('-rating').values('rating')[:1]
+ ),
+ total_pages=Sum('pages'),
+ ).values('pub_year', 'total_pages', 'top_rating')
+ self.assertCountEqual(qs, [
+ {'pub_year': 1991, 'top_rating': 5.0, 'total_pages': 946},
+ {'pub_year': 1995, 'top_rating': 4.0, 'total_pages': 1132},
+ {'pub_year': 2007, 'top_rating': 4.5, 'total_pages': 447},
+ {'pub_year': 2008, 'top_rating': 4.0, 'total_pages': 1178},
+ ])
+
@skipIf(
connection.vendor == 'mysql',
'GROUP BY optimization does not work properly when ONLY_FULL_GROUP_BY '