mirror of
https://github.com/django/django.git
synced 2025-07-24 13:44:32 +00:00
Fixed #26327 -- Added JsonAgg to contrib.postgres.
Thanks Tim Graham for review.
This commit is contained in:
parent
52188a5ca6
commit
0a26f3c338
4 changed files with 46 additions and 6 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue