summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-07-17 12:51:54 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-07-19 09:06:16 +0200
commit7a67b065d7e5653f3af1cbd28882d33d2a088b02 (patch)
tree757e701a3631e6d3c29c935c47e07a83bd9ba297 /tests
parentc646412a7594418985ccda02043f35c05da15b1c (diff)
[4.2.x] Fixed #34717 -- Fixed QuerySet.aggregate() crash when referencing window functions.
Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7. Refs #28477. Thanks younes-chaoui for the report. Backport of 68912e4f6f84f21322f92a2c7b6c77f68f91b9c9 from main
Diffstat (limited to 'tests')
-rw-r--r--tests/aggregation/tests.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 366b8434e5..db69246952 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -28,6 +28,7 @@ from django.db.models import (
Value,
Variance,
When,
+ Window,
)
from django.db.models.expressions import Func, RawSQL
from django.db.models.functions import (
@@ -2207,3 +2208,23 @@ class AggregateAnnotationPruningTests(TestCase):
sql = ctx.captured_queries[0]["sql"].lower()
self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
self.assertEqual(aggregate, {"sum_total_books": 3})
+
+ @skipUnlessDBFeature("supports_over_clause")
+ def test_referenced_window_requires_wrapping(self):
+ total_books_qs = Book.objects.annotate(
+ avg_publisher_pages=Coalesce(
+ Window(Avg("pages"), partition_by=F("publisher")),
+ 0.0,
+ )
+ )
+ with self.assertNumQueries(1) as ctx:
+ aggregate = total_books_qs.aggregate(
+ sum_avg_publisher_pages=Sum("avg_publisher_pages"),
+ books_count=Count("id"),
+ )
+ sql = ctx.captured_queries[0]["sql"].lower()
+ self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
+ self.assertEqual(
+ aggregate,
+ {"sum_avg_publisher_pages": 1100.0, "books_count": 2},
+ )