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
1 changed files with 17 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import json 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.expressions import F, OuterRef, Subquery, Value
from django.db.models.functions import Cast, Concat, Substr from django.db.models.functions import Cast, Concat, Substr
from django.test.utils import Approximate from django.test.utils import Approximate
@ -80,6 +80,12 @@ class TestGeneralAggregate(PostgreSQLTestCase):
) )
self.assertEqual(values, {'arrayagg': expected_output}) 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): def test_array_agg_empty_result(self):
AggregateTestModel.objects.all().delete() AggregateTestModel.objects.all().delete()
values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg('char_field')) values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg('char_field'))
@ -184,6 +190,16 @@ class TestGeneralAggregate(PostgreSQLTestCase):
) )
self.assertEqual(values, {'stringagg': expected_output}) 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): def test_string_agg_empty_result(self):
AggregateTestModel.objects.all().delete() AggregateTestModel.objects.all().delete()
values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter=';')) values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter=';'))