diff options
author | Loic Bistuer <loic.bistuer@gmail.com> | 2014-07-01 20:48:00 +0700 |
---|---|---|
committer | Loic Bistuer <loic.bistuer@gmail.com> | 2014-07-04 17:05:31 +0700 |
commit | 1966054febbb96b713db27513617eabdbd70957b (patch) | |
tree | 77326d8d658b2f661bd12f5baba95130c8376378 /tests/forms_tests | |
parent | 1bb1d3168b813e050df757ff40cdaf2feab45fdb (diff) | |
download | django-1966054febbb96b713db27513617eabdbd70957b.tar.gz |
Fixed #22915 -- Document backward incompatible changes in the ValidationError constructor.
This patch also fixes update_error_dict to better handle the use case described
in this ticket, previously the type of the provided container could be lost in
some conditions.
Thanks Russell Keith-Magee for the report and Tim Graham for review.
Diffstat (limited to 'tests/forms_tests')
-rw-r--r-- | tests/forms_tests/tests/test_forms.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 985ff8e2e0..23de5024fb 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -740,6 +740,53 @@ class FormsTestCase(TestCase): with six.assertRaisesRegex(self, ValueError, "has no field named"): f.add_error('missing_field', 'Some error.') + def test_update_error_dict(self): + class CodeForm(Form): + code = CharField(max_length=10) + + def clean(self): + try: + raise ValidationError({'code': [ValidationError('Code error 1.')]}) + except ValidationError as e: + self._errors = e.update_error_dict(self._errors) + + try: + raise ValidationError({'code': [ValidationError('Code error 2.')]}) + except ValidationError as e: + self._errors = e.update_error_dict(self._errors) + + try: + raise ValidationError({'code': forms.ErrorList(['Code error 3.'])}) + except ValidationError as e: + self._errors = e.update_error_dict(self._errors) + + try: + raise ValidationError('Non-field error 1.') + except ValidationError as e: + self._errors = e.update_error_dict(self._errors) + + try: + raise ValidationError([ValidationError('Non-field error 2.')]) + except ValidationError as e: + self._errors = e.update_error_dict(self._errors) + + # Ensure that the newly added list of errors is an instance of ErrorList. + for field, error_list in self._errors.items(): + if not isinstance(error_list, self.error_class): + self._errors[field] = self.error_class(error_list) + + form = CodeForm({'code': 'hello'}) + # Trigger validation. + self.assertFalse(form.is_valid()) + + # Check that update_error_dict didn't lose track of the ErrorDict type. + self.assertTrue(isinstance(form._errors, forms.ErrorDict)) + + self.assertEqual(dict(form.errors), { + 'code': ['Code error 1.', 'Code error 2.', 'Code error 3.'], + NON_FIELD_ERRORS: ['Non-field error 1.', 'Non-field error 2.'], + }) + def test_has_error(self): class UserRegistration(Form): username = CharField(max_length=10) |