summaryrefslogtreecommitdiff
path: root/tests/constraints/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/constraints/tests.py')
-rw-r--r--tests/constraints/tests.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index e1c431956f..9047710098 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -953,6 +953,41 @@ class UniqueConstraintTests(TestCase):
ChildUniqueConstraintProduct(name=self.p1.name, color=self.p1.color),
)
+ def test_validate_unique_custom_code_and_message(self):
+ product = UniqueConstraintProduct.objects.create(
+ name="test", color="red", age=42
+ )
+ code = "custom_code"
+ message = "Custom message"
+ multiple_fields_constraint = models.UniqueConstraint(
+ fields=["color", "age"],
+ name="color_age_uniq",
+ violation_error_code=code,
+ violation_error_message=message,
+ )
+ single_field_constraint = models.UniqueConstraint(
+ fields=["color"],
+ name="color_uniq",
+ violation_error_code=code,
+ violation_error_message=message,
+ )
+
+ with self.assertRaisesMessage(ValidationError, message) as cm:
+ multiple_fields_constraint.validate(
+ UniqueConstraintProduct,
+ UniqueConstraintProduct(
+ name="new-test", color=product.color, age=product.age
+ ),
+ )
+ self.assertEqual(cm.exception.code, code)
+
+ with self.assertRaisesMessage(ValidationError, message) as cm:
+ single_field_constraint.validate(
+ UniqueConstraintProduct,
+ UniqueConstraintProduct(name="new-test", color=product.color),
+ )
+ self.assertEqual(cm.exception.code, code)
+
@skipUnlessDBFeature("supports_table_check_constraints")
def test_validate_fields_unattached(self):
Product.objects.create(price=42)