Refs #33348 -- Made SimpleTestCase.assertFormError()/assertFormsetErrors() raise an error for unbound forms/formsets.

This commit is contained in:
Baptiste Mispelon 2022-02-14 08:42:27 +01:00 committed by Mariusz Felisiak
parent cdd4ff67d2
commit 7986028e3f
3 changed files with 32 additions and 2 deletions

View File

@ -83,6 +83,10 @@ class AdminForm:
def non_field_errors(self):
return self.form.non_field_errors
@property
def is_bound(self):
return self.form.is_bound
@property
def media(self):
media = self.form.media
@ -437,6 +441,10 @@ class InlineAdminFormSet:
def non_form_errors(self):
return self.formset.non_form_errors
@property
def is_bound(self):
return self.formset.is_bound
@property
def media(self):
media = self.opts.media + self.formset.media

View File

@ -595,6 +595,12 @@ class SimpleTestCase(unittest.TestCase):
for i, context in enumerate(contexts):
if form not in context:
continue
if not context[form].is_bound:
form_repr = repr(context[form])
self.fail(
f"{msg_prefix}The form {form_repr} is not bound, it will never "
f"have any errors."
)
found_form = True
for err in errors:
if field:
@ -680,6 +686,12 @@ class SimpleTestCase(unittest.TestCase):
for i, context in enumerate(contexts):
if formset not in context or not hasattr(context[formset], "forms"):
continue
if not context[formset].is_bound:
formset_repr = repr(context[formset])
self.fail(
f"{msg_prefix}The formset {formset_repr} is not bound, it will "
f"never have any errors."
)
found_formset = True
for err in errors:
if field is not None:

View File

@ -1454,7 +1454,12 @@ class AssertFormErrorTests(SimpleTestCase):
self.assertFormError(response, "form", "field", "invalid value")
def test_empty_errors_unbound_form(self):
msg = (
"The form <TestForm bound=False, valid=Unknown, fields=(field)> is not "
"bound, it will never have any errors."
)
response = mock.Mock(context=[{"form": TestForm()}])
with self.assertRaisesMessage(AssertionError, msg):
self.assertFormError(response, "form", "field", [])
def test_empty_errors_valid_form(self):
@ -1590,7 +1595,12 @@ class AssertFormsetErrorTests(SimpleTestCase):
self.assertFormsetError(response, "formset", 0, "field", "invalid value")
def test_empty_errors_unbound_formset(self):
msg = (
"The formset <TestFormset: bound=False valid=Unknown total_forms=1> is not "
"bound, it will never have any errors."
)
response = mock.Mock(context=[{"formset": TestFormset()}])
with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(response, "formset", 0, "field", [])
def test_empty_errors_valid_formset(self):