mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Moved remaining SimpleTestCase.assertFormError()/assertFormsetErrors() tests to test_utils.
This also removes redundant tests in test_client_regress.
Follow up to 68144f4049.
This commit is contained in:
@@ -615,360 +615,6 @@ class AssertRedirectsTests(SimpleTestCase):
|
||||
)
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF="test_client_regress.urls")
|
||||
class AssertFormErrorTests(SimpleTestCase):
|
||||
def test_unknown_form(self):
|
||||
"An assertion is raised if the form name is unknown"
|
||||
post_data = {
|
||||
"text": "Hello World",
|
||||
"email": "not an email address",
|
||||
"value": 37,
|
||||
"single": "b",
|
||||
"multi": ("b", "c", "e"),
|
||||
}
|
||||
response = self.client.post("/form_view/", post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "Invalid POST Template")
|
||||
|
||||
msg = "The form 'wrong_form' was not used to render the response"
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormError(response, "wrong_form", "some_field", "Some error.")
|
||||
with self.assertRaisesMessage(AssertionError, "abc: " + msg):
|
||||
self.assertFormError(
|
||||
response, "wrong_form", "some_field", "Some error.", msg_prefix="abc"
|
||||
)
|
||||
|
||||
def test_unknown_field(self):
|
||||
"An assertion is raised if the field name is unknown"
|
||||
post_data = {
|
||||
"text": "Hello World",
|
||||
"email": "not an email address",
|
||||
"value": 37,
|
||||
"single": "b",
|
||||
"multi": ("b", "c", "e"),
|
||||
}
|
||||
response = self.client.post("/form_view/", post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "Invalid POST Template")
|
||||
|
||||
msg = (
|
||||
"The form <TestForm bound=True, valid=False, "
|
||||
"fields=(text;email;value;single;multi)> does not contain the field "
|
||||
"'some_field'."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormError(response, "form", "some_field", "Some error.")
|
||||
with self.assertRaisesMessage(AssertionError, "abc: " + msg):
|
||||
self.assertFormError(
|
||||
response, "form", "some_field", "Some error.", msg_prefix="abc"
|
||||
)
|
||||
|
||||
def test_noerror_field(self):
|
||||
"An assertion is raised if the field doesn't have any errors"
|
||||
post_data = {
|
||||
"text": "Hello World",
|
||||
"email": "not an email address",
|
||||
"value": 37,
|
||||
"single": "b",
|
||||
"multi": ("b", "c", "e"),
|
||||
}
|
||||
response = self.client.post("/form_view/", post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "Invalid POST Template")
|
||||
|
||||
msg = (
|
||||
"The errors of field 'value' on form <TestForm bound=True, valid=False, "
|
||||
"fields=(text;email;value;single;multi)> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormError(response, "form", "value", "Some error.")
|
||||
with self.assertRaisesMessage(AssertionError, "abc: " + msg):
|
||||
self.assertFormError(
|
||||
response, "form", "value", "Some error.", msg_prefix="abc"
|
||||
)
|
||||
|
||||
def test_unknown_error(self):
|
||||
"An assertion is raised if the field doesn't contain the provided error"
|
||||
post_data = {
|
||||
"text": "Hello World",
|
||||
"email": "not an email address",
|
||||
"value": 37,
|
||||
"single": "b",
|
||||
"multi": ("b", "c", "e"),
|
||||
}
|
||||
response = self.client.post("/form_view/", post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "Invalid POST Template")
|
||||
|
||||
msg = (
|
||||
"The errors of field 'email' on form <TestForm bound=True, valid=False, "
|
||||
"fields=(text;email;value;single;multi)> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormError(response, "form", "email", "Some error.")
|
||||
with self.assertRaisesMessage(AssertionError, "abc: " + msg):
|
||||
self.assertFormError(
|
||||
response, "form", "email", "Some error.", msg_prefix="abc"
|
||||
)
|
||||
|
||||
def test_unknown_nonfield_error(self):
|
||||
"""
|
||||
An assertion is raised if the form's non field errors doesn't contain
|
||||
the provided error.
|
||||
"""
|
||||
post_data = {
|
||||
"text": "Hello World",
|
||||
"email": "not an email address",
|
||||
"value": 37,
|
||||
"single": "b",
|
||||
"multi": ("b", "c", "e"),
|
||||
}
|
||||
response = self.client.post("/form_view/", post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "Invalid POST Template")
|
||||
|
||||
msg = (
|
||||
"The non-field errors of form <TestForm bound=True, valid=False, "
|
||||
"fields=(text;email;value;single;multi)> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormError(response, "form", None, "Some error.")
|
||||
with self.assertRaisesMessage(AssertionError, "abc: " + msg):
|
||||
self.assertFormError(
|
||||
response, "form", None, "Some error.", msg_prefix="abc"
|
||||
)
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF="test_client_regress.urls")
|
||||
class AssertFormsetErrorTests(SimpleTestCase):
|
||||
msg_prefixes = [("", {}), ("abc: ", {"msg_prefix": "abc"})]
|
||||
|
||||
def setUp(self):
|
||||
"""Makes response object for testing field and non-field errors"""
|
||||
# For testing field and non-field errors
|
||||
self.response_form_errors = self.getResponse(
|
||||
{
|
||||
"form-TOTAL_FORMS": "2",
|
||||
"form-INITIAL_FORMS": "2",
|
||||
"form-0-text": "Raise non-field error",
|
||||
"form-0-email": "not an email address",
|
||||
"form-0-value": 37,
|
||||
"form-0-single": "b",
|
||||
"form-0-multi": ("b", "c", "e"),
|
||||
"form-1-text": "Hello World",
|
||||
"form-1-email": "email@domain.com",
|
||||
"form-1-value": 37,
|
||||
"form-1-single": "b",
|
||||
"form-1-multi": ("b", "c", "e"),
|
||||
}
|
||||
)
|
||||
# For testing non-form errors
|
||||
self.response_nonform_errors = self.getResponse(
|
||||
{
|
||||
"form-TOTAL_FORMS": "2",
|
||||
"form-INITIAL_FORMS": "2",
|
||||
"form-0-text": "Hello World",
|
||||
"form-0-email": "email@domain.com",
|
||||
"form-0-value": 37,
|
||||
"form-0-single": "b",
|
||||
"form-0-multi": ("b", "c", "e"),
|
||||
"form-1-text": "Hello World",
|
||||
"form-1-email": "email@domain.com",
|
||||
"form-1-value": 37,
|
||||
"form-1-single": "b",
|
||||
"form-1-multi": ("b", "c", "e"),
|
||||
}
|
||||
)
|
||||
|
||||
def getResponse(self, post_data):
|
||||
response = self.client.post("/formset_view/", post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "Invalid POST Template")
|
||||
return response
|
||||
|
||||
def test_unknown_formset(self):
|
||||
"An assertion is raised if the formset name is unknown"
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
prefix
|
||||
+ "The formset 'wrong_formset' was not used to render the response"
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"wrong_formset",
|
||||
0,
|
||||
"Some_field",
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_unknown_field(self):
|
||||
"An assertion is raised if the field name is unknown"
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The form 0 of formset <TestFormFormSet: bound=True "
|
||||
f"valid=False total_forms=2> does not contain the field 'Some_field'."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
0,
|
||||
"Some_field",
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_no_error_field(self):
|
||||
"An assertion is raised if the field doesn't have any errors"
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The errors of field 'value' on form 1 of formset "
|
||||
f"<TestFormFormSet: bound=True valid=False total_forms=2> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
1,
|
||||
"value",
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_unknown_error(self):
|
||||
"An assertion is raised if the field doesn't contain the specified error"
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The errors of field 'email' on form 0 of formset "
|
||||
f"<TestFormFormSet: bound=True valid=False total_forms=2> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
0,
|
||||
"email",
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_field_error(self):
|
||||
"No assertion is raised if the field contains the provided error"
|
||||
error_msg = ["Enter a valid email address."]
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors, "my_formset", 0, "email", error_msg, **kwargs
|
||||
)
|
||||
|
||||
def test_no_nonfield_error(self):
|
||||
"""
|
||||
An assertion is raised if the formsets non-field errors doesn't contain
|
||||
any errors.
|
||||
"""
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The non-field errors of form 1 of formset <TestFormFormSet: "
|
||||
f"bound=True valid=False total_forms=2> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
1,
|
||||
None,
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_unknown_nonfield_error(self):
|
||||
"""
|
||||
An assertion is raised if the formsets non-field errors doesn't contain
|
||||
the provided error.
|
||||
"""
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The non-field errors of form 0 of formset <TestFormFormSet: "
|
||||
f"bound=True valid=False total_forms=2> don't match."
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
0,
|
||||
None,
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_nonfield_error(self):
|
||||
"""
|
||||
No assertion is raised if the formsets non-field errors contains the
|
||||
provided error.
|
||||
"""
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
0,
|
||||
None,
|
||||
"Non-field error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_no_nonform_error(self):
|
||||
"""
|
||||
An assertion is raised if the formsets non-form errors doesn't contain
|
||||
any errors.
|
||||
"""
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The non-form errors of formset <TestFormFormSet: bound=True "
|
||||
f"valid=False total_forms=2> don't match"
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_form_errors,
|
||||
"my_formset",
|
||||
None,
|
||||
None,
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_unknown_nonform_error(self):
|
||||
"""
|
||||
An assertion is raised if the formsets non-form errors doesn't contain
|
||||
the provided error.
|
||||
"""
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
msg = (
|
||||
f"{prefix}The non-form errors of formset <TestFormFormSet: bound=True "
|
||||
f"valid=False total_forms=2> don't match"
|
||||
)
|
||||
with self.assertRaisesMessage(AssertionError, msg):
|
||||
self.assertFormsetError(
|
||||
self.response_nonform_errors,
|
||||
"my_formset",
|
||||
None,
|
||||
None,
|
||||
"Some error.",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def test_nonform_error(self):
|
||||
"""
|
||||
No assertion is raised if the formsets non-form errors contains the
|
||||
provided error.
|
||||
"""
|
||||
msg = "Forms in a set must have distinct email addresses."
|
||||
for prefix, kwargs in self.msg_prefixes:
|
||||
self.assertFormsetError(
|
||||
self.response_nonform_errors, "my_formset", None, None, msg, **kwargs
|
||||
)
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF="test_client_regress.urls")
|
||||
class LoginTests(TestDataMixin, TestCase):
|
||||
def test_login_different_client(self):
|
||||
|
||||
Reference in New Issue
Block a user