summaryrefslogtreecommitdiff
path: root/tests/postgres_tests/test_aggregates.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-12 13:02:12 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-14 20:56:04 +0200
commit1f31027bb3ad460864fbcbbb89eeb328c0a2f184 (patch)
treec7a3c4f8e7ebc044a2c579c9dd79e8a4701ab9d9 /tests/postgres_tests/test_aggregates.py
parent1d650ad019c1ab8e73d1e5b2587bb232c8ab35b6 (diff)
Refs #32096 -- Fixed crash of ArrayAgg/StringAgg/JSONBAgg with ordering over JSONField key transforms.
Regression in 6789ded0a6ab797f0dcdfa6ad5d1cfa46e23abcd. Thanks Igor Jerosimić for the report.
Diffstat (limited to 'tests/postgres_tests/test_aggregates.py')
-rw-r--r--tests/postgres_tests/test_aggregates.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py
index b6cce4658b..06cca6313d 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)),
@@ -232,6 +242,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(
@@ -297,6 +318,16 @@ class TestGeneralAggregate(PostgreSQLTestCase):
)
self.assertEqual(values, {'jsonagg': expected_output})
+ def test_json_agg_jsonfield_ordering(self):
+ values = AggregateTestModel.objects.aggregate(
+ jsonagg=JSONBAgg(
+ KeyTransform('lang', 'json_field'),
+ filter=Q(json_field__lang__isnull=False),
+ ordering=KeyTransform('lang', 'json_field'),
+ ),
+ )
+ self.assertEqual(values, {'jsonagg': ['en', 'pl']})
+
def test_string_agg_array_agg_ordering_in_subquery(self):
stats = []
for i, agg in enumerate(AggregateTestModel.objects.order_by('char_field')):