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:
Mariusz Felisiak 2022-04-05 08:37:28 +02:00 committed by GitHub
parent 78277faafd
commit 1a7d75cf77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 135 additions and 398 deletions

View File

@ -42,7 +42,6 @@ urlpatterns = [
path("bad_view/", views.bad_view),
path("form_view/", views.form_view),
path("form_view_with_template/", views.form_view_with_template),
path("formset_view/", views.formset_view),
path("json_view/", views.json_view),
path("login_protected_view/", views.login_protected_view),
path("login_protected_method_view/", views.login_protected_method_view),

View File

@ -7,7 +7,6 @@ from django.core import mail
from django.core.exceptions import ValidationError
from django.forms import fields
from django.forms.forms import Form
from django.forms.formsets import BaseFormSet, formset_factory
from django.http import (
HttpResponse,
HttpResponseBadRequest,
@ -259,48 +258,6 @@ def form_view_with_template(request):
)
class BaseTestFormSet(BaseFormSet):
def clean(self):
"""No two email addresses are the same."""
if any(self.errors):
# Don't bother validating the formset unless each form is valid
return
emails = []
for form in self.forms:
email = form.cleaned_data["email"]
if email in emails:
raise ValidationError(
"Forms in a set must have distinct email addresses."
)
emails.append(email)
TestFormSet = formset_factory(TestForm, BaseTestFormSet)
def formset_view(request):
"A view that tests a simple formset"
if request.method == "POST":
formset = TestFormSet(request.POST)
if formset.is_valid():
t = Template("Valid POST data.", name="Valid POST Template")
c = Context()
else:
t = Template(
"Invalid POST data. {{ my_formset.errors }}",
name="Invalid POST Template",
)
c = Context({"my_formset": formset})
else:
formset = TestForm(request.GET)
t = Template(
"Viewing base formset. {{ my_formset }}.", name="Formset GET Template"
)
c = Context({"my_formset": formset})
return HttpResponse(t.render(c))
@login_required
def login_protected_view(request):
"A simple view that is login protected."

View File

@ -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):

View File

@ -1402,6 +1402,11 @@ class AssertFormErrorTests(SimpleTestCase):
response = mock.Mock(context=[{}])
with self.assertRaisesMessage(AssertionError, msg):
self.assertFormError(response, "form", "field", "invalid value")
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormError(
response, "form", "field", "invalid value", msg_prefix=msg_prefix
)
def test_field_not_in_form(self):
msg = (
@ -1411,6 +1416,11 @@ class AssertFormErrorTests(SimpleTestCase):
response = mock.Mock(context=[{"form": TestForm.invalid()}])
with self.assertRaisesMessage(AssertionError, msg):
self.assertFormError(response, "form", "other_field", "invalid value")
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormError(
response, "form", "other_field", "invalid value", msg_prefix=msg_prefix
)
def test_field_with_no_errors(self):
msg = (
@ -1421,6 +1431,11 @@ class AssertFormErrorTests(SimpleTestCase):
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormError(response, "form", "field", "invalid value")
self.assertIn("[] != ['invalid value']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormError(
response, "form", "field", "invalid value", msg_prefix=msg_prefix
)
def test_field_with_different_error(self):
msg = (
@ -1431,6 +1446,11 @@ class AssertFormErrorTests(SimpleTestCase):
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormError(response, "form", "field", "other error")
self.assertIn("['invalid value'] != ['other error']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormError(
response, "form", "field", "other error", msg_prefix=msg_prefix
)
def test_basic_positive_assertion(self):
response = mock.Mock(context=[{"form": TestForm.invalid()}])
@ -1470,6 +1490,23 @@ class AssertFormErrorTests(SimpleTestCase):
response = mock.Mock(context=[{"form": TestForm.invalid(nonfield=True)}])
self.assertFormError(response, "form", None, "non-field error")
def test_different_non_field_errors(self):
response = mock.Mock(context=[{"form": TestForm.invalid(nonfield=True)}])
msg = (
"The non-field errors of form <TestForm bound=True, valid=False, "
"fields=(field)> don't match."
)
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormError(response, "form", None, "other non-field error")
self.assertIn(
"['non-field error'] != ['other non-field error']", str(ctx.exception)
)
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormError(
response, "form", None, "other non-field error", msg_prefix=msg_prefix
)
@ignore_warnings(category=RemovedInDjango50Warning)
def test_errors_none(self):
msg = (
@ -1518,6 +1555,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
response = mock.Mock(context=[{}])
with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(response, "formset", 0, "field", "invalid value")
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response, "formset", 0, "field", "invalid value", msg_prefix=msg_prefix
)
def test_field_not_in_form(self):
msg = (
@ -1533,6 +1575,16 @@ class AssertFormsetErrorTests(SimpleTestCase):
"other_field",
"invalid value",
)
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response,
"formset",
0,
"other_field",
"invalid value",
msg_prefix=msg_prefix,
)
def test_field_with_no_errors(self):
msg = (
@ -1543,6 +1595,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(response, "formset", 0, "field", "invalid value")
self.assertIn("[] != ['invalid value']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response, "formset", 0, "field", "invalid value", msg_prefix=msg_prefix
)
def test_field_with_different_error(self):
msg = (
@ -1553,6 +1610,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(response, "formset", 0, "field", "other error")
self.assertIn("['invalid value'] != ['other error']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response, "formset", 0, "field", "other error", msg_prefix=msg_prefix
)
def test_basic_positive_assertion(self):
response = mock.Mock(context=[{"formset": TestFormset.invalid()}])
@ -1594,6 +1656,47 @@ class AssertFormsetErrorTests(SimpleTestCase):
)
self.assertFormsetError(response, "formset", 0, None, "non-field error")
def test_different_non_field_errors(self):
response = mock.Mock(
context=[{}, {"formset": TestFormset.invalid(nonfield=True)}],
)
msg = (
"The non-field errors of form 0 of formset <TestFormset: bound=True "
"valid=False total_forms=1> don't match."
)
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(
response, "formset", 0, None, "other non-field error"
)
self.assertIn(
"['non-field error'] != ['other non-field error']", str(ctx.exception)
)
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response,
"formset",
0,
None,
"other non-field error",
msg_prefix=msg_prefix,
)
def test_no_non_field_errors(self):
response = mock.Mock(context=[{}, {"formset": TestFormset.invalid()}])
msg = (
"The non-field errors of form 0 of formset <TestFormset: bound=True "
"valid=False total_forms=1> don't match."
)
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(response, "formset", 0, None, "non-field error")
self.assertIn("[] != ['non-field error']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response, "formset", 0, None, "non-field error", msg_prefix=msg_prefix
)
def test_non_form_errors(self):
response = mock.Mock(
context=[
@ -1603,6 +1706,38 @@ class AssertFormsetErrorTests(SimpleTestCase):
)
self.assertFormsetError(response, "formset", None, None, "error")
def test_different_non_form_errors(self):
response = mock.Mock(
context=[{}, {"formset": TestFormset.invalid(nonform=True)}],
)
msg = (
"The non-form errors of formset <TestFormset: bound=True valid=False "
"total_forms=0> don't match."
)
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(response, "formset", None, None, "other error")
self.assertIn("['error'] != ['other error']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response, "formset", None, None, "other error", msg_prefix=msg_prefix
)
def test_no_non_form_errors(self):
response = mock.Mock(context=[{}, {"formset": TestFormset.invalid()}])
msg = (
"The non-form errors of formset <TestFormset: bound=True valid=False "
"total_forms=1> don't match."
)
with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(response, "formset", None, None, "error")
self.assertIn("[] != ['error']", str(ctx.exception))
msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError(
response, "formset", None, None, "error", msg_prefix=msg_prefix
)
def test_non_form_errors_with_field(self):
response = mock.Mock(
context=[