summaryrefslogtreecommitdiff
path: root/tests/constraints
diff options
context:
space:
mode:
authorDavid Sanders <shang.xiao.sanders@gmail.com>2022-09-09 00:02:58 +1000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-09-13 12:48:31 +0200
commite14d08cd894e9d91cb5d9f44ba7532c1a223f458 (patch)
treea524445fadbda60124a63468c643f31639d794fc /tests/constraints
parentb731e8841558ee4caaba766c83f34ea9c7004f8b (diff)
downloaddjango-e14d08cd894e9d91cb5d9f44ba7532c1a223f458.tar.gz
Fixed #33996 -- Fixed CheckConstraint validation on NULL values.
Bug in 667105877e6723c6985399803a364848891513cc. Thanks James Beith for the report.
Diffstat (limited to 'tests/constraints')
-rw-r--r--tests/constraints/tests.py19
1 files changed, 18 insertions, 1 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