diff options
author | Simon Charette <charette.s@gmail.com> | 2022-11-22 21:49:12 -0500 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-11-23 17:46:07 +0100 |
commit | 1297c0d0d76a708017fe196b61a0ab324df76954 (patch) | |
tree | 95e6635d536eb24c7f7a1650628cad659041a948 /tests/aggregation | |
parent | d526d1569ca4a1e62bb6a1dd779d2068766d348c (diff) | |
download | django-1297c0d0d76a708017fe196b61a0ab324df76954.tar.gz |
Fixed #31679 -- Delayed annotating aggregations.
By avoiding to annotate aggregations meant to be possibly pushed to an
outer query until their references are resolved it is possible to
aggregate over a query with the same alias.
Even if #34176 is a convoluted case to support, this refactor seems
worth it given the reduction in complexity it brings with regards to
annotation removal when performing a subquery pushdown.
Diffstat (limited to 'tests/aggregation')
-rw-r--r-- | tests/aggregation/tests.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index c098716cca..4e860a7aa3 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -1258,11 +1258,11 @@ class AggregateTestCase(TestCase): self.assertEqual(author.sum_age, other_author.sum_age) def test_aggregate_over_aggregate(self): - msg = "Cannot compute Avg('age'): 'age' is an aggregate" + msg = "Cannot resolve keyword 'age_agg' into field." with self.assertRaisesMessage(FieldError, msg): - Author.objects.annotate(age_alias=F("age"),).aggregate( - age=Sum(F("age")), - avg_age=Avg(F("age")), + Author.objects.aggregate( + age_agg=Sum(F("age")), + avg_age=Avg(F("age_agg")), ) def test_annotated_aggregate_over_annotated_aggregate(self): @@ -2086,6 +2086,14 @@ class AggregateTestCase(TestCase): ) self.assertEqual(len(qs), 6) + def test_aggregation_over_annotation_shared_alias(self): + self.assertEqual( + Publisher.objects.annotate(agg=Count("book__authors"),).aggregate( + agg=Count("agg"), + ), + {"agg": 5}, + ) + class AggregateAnnotationPruningTests(TestCase): @classmethod |