diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/constraints/tests.py | 19 | ||||
| -rw-r--r-- | tests/postgres_tests/test_constraints.py | 4 |
2 files changed, 19 insertions, 4 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index d4054dfd77..5a498f0d73 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -6,7 +6,7 @@ from django.db.models import F from django.db.models.constraints import BaseConstraint from django.db.models.functions import Lower from django.db.transaction import atomic -from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature +from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature from .models import ( ChildModel, @@ -234,6 +234,23 @@ class CheckConstraintTests(TestCase): constraint.validate(Product, Product(price=501, discounted_price=5)) constraint.validate(Product, Product(price=499, discounted_price=5)) + @skipUnlessDBFeature("supports_comparing_boolean_expr") + def test_validate_nullable_field_with_none(self): + # Nullable fields should be considered valid on None values. + constraint = models.CheckConstraint( + check=models.Q(price__gte=0), + name="positive_price", + ) + constraint.validate(Product, Product()) + + @skipIfDBFeature("supports_comparing_boolean_expr") + def test_validate_nullable_field_with_isnull(self): + constraint = models.CheckConstraint( + check=models.Q(price__gte=0) | models.Q(price__isnull=True), + name="positive_price", + ) + constraint.validate(Product, Product()) + class UniqueConstraintTests(TestCase): @classmethod diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py index 2b6df7d5f5..844c04cd6d 100644 --- a/tests/postgres_tests/test_constraints.py +++ b/tests/postgres_tests/test_constraints.py @@ -156,9 +156,7 @@ class SchemaTests(PostgreSQLTestCase): check=Q(ints__startswith__gte=0), name="ints_positive_range", ) - msg = f"Constraint “{constraint.name}” is violated." - with self.assertRaisesMessage(ValidationError, msg): - constraint.validate(RangesModel, RangesModel()) + constraint.validate(RangesModel, RangesModel()) def test_opclass(self): constraint = UniqueConstraint( |
