From d94e777b6658701b6d9beea2c7be864bee4f6d59 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 12 Oct 2020 13:02:12 +0200 Subject: [3.1.x] Refs #32096 -- Fixed crash of ArrayAgg/StringAgg/JSONBAgg with ordering over JSONField key transforms. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Regression in 6789ded0a6ab797f0dcdfa6ad5d1cfa46e23abcd. Thanks Igor Jerosimić for the report. Backport of 1f31027bb3ad460864fbcbbb89eeb328c0a2f184 from master --- tests/postgres_tests/test_aggregates.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'tests/postgres_tests') diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index 8963ddbcf1..12f2a38e71 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -1,7 +1,7 @@ import json from django.db.models import CharField, F, OuterRef, Q, Subquery, Value -from django.db.models.fields.json import KeyTransform +from django.db.models.fields.json import KeyTextTransform, KeyTransform from django.db.models.functions import Cast, Concat, Substr from django.test.utils import Approximate @@ -106,6 +106,16 @@ class TestGeneralAggregate(PostgreSQLTestCase): ) self.assertEqual(values, {'arrayagg': ['pl', 'en']}) + def test_array_agg_jsonfield_ordering(self): + values = AggregateTestModel.objects.aggregate( + arrayagg=ArrayAgg( + KeyTransform('lang', 'json_field'), + filter=Q(json_field__lang__isnull=False), + ordering=KeyTransform('lang', 'json_field'), + ), + ) + self.assertEqual(values, {'arrayagg': ['en', 'pl']}) + def test_array_agg_filter(self): values = AggregateTestModel.objects.aggregate( arrayagg=ArrayAgg('integer_field', filter=Q(integer_field__gt=0)), @@ -220,6 +230,17 @@ class TestGeneralAggregate(PostgreSQLTestCase): ) self.assertEqual(values, {'stringagg': expected_output}) + def test_string_agg_jsonfield_ordering(self): + values = AggregateTestModel.objects.aggregate( + stringagg=StringAgg( + KeyTextTransform('lang', 'json_field'), + delimiter=';', + ordering=KeyTextTransform('lang', 'json_field'), + output_field=CharField(), + ), + ) + self.assertEqual(values, {'stringagg': 'en;pl'}) + def test_string_agg_filter(self): values = AggregateTestModel.objects.aggregate( stringagg=StringAgg( -- cgit v1.3