diff options
Diffstat (limited to 'tests/forms_tests')
| -rw-r--r-- | tests/forms_tests/tests/test_forms.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 9fec9ddcaa..675565b026 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import copy import datetime +import json import warnings from django.core.files.uploadedfile import SimpleUploadedFile @@ -2031,3 +2032,40 @@ class FormsTestCase(TestCase): form = SomeForm() self.assertHTMLEqual(form.as_p(), '<p id="p_some_field"></p>') + + def test_error_dict(self): + class MyForm(Form): + foo = CharField() + bar = CharField() + + def clean(self): + raise ValidationError('Non-field error.', code='secret', params={'a': 1, 'b': 2}) + + form = MyForm({}) + self.assertEqual(form.is_valid(), False) + + errors = form.errors.as_text() + control = [ + '* foo\n * This field is required.', + '* bar\n * This field is required.', + '* __all__\n * Non-field error.', + ] + for error in control: + self.assertIn(error, errors) + + errors = form.errors.as_ul() + control = [ + '<li>foo<ul class="errorlist"><li>This field is required.</li></ul></li>', + '<li>bar<ul class="errorlist"><li>This field is required.</li></ul></li>', + '<li>__all__<ul class="errorlist"><li>Non-field error.</li></ul></li>', + ] + for error in control: + self.assertInHTML(error, errors) + + errors = json.loads(form.errors.as_json()) + control = { + 'foo': [{'code': 'required', 'message': 'This field is required.'}], + 'bar': [{'code': 'required', 'message': 'This field is required.'}], + '__all__': [{'code': 'secret', 'message': 'Non-field error.'}] + } + self.assertEqual(errors, control) |
