summaryrefslogtreecommitdiff
path: root/tests/forms_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/forms_tests')
-rw-r--r--tests/forms_tests/tests/test_forms.py47
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)