diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index c15204ce33..42b6b828f4 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -15,7 +15,9 @@ from django.db.models import ( Min, Model, OrderBy, OuterRef, Q, StdDev, Subquery, Sum, TimeField, UUIDField, Value, Variance, When, ) -from django.db.models.expressions import Col, Combinable, Random, RawSQL, Ref +from django.db.models.expressions import ( + Col, Combinable, CombinedExpression, Random, RawSQL, Ref, +) from django.db.models.functions import ( Coalesce, Concat, Left, Length, Lower, Substr, Upper, ) @@ -1888,6 +1890,26 @@ class CombinableTests(SimpleTestCase): object() | Combinable() +class CombinedExpressionTests(SimpleTestCase): + def test_resolve_output_field(self): + tests = [ + (IntegerField, DecimalField, DecimalField), + (DecimalField, IntegerField, DecimalField), + (IntegerField, FloatField, FloatField), + (FloatField, IntegerField, FloatField), + ] + connectors = [Combinable.ADD, Combinable.SUB, Combinable.MUL, Combinable.DIV] + for lhs, rhs, combined in tests: + for connector in connectors: + with self.subTest(lhs=lhs, connector=connector, rhs=rhs, combined=combined): + expr = CombinedExpression( + Expression(lhs()), + connector, + Expression(rhs()), + ) + self.assertIsInstance(expr.output_field, combined) + + class ExpressionWrapperTests(SimpleTestCase): def test_empty_group_by(self): expr = ExpressionWrapper(Value(3), output_field=IntegerField())