summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/aggregation/tests.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index dc745e4138..342507da01 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -1,6 +1,7 @@
import datetime
import re
from decimal import Decimal
+from unittest import skipIf
from django.core.exceptions import FieldError
from django.db import connection
@@ -1189,6 +1190,26 @@ class AggregateTestCase(TestCase):
},
])
+ @skipUnlessDBFeature('supports_subqueries_in_group_by')
+ @skipIf(
+ connection.vendor == 'mysql',
+ 'GROUP BY optimization does not work properly when ONLY_FULL_GROUP_BY '
+ 'mode is enabled on MySQL, see #31331.',
+ )
+ def test_aggregation_subquery_annotation_multivalued(self):
+ """
+ Subquery annotations must be included in the GROUP BY if they use
+ potentially multivalued relations (contain the LOOKUP_SEP).
+ """
+ subquery_qs = Author.objects.filter(
+ pk=OuterRef('pk'),
+ book__name=OuterRef('book__name'),
+ ).values('pk')
+ author_qs = Author.objects.annotate(
+ subquery_id=Subquery(subquery_qs),
+ ).annotate(count=Count('book'))
+ self.assertEqual(author_qs.count(), Author.objects.count())
+
def test_aggregation_order_by_not_selected_annotation_values(self):
result_asc = [
self.b4.pk,
@@ -1247,6 +1268,7 @@ class AggregateTestCase(TestCase):
).annotate(total=Count('*'))
self.assertEqual(dict(has_long_books_breakdown), {True: 2, False: 3})
+ @skipUnlessDBFeature('supports_subqueries_in_group_by')
def test_aggregation_subquery_annotation_related_field(self):
publisher = Publisher.objects.create(name=self.a9.name, num_awards=2)
book = Book.objects.create(
@@ -1266,3 +1288,8 @@ class AggregateTestCase(TestCase):
contact_publisher__isnull=False,
).annotate(count=Count('authors'))
self.assertSequenceEqual(books_qs, [book])
+ # FIXME: GROUP BY doesn't need to include a subquery with
+ # non-multivalued JOINs, see Col.possibly_multivalued (refs #31150):
+ # with self.assertNumQueries(1) as ctx:
+ # self.assertSequenceEqual(books_qs, [book])
+ # self.assertEqual(ctx[0]['sql'].count('SELECT'), 2)