From 5b3d3e400ab9334ba429ca360c9818c6dfc3a51b Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Tue, 14 Feb 2023 21:06:45 +0100 Subject: Fixed #34338 -- Allowed customizing code of ValidationError in BaseConstraint and subclasses. --- tests/postgres_tests/test_constraints.py | 40 +++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'tests/postgres_tests') diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py index a5248e1491..bf47833743 100644 --- a/tests/postgres_tests/test_constraints.py +++ b/tests/postgres_tests/test_constraints.py @@ -397,6 +397,17 @@ class ExclusionConstraintTests(PostgreSQLTestCase): "(F(datespan), '-|-')] name='exclude_overlapping' " "violation_error_message='Overlapping must be excluded'>", ) + constraint = ExclusionConstraint( + name="exclude_overlapping", + expressions=[(F("datespan"), RangeOperators.ADJACENT_TO)], + violation_error_code="overlapping_must_be_excluded", + ) + self.assertEqual( + repr(constraint), + "", + ) def test_eq(self): constraint_1 = ExclusionConstraint( @@ -470,6 +481,16 @@ class ExclusionConstraintTests(PostgreSQLTestCase): condition=Q(cancelled=False), violation_error_message="other custom error", ) + constraint_12 = ExclusionConstraint( + name="exclude_overlapping", + expressions=[ + (F("datespan"), RangeOperators.OVERLAPS), + (F("room"), RangeOperators.EQUAL), + ], + condition=Q(cancelled=False), + violation_error_code="custom_code", + violation_error_message="other custom error", + ) self.assertEqual(constraint_1, constraint_1) self.assertEqual(constraint_1, mock.ANY) self.assertNotEqual(constraint_1, constraint_2) @@ -483,7 +504,9 @@ class ExclusionConstraintTests(PostgreSQLTestCase): self.assertNotEqual(constraint_5, constraint_6) self.assertNotEqual(constraint_1, object()) self.assertNotEqual(constraint_10, constraint_11) + self.assertNotEqual(constraint_11, constraint_12) self.assertEqual(constraint_10, constraint_10) + self.assertEqual(constraint_12, constraint_12) def test_deconstruct(self): constraint = ExclusionConstraint( @@ -760,17 +783,32 @@ class ExclusionConstraintTests(PostgreSQLTestCase): constraint = ExclusionConstraint( name="ints_adjacent", expressions=[("ints", RangeOperators.ADJACENT_TO)], + violation_error_code="custom_code", violation_error_message="Custom error message.", ) range_obj = RangesModel.objects.create(ints=(20, 50)) constraint.validate(RangesModel, range_obj) msg = "Custom error message." - with self.assertRaisesMessage(ValidationError, msg): + with self.assertRaisesMessage(ValidationError, msg) as cm: constraint.validate(RangesModel, RangesModel(ints=(10, 20))) + self.assertEqual(cm.exception.code, "custom_code") constraint.validate(RangesModel, RangesModel(ints=(10, 19))) constraint.validate(RangesModel, RangesModel(ints=(51, 60))) constraint.validate(RangesModel, RangesModel(ints=(10, 20)), exclude={"ints"}) + def test_validate_with_custom_code_and_condition(self): + constraint = ExclusionConstraint( + name="ints_adjacent", + expressions=[("ints", RangeOperators.ADJACENT_TO)], + violation_error_code="custom_code", + condition=Q(ints__lt=(100, 200)), + ) + range_obj = RangesModel.objects.create(ints=(20, 50)) + constraint.validate(RangesModel, range_obj) + with self.assertRaises(ValidationError) as cm: + constraint.validate(RangesModel, RangesModel(ints=(10, 20))) + self.assertEqual(cm.exception.code, "custom_code") + def test_expressions_with_params(self): constraint_name = "scene_left_equal" self.assertNotIn(constraint_name, self.get_constraints(Scene._meta.db_table)) -- cgit v1.2.1