summaryrefslogtreecommitdiff
path: root/tests/postgres_tests
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2021-02-21 01:38:55 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-07-19 13:04:27 +0200
commit501a8db46595b2d5b99c1d3b1146a832f43cdf1c (patch)
treec7f566647cc8d22f144b32d4b5f74ec73aacc523 /tests/postgres_tests
parent59942a66ceb79868cb91844df3a72a24c63e39fa (diff)
Fixed #10929 -- Added default argument to aggregates.
Thanks to Simon Charette and Adam Johnson for the reviews.
Diffstat (limited to 'tests/postgres_tests')
-rw-r--r--tests/postgres_tests/test_aggregates.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py
index d47c24203b..393e3d38e8 100644
--- a/tests/postgres_tests/test_aggregates.py
+++ b/tests/postgres_tests/test_aggregates.py
@@ -72,6 +72,34 @@ class TestGeneralAggregate(PostgreSQLTestCase):
)
self.assertEqual(values, {'aggregation': expected_result})
+ def test_default_argument(self):
+ AggregateTestModel.objects.all().delete()
+ tests = [
+ (ArrayAgg('char_field', default=['<empty>']), ['<empty>']),
+ (ArrayAgg('integer_field', default=[0]), [0]),
+ (ArrayAgg('boolean_field', default=[False]), [False]),
+ (BitAnd('integer_field', default=0), 0),
+ (BitOr('integer_field', default=0), 0),
+ (BoolAnd('boolean_field', default=False), False),
+ (BoolOr('boolean_field', default=False), False),
+ (JSONBAgg('integer_field', default=Value('["<empty>"]')), ['<empty>']),
+ (StringAgg('char_field', delimiter=';', default=Value('<empty>')), '<empty>'),
+ ]
+ for aggregation, expected_result in tests:
+ with self.subTest(aggregation=aggregation):
+ # Empty result with non-execution optimization.
+ with self.assertNumQueries(0):
+ values = AggregateTestModel.objects.none().aggregate(
+ aggregation=aggregation,
+ )
+ self.assertEqual(values, {'aggregation': expected_result})
+ # Empty result when query must be executed.
+ with self.assertNumQueries(1):
+ values = AggregateTestModel.objects.aggregate(
+ aggregation=aggregation,
+ )
+ self.assertEqual(values, {'aggregation': expected_result})
+
def test_array_agg_charfield(self):
values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg('char_field'))
self.assertEqual(values, {'arrayagg': ['Foo1', 'Foo2', 'Foo4', 'Foo3']})
@@ -515,6 +543,37 @@ class TestStatisticsAggregate(PostgreSQLTestCase):
)
self.assertEqual(values, {'aggregation': expected_result})
+ def test_default_argument(self):
+ StatTestModel.objects.all().delete()
+ tests = [
+ (Corr(y='int2', x='int1', default=0), 0),
+ (CovarPop(y='int2', x='int1', default=0), 0),
+ (CovarPop(y='int2', x='int1', sample=True, default=0), 0),
+ (RegrAvgX(y='int2', x='int1', default=0), 0),
+ (RegrAvgY(y='int2', x='int1', default=0), 0),
+ # RegrCount() doesn't support the default argument.
+ (RegrIntercept(y='int2', x='int1', default=0), 0),
+ (RegrR2(y='int2', x='int1', default=0), 0),
+ (RegrSlope(y='int2', x='int1', default=0), 0),
+ (RegrSXX(y='int2', x='int1', default=0), 0),
+ (RegrSXY(y='int2', x='int1', default=0), 0),
+ (RegrSYY(y='int2', x='int1', default=0), 0),
+ ]
+ for aggregation, expected_result in tests:
+ with self.subTest(aggregation=aggregation):
+ # Empty result with non-execution optimization.
+ with self.assertNumQueries(0):
+ values = StatTestModel.objects.none().aggregate(
+ aggregation=aggregation,
+ )
+ self.assertEqual(values, {'aggregation': expected_result})
+ # Empty result when query must be executed.
+ with self.assertNumQueries(1):
+ values = StatTestModel.objects.aggregate(
+ aggregation=aggregation,
+ )
+ self.assertEqual(values, {'aggregation': expected_result})
+
def test_corr_general(self):
values = StatTestModel.objects.aggregate(corr=Corr(y='int2', x='int1'))
self.assertEqual(values, {'corr': -1.0})
@@ -539,6 +598,11 @@ class TestStatisticsAggregate(PostgreSQLTestCase):
values = StatTestModel.objects.aggregate(regrcount=RegrCount(y='int2', x='int1'))
self.assertEqual(values, {'regrcount': 3})
+ def test_regr_count_default(self):
+ msg = 'RegrCount does not allow default.'
+ with self.assertRaisesMessage(TypeError, msg):
+ RegrCount(y='int2', x='int1', default=0)
+
def test_regr_intercept_general(self):
values = StatTestModel.objects.aggregate(regrintercept=RegrIntercept(y='int2', x='int1'))
self.assertEqual(values, {'regrintercept': 4})