From fee87345967b3d917b618533585076cbfa43451b Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Wed, 30 Jun 2021 00:08:27 +0100 Subject: Refs #10929 -- Deprecated forced empty result value for PostgreSQL aggregates. This deprecates forcing a return value for ArrayAgg, JSONBAgg, and StringAgg when there are no rows in the query. Now that we have a ``default`` argument for aggregates, we want to revert to returning the default of ``None`` which most aggregate functions return and leave it up to the user to decide what they want to be returned by default. --- tests/postgres_tests/test_aggregates.py | 47 ++++++++++++++++++++++++++++++++- 1 file changed, 46 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 393e3d38e8..7ae7b16c9f 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -3,7 +3,8 @@ from django.db.models import ( ) from django.db.models.fields.json import KeyTextTransform, KeyTransform from django.db.models.functions import Cast, Concat, Substr -from django.test.utils import Approximate +from django.test.utils import Approximate, ignore_warnings +from django.utils.deprecation import RemovedInDjango50Warning from . import PostgreSQLTestCase from .models import AggregateTestModel, StatTestModel @@ -44,6 +45,7 @@ class TestGeneralAggregate(PostgreSQLTestCase): ), ]) + @ignore_warnings(category=RemovedInDjango50Warning) def test_empty_result_set(self): AggregateTestModel.objects.all().delete() tests = [ @@ -100,6 +102,49 @@ class TestGeneralAggregate(PostgreSQLTestCase): ) self.assertEqual(values, {'aggregation': expected_result}) + def test_convert_value_deprecation(self): + AggregateTestModel.objects.all().delete() + queryset = AggregateTestModel.objects.all() + + with self.assertWarnsMessage(RemovedInDjango50Warning, ArrayAgg.deprecation_msg): + queryset.aggregate(aggregation=ArrayAgg('boolean_field')) + + with self.assertWarnsMessage(RemovedInDjango50Warning, JSONBAgg.deprecation_msg): + queryset.aggregate(aggregation=JSONBAgg('integer_field')) + + with self.assertWarnsMessage(RemovedInDjango50Warning, StringAgg.deprecation_msg): + queryset.aggregate(aggregation=StringAgg('char_field', delimiter=';')) + + # No warnings raised if default argument provided. + self.assertEqual( + queryset.aggregate(aggregation=ArrayAgg('boolean_field', default=None)), + {'aggregation': None}, + ) + self.assertEqual( + queryset.aggregate(aggregation=JSONBAgg('integer_field', default=None)), + {'aggregation': None}, + ) + self.assertEqual( + queryset.aggregate( + aggregation=StringAgg('char_field', delimiter=';', default=None), + ), + {'aggregation': None}, + ) + self.assertEqual( + queryset.aggregate(aggregation=ArrayAgg('boolean_field', default=Value([]))), + {'aggregation': []}, + ) + self.assertEqual( + queryset.aggregate(aggregation=JSONBAgg('integer_field', default=Value('[]'))), + {'aggregation': []}, + ) + self.assertEqual( + queryset.aggregate( + aggregation=StringAgg('char_field', delimiter=';', default=Value('')), + ), + {'aggregation': ''}, + ) + def test_array_agg_charfield(self): values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg('char_field')) self.assertEqual(values, {'arrayagg': ['Foo1', 'Foo2', 'Foo4', 'Foo3']}) -- cgit v1.3