diff options
| author | Simon Charette <charette.s@gmail.com> | 2015-12-01 17:59:58 -0500 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2015-12-17 20:25:04 -0500 |
| commit | 3738e4ac46688a0f13139c0b9058fc81c1aac424 (patch) | |
| tree | 37d6d9f541cbc86fd55f9f665240cf17eccbec00 /tests/postgres_tests/test_array.py | |
| parent | 86eccdc8b67728d84440a46e5bf62c78f2eddf6d (diff) | |
Fixed #25841 -- Handled base array fields validation errors with params.
Thanks to Trac alias benzid-wael for the report.
Diffstat (limited to 'tests/postgres_tests/test_array.py')
| -rw-r--r-- | tests/postgres_tests/test_array.py | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 094a1e2235..e9cec6b5b5 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -507,16 +507,32 @@ class TestValidation(PostgreSQLTestCase): self.assertEqual(cm.exception.code, 'nested_array_mismatch') self.assertEqual(cm.exception.messages[0], 'Nested arrays must have the same length.') + def test_with_base_field_error_params(self): + field = ArrayField(models.CharField(max_length=2)) + with self.assertRaises(exceptions.ValidationError) as cm: + field.clean(['abc'], None) + self.assertEqual(len(cm.exception.error_list), 1) + exception = cm.exception.error_list[0] + self.assertEqual( + exception.message, + 'Item 0 in the array did not validate: Ensure this value has at most 2 characters (it has 3).' + ) + self.assertEqual(exception.code, 'item_invalid') + self.assertEqual(exception.params, {'nth': 0, 'value': 'abc', 'limit_value': 2, 'show_value': 3}) + def test_with_validators(self): field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)])) field.clean([1, 2], None) with self.assertRaises(exceptions.ValidationError) as cm: field.clean([0], None) - self.assertEqual(cm.exception.code, 'item_invalid') + self.assertEqual(len(cm.exception.error_list), 1) + exception = cm.exception.error_list[0] self.assertEqual( - cm.exception.messages[0], + exception.message, 'Item 0 in the array did not validate: Ensure this value is greater than or equal to 1.' ) + self.assertEqual(exception.code, 'item_invalid') + self.assertEqual(exception.params, {'nth': 0, 'value': 0, 'limit_value': 1, 'show_value': 0}) class TestSimpleFormField(PostgreSQLTestCase): @@ -538,6 +554,27 @@ class TestSimpleFormField(PostgreSQLTestCase): field.clean('a,b,') self.assertEqual(cm.exception.messages[0], 'Item 2 in the array did not validate: This field is required.') + def test_validate_fail_base_field_error_params(self): + field = SimpleArrayField(forms.CharField(max_length=2)) + with self.assertRaises(exceptions.ValidationError) as cm: + field.clean('abc,c,defg') + errors = cm.exception.error_list + self.assertEqual(len(errors), 2) + first_error = errors[0] + self.assertEqual( + first_error.message, + 'Item 0 in the array did not validate: Ensure this value has at most 2 characters (it has 3).' + ) + self.assertEqual(first_error.code, 'item_invalid') + self.assertEqual(first_error.params, {'nth': 0, 'value': 'abc', 'limit_value': 2, 'show_value': 3}) + second_error = errors[1] + self.assertEqual( + second_error.message, + 'Item 2 in the array did not validate: Ensure this value has at most 2 characters (it has 4).' + ) + self.assertEqual(second_error.code, 'item_invalid') + self.assertEqual(second_error.params, {'nth': 2, 'value': 'defg', 'limit_value': 2, 'show_value': 4}) + def test_validators_fail(self): field = SimpleArrayField(forms.RegexField('[a-e]{2}')) with self.assertRaises(exceptions.ValidationError) as cm: @@ -648,3 +685,12 @@ class TestSplitFormField(PostgreSQLTestCase): </td> </tr> ''') + + def test_invalid_char_length(self): + field = SplitArrayField(forms.CharField(max_length=2), size=3) + with self.assertRaises(exceptions.ValidationError) as cm: + field.clean(['abc', 'c', 'defg']) + self.assertEqual(cm.exception.messages, [ + 'Item 0 in the array did not validate: Ensure this value has at most 2 characters (it has 3).', + 'Item 2 in the array did not validate: Ensure this value has at most 2 characters (it has 4).', + ]) |
