1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #31097 -- Added tests for filter in ArrayAgg and StringAgg.

This commit is contained in:
David Wobrock 2019-12-31 10:32:27 +01:00 committed by Mariusz Felisiak
parent 307c63f9a7
commit 7d44aeb388

View File

@ -1,6 +1,6 @@
import json
from django.db.models import CharField
from django.db.models import CharField, Q
from django.db.models.expressions import F, OuterRef, Subquery, Value
from django.db.models.functions import Cast, Concat, Substr
from django.test.utils import Approximate
@ -80,6 +80,12 @@ class TestGeneralAggregate(PostgreSQLTestCase):
)
self.assertEqual(values, {'arrayagg': expected_output})
def test_array_agg_filter(self):
values = AggregateTestModel.objects.aggregate(
arrayagg=ArrayAgg('integer_field', filter=Q(integer_field__gt=0)),
)
self.assertEqual(values, {'arrayagg': [1, 2]})
def test_array_agg_empty_result(self):
AggregateTestModel.objects.all().delete()
values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg('char_field'))
@ -184,6 +190,16 @@ class TestGeneralAggregate(PostgreSQLTestCase):
)
self.assertEqual(values, {'stringagg': expected_output})
def test_string_agg_filter(self):
values = AggregateTestModel.objects.aggregate(
stringagg=StringAgg(
'char_field',
delimiter=';',
filter=Q(char_field__endswith='3') | Q(char_field__endswith='1'),
)
)
self.assertEqual(values, {'stringagg': 'Foo1;Foo3'})
def test_string_agg_empty_result(self):
AggregateTestModel.objects.all().delete()
values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter=';'))