summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2024-07-13 22:18:21 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-17 12:52:17 +0200
commitadc0b6aac3f8a5c96e1ca282bc9f46e28d20281c (patch)
treeb98649b463e59d05793d0b3536564e80c4e0f556 /tests
parent13922580cccfb9ab2922ff4943dd39da56dfbd8c (diff)
Fixed #35594 -- Added unique nulls distinct validation for expressions.
Thanks Mark Gensler for the report.
Diffstat (limited to 'tests')
-rw-r--r--tests/constraints/tests.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index 8b7599adc1..fd68c5156d 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -4,7 +4,7 @@ from django.core.exceptions import ValidationError
from django.db import IntegrityError, connection, models
from django.db.models import F
from django.db.models.constraints import BaseConstraint, UniqueConstraint
-from django.db.models.functions import Lower
+from django.db.models.functions import Abs, Lower
from django.db.transaction import atomic
from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test.utils import ignore_warnings
@@ -1070,6 +1070,19 @@ class UniqueConstraintTests(TestCase):
is_not_null_constraint.validate(Product, Product(price=4, discounted_price=3))
is_not_null_constraint.validate(Product, Product(price=2, discounted_price=1))
+ def test_validate_nulls_distinct_expressions(self):
+ Product.objects.create(price=42)
+ constraint = models.UniqueConstraint(
+ Abs("price"),
+ nulls_distinct=False,
+ name="uniq_prices_nulls_distinct",
+ )
+ constraint.validate(Product, Product(price=None))
+ Product.objects.create(price=None)
+ msg = f"Constraint “{constraint.name}” is violated."
+ with self.assertRaisesMessage(ValidationError, msg):
+ constraint.validate(Product, Product(price=None))
+
def test_name(self):
constraints = get_constraints(UniqueConstraintProduct._meta.db_table)
expected_name = "name_color_uniq"