summaryrefslogtreecommitdiff
path: root/tests/constraints
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2025-02-18 12:43:38 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2025-03-01 19:57:53 +0100
commitfc303551077c3e023fe4f9d01fc1b3026c816fa4 (patch)
treeb0df1aee2d06932305f46e2aea596986b4f20460 /tests/constraints
parentff3aaf036f0cb66cd8f404cd51c603e68aaa7676 (diff)
Fixed #36198 -- Implemented unresolved transform expression replacement.
This allows the proper resolving of F("field__transform") when performing constraint validation. Thanks Tom Hall for the report and Sarah for the test.
Diffstat (limited to 'tests/constraints')
-rw-r--r--tests/constraints/models.py1
-rw-r--r--tests/constraints/tests.py18
2 files changed, 19 insertions, 0 deletions
diff --git a/tests/constraints/models.py b/tests/constraints/models.py
index 95a29ffa4d..41b827640e 100644
--- a/tests/constraints/models.py
+++ b/tests/constraints/models.py
@@ -73,6 +73,7 @@ class UniqueConstraintProduct(models.Model):
name = models.CharField(max_length=255)
color = models.CharField(max_length=32, null=True)
age = models.IntegerField(null=True)
+ updated = models.DateTimeField(null=True)
class Meta:
constraints = [
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index 51f09f2937..20a5357cc5 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -1,3 +1,4 @@
+from datetime import datetime, timedelta
from unittest import mock
from django.core.exceptions import ValidationError
@@ -1030,6 +1031,23 @@ class UniqueConstraintTests(TestCase):
exclude={"name"},
)
+ def test_validate_field_transform(self):
+ updated_date = datetime(2005, 7, 26)
+ UniqueConstraintProduct.objects.create(name="p1", updated=updated_date)
+ constraint = models.UniqueConstraint(
+ models.F("updated__date"), name="date_created_unique"
+ )
+ msg = "Constraint “date_created_unique” is violated."
+ with self.assertRaisesMessage(ValidationError, msg):
+ constraint.validate(
+ UniqueConstraintProduct,
+ UniqueConstraintProduct(updated=updated_date),
+ )
+ constraint.validate(
+ UniqueConstraintProduct,
+ UniqueConstraintProduct(updated=updated_date + timedelta(days=1)),
+ )
+
def test_validate_ordered_expression(self):
constraint = models.UniqueConstraint(
Lower("name").desc(), name="name_lower_uniq_desc"