Refs #30462 -- Added test for conditional aggregation on annotated subquery.

This commit is contained in:
Mariusz Felisiak 2019-05-08 11:29:17 +02:00 committed by GitHub
parent f9c1d1884e
commit ef9f2eb69c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 1 deletions

View File

@ -1,7 +1,7 @@
import datetime import datetime
from decimal import Decimal 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 django.test import TestCase
from .models import Author, Book, Publisher from .models import Author, Book, Publisher
@ -95,3 +95,15 @@ class FilteredAggregateTests(TestCase):
cnt=Count('pk', filter=Q(double_age__gt=100)), cnt=Count('pk', filter=Q(double_age__gt=100)),
) )
self.assertEqual(aggs['cnt'], 2) 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)