diff options
author | Xavier Fernandez <xavier.fernandez@beta.gouv.fr> | 2023-02-14 21:06:45 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-02-23 10:58:20 +0100 |
commit | 5b3d3e400ab9334ba429ca360c9818c6dfc3a51b (patch) | |
tree | 03bd0bc014819b45f3941917a9faf9e2a8eb5bdc /tests/postgres_tests | |
parent | 51c9bb7cd16081133af4f0ab6d06572660309730 (diff) | |
download | django-5b3d3e400ab9334ba429ca360c9818c6dfc3a51b.tar.gz |
Fixed #34338 -- Allowed customizing code of ValidationError in BaseConstraint and subclasses.
Diffstat (limited to 'tests/postgres_tests')
-rw-r--r-- | tests/postgres_tests/test_constraints.py | 40 |
1 files changed, 39 insertions, 1 deletions
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), + "<ExclusionConstraint: index_type='GIST' expressions=[" + "(F(datespan), '-|-')] name='exclude_overlapping' " + "violation_error_code='overlapping_must_be_excluded'>", + ) 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)) |