summaryrefslogtreecommitdiff
path: root/tests/annotations/tests.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2020-05-13 23:38:29 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-05-14 08:40:40 +0200
commitafceb2241ba84dfafe59de326cdc9c01963ddefb (patch)
treed39cc3ece50f4a5592a81d703c1cb184e5e01632 /tests/annotations/tests.py
parent6e8a11e88cb11737a3230da45b9206bd3c7a2d98 (diff)
[3.0.x] Fixed #31566 -- Fixed aliases crash when chaining values()/values_list() after annotate() with aggregations and subqueries.
Subquery annotation references must be resolved if they are excluded from the GROUP BY clause by a following .values() call. Regression in fb3f034f1c63160c0ff13c609acd01c18be12f80. Thanks Makina Corpus for the report. Backport of 42c08ee46539ef44f8658ebb1cbefb408e0d03fe from master
Diffstat (limited to 'tests/annotations/tests.py')
-rw-r--r--tests/annotations/tests.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index c39e8d3fbe..0cdc9178b0 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -1,10 +1,13 @@
import datetime
from decimal import Decimal
+from unittest import skipIf
from django.core.exceptions import FieldDoesNotExist, FieldError
+from django.db import connection
from django.db.models import (
- BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, F, Func,
- IntegerField, NullBooleanField, OuterRef, Q, Subquery, Sum, Value,
+ BooleanField, CharField, Count, DateTimeField, Exists, ExpressionWrapper,
+ F, Func, IntegerField, Max, NullBooleanField, OuterRef, Q, Subquery, Sum,
+ Value,
)
from django.db.models.expressions import RawSQL
from django.db.models.functions import Length, Lower
@@ -619,3 +622,16 @@ class NonAggregateAnnotationTestCase(TestCase):
total_books=Subquery(long_books_qs, output_field=IntegerField()),
).values('name')
self.assertCountEqual(publisher_books_qs, [{'name': 'Sams'}, {'name': 'Morgan Kaufmann'}])
+
+ @skipIf(connection.vendor == 'oracle', 'See https://code.djangoproject.com/ticket/31584')
+ def test_annotation_exists_aggregate_values_chaining(self):
+ qs = Book.objects.values('publisher').annotate(
+ has_authors=Exists(Book.authors.through.objects.filter(book=OuterRef('pk'))),
+ max_pubdate=Max('pubdate'),
+ ).values_list('max_pubdate', flat=True).order_by('max_pubdate')
+ self.assertCountEqual(qs, [
+ datetime.date(1991, 10, 15),
+ datetime.date(2008, 3, 3),
+ datetime.date(2008, 6, 23),
+ datetime.date(2008, 11, 3),
+ ])