From ef9f2eb69c9396683cefa742bc7d0a0792090e8d Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 8 May 2019 11:29:17 +0200 Subject: [PATCH] Refs #30462 -- Added test for conditional aggregation on annotated subquery. --- tests/aggregation/test_filter_argument.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/aggregation/test_filter_argument.py b/tests/aggregation/test_filter_argument.py index 5848fc9a12..0339afcb51 100644 --- a/tests/aggregation/test_filter_argument.py +++ b/tests/aggregation/test_filter_argument.py @@ -1,7 +1,7 @@ import datetime from decimal import Decimal -from django.db.models import Case, Count, F, Q, Sum, When +from django.db.models import Case, Count, F, OuterRef, Q, Subquery, Sum, When from django.test import TestCase from .models import Author, Book, Publisher @@ -95,3 +95,15 @@ class FilteredAggregateTests(TestCase): cnt=Count('pk', filter=Q(double_age__gt=100)), ) self.assertEqual(aggs['cnt'], 2) + + def test_filtered_aggregate_ref_subquery_annotation(self): + aggs = Author.objects.annotate( + earliest_book_year=Subquery( + Book.objects.filter( + contact__pk=OuterRef('pk'), + ).order_by('pubdate').values('pubdate__year')[:1] + ), + ).aggregate( + cnt=Count('pk', filter=Q(earliest_book_year=2008)), + ) + self.assertEqual(aggs['cnt'], 2)