Repository: django/django
File: tests/expressions/tests.py (Python)

Code before:
        self.assertNotEqual(
            hash(Expression(IntegerField())),
            hash(Expression(CharField())),
        )

        class TestModel(Model):
            field = IntegerField()
            other_field = IntegerField()

        self.assertNotEqual(
            hash(Expression(TestModel._meta.get_field("field"))),
            hash(Expression(TestModel._meta.get_field("other_field"))),
        )

    def test_get_expression_for_validation_only_one_source_expression(self):
        expression = Expression()
        expression.constraint_validation_compatible = False
        msg = (
            "Expressions with constraint_validation_compatible set to False must have "
            "only one source expression."
        )
        with self.assertRaisesMessage(ValueError, msg):
            expression.get_expression_for_validation()


class ExpressionsNumericTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        Number(integer=-1).save()
        Number(integer=42).save()
        Number(integer=1337).save()
        Number.objects.update(float=F("integer"))

    def test_fill_with_value_from_same_object(self):
        """
        We can fill a value in all objects with an other value of the
        same object.
        """
        self.assertQuerySetEqual(
            Number.objects.all(),
            [(-1, -1), (42, 42), (1337, 1337)],
            lambda n: (n.integer, round(n.float)),
            ordered=False,
        )

    def test_increment_value(self):
        """
        We can increment a value of all objects in a query set.
        """
        self.assertEqual(
            Number.objects.filter(integer__gt=0).update(integer=F("integer") + 1), 2

Review comment (suggestion):
I wanted to use `Expression` directly but it doesn't allow assignment of `source_expressions`

https://github.com/django/django/blob/ec60df6d1ea8939a316d9b180faa0b4ef2e83606/django/db/models/expressions.py#L212-L213

Code after:
        self.assertNotEqual(
            hash(Expression(IntegerField())),
            hash(Expression(CharField())),
        )

        class TestModel(Model):
            field = IntegerField()
            other_field = IntegerField()

        self.assertNotEqual(
            hash(Expression(TestModel._meta.get_field("field"))),
            hash(Expression(TestModel._meta.get_field("other_field"))),
        )

    def test_get_expression_for_validation_only_one_source_expression(self):
        expression = Expression()
        expression.constraint_validation_compatible = False
        msg = (
            "Expressions with constraint_validation_compatible set to False must have "
            "only one source expression."
        )
        with self.assertRaisesMessage(ValueError, msg):
            expression.get_expression_for_validation()

    def test_replace_expressions_falsey(self):
        class AssignableExpression(Expression):
            def __init__(self, *source_expressions):
                super().__init__()
                self.set_source_expressions(list(source_expressions))

            def get_source_expressions(self):
                return self.source_expressions

            def set_source_expressions(self, exprs):
                self.source_expressions = exprs

        expression = AssignableExpression()
        falsey = Q()
        expression.set_source_expressions([falsey])
        replaced = expression.replace_expressions({"replacement": Expression()})
        self.assertEqual(replaced.get_source_expressions(), [falsey])


class ExpressionsNumericTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        Number(integer=-1).save()
        Number(integer=42).save()
        Number(integer=1337).save()
        Number.objects.update(float=F("integer"))