From 04518e310d4552ff7595a34f5a7f93487d78a406 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 22 Sep 2022 23:57:06 -0400 Subject: Refs #33308 -- Enabled explicit GROUP BY and ORDER BY aliases. This ensures explicit grouping from using values() before annotating an aggregate function groups by selected aliases if supported. The GROUP BY feature is disabled on Oracle because it doesn't support it. --- tests/postgres_tests/test_array.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'tests/postgres_tests/test_array.py') diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 436838dd09..dff9a651c3 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -12,7 +12,12 @@ from django.core.management import call_command from django.db import IntegrityError, connection, models from django.db.models.expressions import Exists, OuterRef, RawSQL, Value from django.db.models.functions import Cast, JSONObject, Upper -from django.test import TransactionTestCase, modify_settings, override_settings +from django.test import ( + TransactionTestCase, + modify_settings, + override_settings, + skipUnlessDBFeature, +) from django.test.utils import isolate_apps from django.utils import timezone @@ -405,6 +410,27 @@ class TestQuerying(PostgreSQLTestCase): expected, ) + @skipUnlessDBFeature("allows_group_by_refs") + def test_group_by_order_by_aliases(self): + with self.assertNumQueries(1) as ctx: + self.assertSequenceEqual( + NullableIntegerArrayModel.objects.filter( + field__0__isnull=False, + ) + .values("field__0") + .annotate(arrayagg=ArrayAgg("id")) + .order_by("field__0"), + [ + {"field__0": 1, "arrayagg": [1]}, + {"field__0": 2, "arrayagg": [2, 3]}, + {"field__0": 20, "arrayagg": [4]}, + ], + ) + alias = connection.ops.quote_name("field__0") + sql = ctx[0]["sql"] + self.assertIn(f"GROUP BY {alias}", sql) + self.assertIn(f"ORDER BY {alias}", sql) + def test_index(self): self.assertSequenceEqual( NullableIntegerArrayModel.objects.filter(field__0=2), self.objs[1:3] -- cgit v1.3