diff options
author | Simon Charette <charette.s@gmail.com> | 2019-03-08 15:54:03 -0500 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2019-03-23 09:50:42 -0400 |
commit | d1e9c25162eecb24dee50fcfe834a2735e53fb0e (patch) | |
tree | b67634047cc88ee8fca3adb610254c8fe09f0aea /tests/expressions | |
parent | bdc07f176eb7c099ac3fdc42ebaadd48b7f67409 (diff) | |
download | django-d1e9c25162eecb24dee50fcfe834a2735e53fb0e.tar.gz |
Refs #30188 -- Prevented double annotation of subquery when aggregated over.
Thanks Can Sarıgöl for the suggested trimming approach.
Diffstat (limited to 'tests/expressions')
-rw-r--r-- | tests/expressions/tests.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index bb448745c8..a51080ef13 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -553,16 +553,20 @@ class BasicExpressionsTests(TestCase): @skipUnlessDBFeature('supports_subqueries_in_group_by') def test_aggregate_subquery_annotation(self): - aggregate = Company.objects.annotate( - ceo_salary=Subquery( - Employee.objects.filter( - id=OuterRef('ceo_id'), - ).values('salary') - ), - ).aggregate( - ceo_salary_gt_20=Count('pk', filter=Q(ceo_salary__gt=20)), - ) + with self.assertNumQueries(1) as ctx: + aggregate = Company.objects.annotate( + ceo_salary=Subquery( + Employee.objects.filter( + id=OuterRef('ceo_id'), + ).values('salary') + ), + ).aggregate( + ceo_salary_gt_20=Count('pk', filter=Q(ceo_salary__gt=20)), + ) self.assertEqual(aggregate, {'ceo_salary_gt_20': 1}) + # Aggregation over a subquery annotation doesn't annotate the subquery + # twice in the inner query. + self.assertLessEqual(ctx.captured_queries[0]['sql'].count('SELECT'), 4,) def test_explicit_output_field(self): class FuncA(Func): |