diff options
| author | Loic Bistuer <loic.bistuer@sixmedia.com> | 2013-11-30 02:38:13 +0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-12-16 16:33:28 -0500 |
| commit | 3ce9829b615336b0f3ac39b080c27fc8cf5af483 (patch) | |
| tree | def5d16f59f583bb20faf530b24e3f3d977fd458 /tests | |
| parent | e2f142030b81a37e1c3187f5d336dcb6014fd1c0 (diff) | |
Fixed #17413 -- Serialization of form errors along with all metadata.
Diffstat (limited to '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) |
