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

Code before:
        )

    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
        )
        self.assertQuerySetEqual(
            Number.objects.all(),
            [(-1, -1), (43, 42), (1338, 1337)],
            lambda n: (n.integer, round(n.float)),
            ordered=False,
        )

    def test_filter_not_equals_other_field(self):
        """
        We can filter for objects, where a value is not equals the value
        of an other field.

Review comment (suggestion):
We could use any object with a `__bool__() -> False` but I figured it was simpler to use `Q` directly since it's the case that triggered the reported problem.

Code after:
        )

    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"))

    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,
        )