diff options
| author | Mads Jensen <mje@inducks.org> | 2016-09-26 13:16:03 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-10-07 07:52:03 -0400 |
| commit | 0a26f3c3388137c336fb1417ba870fde12c0fbcc (patch) | |
| tree | 6267b1295ee7b8d5a4f9e24f93516e81b393216d /tests/postgres_tests/test_aggregates.py | |
| parent | 52188a5ca6bafea0a66f17baacb315d61c7b99cd (diff) | |
Fixed #26327 -- Added JsonAgg to contrib.postgres.
Thanks Tim Graham for review.
Diffstat (limited to 'tests/postgres_tests/test_aggregates.py')
| -rw-r--r-- | tests/postgres_tests/test_aggregates.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index f6f48fdd61..aaab3b1bc6 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -1,14 +1,21 @@ -from django.contrib.postgres.aggregates import ( - ArrayAgg, BitAnd, BitOr, BoolAnd, BoolOr, Corr, CovarPop, RegrAvgX, - RegrAvgY, RegrCount, RegrIntercept, RegrR2, RegrSlope, RegrSXX, RegrSXY, - RegrSYY, StatAggregate, StringAgg, -) +import json + from django.db.models.expressions import F, Value +from django.test.testcases import skipUnlessDBFeature from django.test.utils import Approximate from . import PostgreSQLTestCase from .models import AggregateTestModel, StatTestModel +try: + from django.contrib.postgres.aggregates import ( + ArrayAgg, BitAnd, BitOr, BoolAnd, BoolOr, Corr, CovarPop, JsonAgg, + RegrAvgX, RegrAvgY, RegrCount, RegrIntercept, RegrR2, RegrSlope, + RegrSXX, RegrSXY, RegrSYY, StatAggregate, StringAgg, + ) +except ImportError: + pass # psycopg2 is not installed + class TestGeneralAggregate(PostgreSQLTestCase): @classmethod @@ -110,6 +117,16 @@ class TestGeneralAggregate(PostgreSQLTestCase): values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter=';')) self.assertEqual(values, {'stringagg': ''}) + @skipUnlessDBFeature('has_jsonb_datatype') + def test_json_agg(self): + values = AggregateTestModel.objects.aggregate(jsonagg=JsonAgg('char_field')) + self.assertEqual(values, {'jsonagg': ['Foo1', 'Foo2', 'Foo3', 'Foo4']}) + + @skipUnlessDBFeature('has_jsonb_datatype') + def test_json_agg_empty(self): + values = AggregateTestModel.objects.none().aggregate(jsonagg=JsonAgg('integer_field')) + self.assertEqual(values, json.loads('{"jsonagg": []}')) + class TestStringAggregateDistinct(PostgreSQLTestCase): @classmethod |
