MCPcopy
hub / github.com/django/django / test_error_dict

Method test_error_dict

tests/forms_tests/tests/test_forms.py:4404–4445  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4402 )
4403
4404 def test_error_dict(self):
4405 class MyForm(Form):
4406 foo = CharField()
4407 bar = CharField()
4408
4409 def clean(self):
4410 raise ValidationError(
4411 "Non-field error.", code="secret", params={"a": 1, "b": 2}
4412 )
4413
4414 form = MyForm({})
4415 self.assertIs(form.is_valid(), False)
4416
4417 errors = form.errors.as_text()
4418 control = [
4419 "* foo\n * This field is required.",
4420 "* bar\n * This field is required.",
4421 "* __all__\n * Non-field error.",
4422 ]
4423 for error in control:
4424 self.assertIn(error, errors)
4425
4426 errors = form.errors.as_ul()
4427 control = [
4428 '<li>foo<ul class="errorlist" id="id_foo_error"><li>This field is required.'
4429 "</li></ul></li>",
4430 '<li>bar<ul class="errorlist" id="id_bar_error"><li>This field is required.'
4431 "</li></ul></li>",
4432 '<li>__all__<ul class="errorlist nonfield"><li>Non-field error.</li></ul>'
4433 "</li>",
4434 ]
4435 for error in control:
4436 self.assertInHTML(error, errors)
4437
4438 errors = form.errors.get_json_data()
4439 control = {
4440 "foo": [{"code": "required", "message": "This field is required."}],
4441 "bar": [{"code": "required", "message": "This field is required."}],
4442 "__all__": [{"code": "secret", "message": "Non-field error."}],
4443 }
4444 self.assertEqual(errors, control)
4445 self.assertEqual(json.dumps(errors), form.errors.as_json())
4446
4447 def test_error_dict_as_json_escape_html(self):
4448 """#21962 - adding html escape flag to ErrorDict"""

Callers

nothing calls this directly

Calls 8

assertInHTMLMethod · 0.80
as_jsonMethod · 0.80
MyFormClass · 0.70
is_validMethod · 0.45
as_textMethod · 0.45
as_ulMethod · 0.45
get_json_dataMethod · 0.45
dumpsMethod · 0.45

Tested by

no test coverage detected