summaryrefslogtreecommitdiff
path: root/tests/aggregation
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-01-01 15:27:52 -0500
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-01-14 16:47:07 +0100
commitd206d4c200d71c0847e7f6720d88c587e7b46843 (patch)
tree9ca44ecf0e7762653d1a3dbae334ee3d2615187c /tests/aggregation
parentf07360e8087d3b403d1d12ff696da3138116055a (diff)
Fixed #36051 -- Declared arity on aggregate functions.
Follow-up to 4a66a69239c493c05b322815b18c605cd4c96e7c.
Diffstat (limited to 'tests/aggregation')
-rw-r--r--tests/aggregation/tests.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index b6ba728e77..861b2c5dfc 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -1276,6 +1276,8 @@ class AggregateTestCase(TestCase):
Book.objects.annotate(Max("id")).annotate(Sum("id__max"))
class MyMax(Max):
+ arity = None
+
def as_sql(self, compiler, connection):
self.set_source_expressions(self.get_source_expressions()[0:1])
return super().as_sql(compiler, connection)
@@ -1288,6 +1290,7 @@ class AggregateTestCase(TestCase):
def test_multi_arg_aggregate(self):
class MyMax(Max):
output_field = DecimalField()
+ arity = None
def as_sql(self, compiler, connection):
copy = self.copy()
@@ -2178,6 +2181,27 @@ class AggregateTestCase(TestCase):
)
self.assertEqual(list(author_qs), [337])
+ def test_aggregate_arity(self):
+ funcs_with_inherited_constructors = [Avg, Max, Min, Sum]
+ msg = "takes exactly 1 argument (2 given)"
+ for function in funcs_with_inherited_constructors:
+ with (
+ self.subTest(function=function),
+ self.assertRaisesMessage(TypeError, msg),
+ ):
+ function(Value(1), Value(2))
+
+ funcs_with_custom_constructors = [Count, StdDev, Variance]
+ for function in funcs_with_custom_constructors:
+ with self.subTest(function=function):
+ # Extra arguments are rejected via the constructor.
+ with self.assertRaises(TypeError):
+ function(Value(1), True, Value(2))
+ # If the constructor is skipped, the arity check runs.
+ func_instance = function(Value(1), True)
+ with self.assertRaisesMessage(TypeError, msg):
+ super(function, func_instance).__init__(Value(1), Value(2))
+
class AggregateAnnotationPruningTests(TestCase):
@classmethod