Refs #33990 -- Renamed SimpleTestCase.assertFormsetError() to assertFormSetError().

Co-Authored-By: Michael Howitz <mh@gocept.com>
This commit is contained in:
Gregor Gärtner 2022-09-24 11:29:58 +01:00 committed by Mariusz Felisiak
parent fa9ac16c13
commit 564b317fb5
9 changed files with 91 additions and 64 deletions

View File

@ -54,7 +54,7 @@ from django.test.utils import (
modify_settings, modify_settings,
override_settings, override_settings,
) )
from django.utils.deprecation import RemovedInDjango50Warning from django.utils.deprecation import RemovedInDjango50Warning, RemovedInDjango51Warning
from django.utils.functional import classproperty from django.utils.functional import classproperty
from django.utils.version import PY310 from django.utils.version import PY310
from django.views.static import serve from django.views.static import serve
@ -206,18 +206,18 @@ class _AssertFormErrorDeprecationHelper:
) )
@staticmethod @staticmethod
def assertFormsetError( def assertFormSetError(
self, response, formset, form_index, field, errors, msg_prefix="" self, response, formset, form_index, field, errors, msg_prefix=""
): ):
""" """
Search for a formset named "formset" in the "response" and dispatch to Search for a formset named "formset" in the "response" and dispatch to
the new assertFormsetError() using that instance. If the name is found the new assertFormSetError() using that instance. If the name is found
in multiple contexts they're all checked in order and any failure will in multiple contexts they're all checked in order and any failure will
abort the test. abort the test.
""" """
warning_msg = ( warning_msg = (
f"Passing response to assertFormsetError() is deprecated. Use the formset " f"Passing response to assertFormSetError() is deprecated. Use the formset "
f"object directly: assertFormsetError(response.context[{formset!r}], " f"object directly: assertFormSetError(response.context[{formset!r}], "
f"{form_index!r}, ...)" f"{form_index!r}, ...)"
) )
warnings.warn(warning_msg, RemovedInDjango50Warning, stacklevel=2) warnings.warn(warning_msg, RemovedInDjango50Warning, stacklevel=2)
@ -234,7 +234,7 @@ class _AssertFormErrorDeprecationHelper:
if formset not in context or not hasattr(context[formset], "forms"): if formset not in context or not hasattr(context[formset], "forms"):
continue continue
found_formset = True found_formset = True
self.assertFormsetError( self.assertFormSetError(
context[formset], form_index, field, errors, msg_prefix context[formset], form_index, field, errors, msg_prefix
) )
if not found_formset: if not found_formset:
@ -737,10 +737,19 @@ class SimpleTestCase(unittest.TestCase):
errors = to_list(errors) errors = to_list(errors)
self._assert_form_error(form, field, errors, msg_prefix, f"form {form!r}") self._assert_form_error(form, field, errors, msg_prefix, f"form {form!r}")
# RemovedInDjango51Warning.
def assertFormsetError(self, *args, **kw):
warnings.warn(
"assertFormsetError() is deprecated in favor of assertFormSetError().",
category=RemovedInDjango51Warning,
stacklevel=2,
)
return self.assertFormSetError(*args, **kw)
# RemovedInDjango50Warning: When the deprecation ends, remove the # RemovedInDjango50Warning: When the deprecation ends, remove the
# decorator. # decorator.
@_AssertFormErrorDeprecationHelper.patch_signature @_AssertFormErrorDeprecationHelper.patch_signature
def assertFormsetError(self, formset, form_index, field, errors, msg_prefix=""): def assertFormSetError(self, formset, form_index, field, errors, msg_prefix=""):
""" """
Similar to assertFormError() but for formsets. Similar to assertFormError() but for formsets.
@ -752,7 +761,7 @@ class SimpleTestCase(unittest.TestCase):
""" """
if errors is None: if errors is None:
warnings.warn( warnings.warn(
"Passing errors=None to assertFormsetError() is deprecated, " "Passing errors=None to assertFormSetError() is deprecated, "
"use errors=[] instead.", "use errors=[] instead.",
RemovedInDjango50Warning, RemovedInDjango50Warning,
stacklevel=2, stacklevel=2,

View File

@ -35,6 +35,8 @@ details on these changes.
* The ``map_width`` and ``map_height`` attributes of ``BaseGeometryWidget`` * The ``map_width`` and ``map_height`` attributes of ``BaseGeometryWidget``
will be removed. will be removed.
* The ``SimpleTestCase.assertFormsetError()`` method will be removed.
.. _deprecation-removed-in-5.0: .. _deprecation-removed-in-5.0:
5.0 5.0

View File

@ -321,7 +321,7 @@ Minor features
* The :class:`~django.test.SimpleTestCase` class includes a new assertion * The :class:`~django.test.SimpleTestCase` class includes a new assertion
helper for testing formset errors: helper for testing formset errors:
:meth:`~django.test.SimpleTestCase.assertFormsetError`. ``django.test.SimpleTestCase.assertFormsetError()``.
* The list of related fields added to a * The list of related fields added to a
:class:`~django.db.models.query.QuerySet` by :class:`~django.db.models.query.QuerySet` by

View File

@ -50,8 +50,7 @@ Bugfixes
======== ========
* Fixed a regression in Django 4.0 that caused a crash of * Fixed a regression in Django 4.0 that caused a crash of
:meth:`~django.test.SimpleTestCase.assertFormsetError` on a formset named ``assertFormsetError()`` on a formset named ``form`` (:ticket:`33346`).
``form`` (:ticket:`33346`).
* Fixed a bug in Django 4.0 that caused a crash on booleans with the * Fixed a bug in Django 4.0 that caused a crash on booleans with the
``RedisCache`` backend (:ticket:`33361`). ``RedisCache`` backend (:ticket:`33361`).

View File

@ -407,8 +407,8 @@ Tests
raises a ``RuntimeError``, the same as outside of tests. raises a ``RuntimeError``, the same as outside of tests.
* :meth:`.SimpleTestCase.assertFormError` and * :meth:`.SimpleTestCase.assertFormError` and
:meth:`~.SimpleTestCase.assertFormsetError` now support passing a :meth:`assertFormsetError() <django.test.SimpleTestCase.assertFormSetError>`
form/formset object directly. now support passing a form/formset object directly.
URLs URLs
~~~~ ~~~~
@ -671,8 +671,8 @@ Miscellaneous
* The undocumented ability to pass ``errors=None`` to * The undocumented ability to pass ``errors=None`` to
:meth:`.SimpleTestCase.assertFormError` and :meth:`.SimpleTestCase.assertFormError` and
:meth:`~.SimpleTestCase.assertFormsetError` is deprecated. Use ``errors=[]`` :meth:`assertFormsetError() <django.test.SimpleTestCase.assertFormSetError>`
instead. is deprecated. Use ``errors=[]`` instead.
* ``django.contrib.sessions.serializers.PickleSerializer`` is deprecated due to * ``django.contrib.sessions.serializers.PickleSerializer`` is deprecated due to
the risk of remote code execution. the risk of remote code execution.

View File

@ -419,3 +419,6 @@ Miscellaneous
* The ``map_height`` and ``map_width`` attributes of ``BaseGeometryWidget`` are * The ``map_height`` and ``map_width`` attributes of ``BaseGeometryWidget`` are
deprecated, use CSS to size map widgets instead. deprecated, use CSS to size map widgets instead.
* ``SimpleTestCase.assertFormsetError()`` is deprecated in favor of
``assertFormSetError()``.

View File

@ -1573,16 +1573,16 @@ your test suite.
``assertFormError()`` is deprecated and will be removed in Django 5.0. ``assertFormError()`` is deprecated and will be removed in Django 5.0.
Use the form instance directly instead. Use the form instance directly instead.
.. method:: SimpleTestCase.assertFormsetError(formset, form_index, field, errors, msg_prefix='') .. method:: SimpleTestCase.assertFormSetError(formset, form_index, field, errors, msg_prefix='')
Asserts that the ``formset`` raises the provided list of errors when Asserts that the ``formset`` raises the provided list of errors when
rendered. rendered.
``formset`` is a ``Formset`` instance. The formset must be bound but not ``formset`` is a ``FormSet`` instance. The formset must be bound but not
necessarily validated (``assertFormsetError()`` will automatically call the necessarily validated (``assertFormSetError()`` will automatically call the
``full_clean()`` on the formset). ``full_clean()`` on the formset).
``form_index`` is the number of the form within the ``Formset`` (starting ``form_index`` is the number of the form within the ``FormSet`` (starting
from 0). Use ``form_index=None`` to check the formset's non-form errors, from 0). Use ``form_index=None`` to check the formset's non-form errors,
i.e. the errors you get when calling ``formset.non_form_errors()``. In that i.e. the errors you get when calling ``formset.non_form_errors()``. In that
case you must also use ``field=None``. case you must also use ``field=None``.
@ -1593,9 +1593,14 @@ your test suite.
.. deprecated:: 4.1 .. deprecated:: 4.1
Support for passing a response object and a formset name to Support for passing a response object and a formset name to
``assertFormsetError()`` is deprecated and will be removed in Django ``assertFormSetError()`` is deprecated and will be removed in Django
5.0. Use the formset instance directly instead. 5.0. Use the formset instance directly instead.
.. deprecated:: 4.2
The ``assertFormsetError()`` assertion method is deprecated. Use
``assertFormSetError()`` instead.
.. method:: SimpleTestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='', html=False) .. method:: SimpleTestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='', html=False)
Asserts that a :class:`response <django.http.HttpResponse>` produced the Asserts that a :class:`response <django.http.HttpResponse>` produced the

View File

@ -8110,7 +8110,7 @@ class AdminViewOnSiteTests(TestCase):
Issue #20522 Issue #20522
Verifying that if the parent form fails validation, the inlines also Verifying that if the parent form fails validation, the inlines also
run validation even if validation is contingent on parent form data. run validation even if validation is contingent on parent form data.
Also, assertFormError() and assertFormsetError() is usable for admin Also, assertFormError() and assertFormSetError() is usable for admin
forms and formsets. forms and formsets.
""" """
# The form validation should fail because 'some_required_info' is # The form validation should fail because 'some_required_info' is
@ -8134,7 +8134,7 @@ class AdminViewOnSiteTests(TestCase):
["This field is required."], ["This field is required."],
) )
self.assertFormError(response.context["adminform"], None, []) self.assertFormError(response.context["adminform"], None, [])
self.assertFormsetError( self.assertFormSetError(
response.context["inline_admin_formset"], response.context["inline_admin_formset"],
0, 0,
None, None,
@ -8143,7 +8143,7 @@ class AdminViewOnSiteTests(TestCase):
"contrived test case" "contrived test case"
], ],
) )
self.assertFormsetError( self.assertFormSetError(
response.context["inline_admin_formset"], None, None, [] response.context["inline_admin_formset"], None, None, []
) )
@ -8179,7 +8179,7 @@ class AdminViewOnSiteTests(TestCase):
"some_required_info", "some_required_info",
["This field is required."], ["This field is required."],
) )
self.assertFormsetError( self.assertFormSetError(
response.context["inline_admin_formset"], response.context["inline_admin_formset"],
0, 0,
None, None,

View File

@ -46,7 +46,7 @@ from django.test.utils import (
setup_test_environment, setup_test_environment,
) )
from django.urls import NoReverseMatch, path, reverse, reverse_lazy from django.urls import NoReverseMatch, path, reverse, reverse_lazy
from django.utils.deprecation import RemovedInDjango50Warning from django.utils.deprecation import RemovedInDjango50Warning, RemovedInDjango51Warning
from django.utils.log import DEFAULT_LOGGING from django.utils.log import DEFAULT_LOGGING
from django.utils.version import PY311 from django.utils.version import PY311
@ -1517,44 +1517,53 @@ class AssertFormErrorTests(SimpleTestCase):
) )
class AssertFormsetErrorTests(SimpleTestCase): class AssertFormSetErrorTests(SimpleTestCase):
@ignore_warnings(category=RemovedInDjango50Warning) @ignore_warnings(category=RemovedInDjango50Warning)
def test_non_client_response(self): def test_non_client_response(self):
msg = ( msg = (
"assertFormsetError() is only usable on responses fetched using " "assertFormSetError() is only usable on responses fetched using "
"the Django test Client." "the Django test Client."
) )
response = HttpResponse() response = HttpResponse()
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
self.assertFormsetError(response, "formset", 0, "field", "invalid value") self.assertFormSetError(response, "formset", 0, "field", "invalid value")
@ignore_warnings(category=RemovedInDjango50Warning) @ignore_warnings(category=RemovedInDjango50Warning)
def test_response_with_no_context(self): def test_response_with_no_context(self):
msg = "Response did not use any contexts to render the response" msg = "Response did not use any contexts to render the response"
response = mock.Mock(context=[]) response = mock.Mock(context=[])
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(response, "formset", 0, "field", "invalid value") self.assertFormSetError(response, "formset", 0, "field", "invalid value")
@ignore_warnings(category=RemovedInDjango50Warning) @ignore_warnings(category=RemovedInDjango50Warning)
def test_formset_not_in_context(self): def test_formset_not_in_context(self):
msg = "The formset 'formset' was not used to render the response" msg = "The formset 'formset' was not used to render the response"
response = mock.Mock(context=[{}]) response = mock.Mock(context=[{}])
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(response, "formset", 0, "field", "invalid value") self.assertFormSetError(response, "formset", 0, "field", "invalid value")
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
response, "formset", 0, "field", "invalid value", msg_prefix=msg_prefix response, "formset", 0, "field", "invalid value", msg_prefix=msg_prefix
) )
def test_single_error(self): def test_rename_assertformseterror_deprecation_warning(self):
msg = "assertFormsetError() is deprecated in favor of assertFormSetError()."
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
self.assertFormsetError()
@ignore_warnings(category=RemovedInDjango51Warning)
def test_deprecated_assertformseterror(self):
self.assertFormsetError(TestFormset.invalid(), 0, "field", "invalid value") self.assertFormsetError(TestFormset.invalid(), 0, "field", "invalid value")
def test_single_error(self):
self.assertFormSetError(TestFormset.invalid(), 0, "field", "invalid value")
def test_error_list(self): def test_error_list(self):
self.assertFormsetError(TestFormset.invalid(), 0, "field", ["invalid value"]) self.assertFormSetError(TestFormset.invalid(), 0, "field", ["invalid value"])
def test_empty_errors_valid_formset(self): def test_empty_errors_valid_formset(self):
self.assertFormsetError(TestFormset.valid(), 0, "field", []) self.assertFormSetError(TestFormset.valid(), 0, "field", [])
def test_multiple_forms(self): def test_multiple_forms(self):
formset = TestFormset( formset = TestFormset(
@ -1566,8 +1575,8 @@ class AssertFormsetErrorTests(SimpleTestCase):
} }
) )
formset.full_clean() formset.full_clean()
self.assertFormsetError(formset, 0, "field", []) self.assertFormSetError(formset, 0, "field", [])
self.assertFormsetError(formset, 1, "field", ["invalid value"]) self.assertFormSetError(formset, 1, "field", ["invalid value"])
def test_field_not_in_form(self): def test_field_not_in_form(self):
msg = ( msg = (
@ -1575,12 +1584,12 @@ class AssertFormsetErrorTests(SimpleTestCase):
"does not contain the field 'other_field'." "does not contain the field 'other_field'."
) )
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(), 0, "other_field", "invalid value" TestFormset.invalid(), 0, "other_field", "invalid value"
) )
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(), TestFormset.invalid(),
0, 0,
"other_field", "other_field",
@ -1594,11 +1603,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
"valid=True total_forms=1> don't match." "valid=True total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(TestFormset.valid(), 0, "field", "invalid value") self.assertFormSetError(TestFormset.valid(), 0, "field", "invalid value")
self.assertIn("[] != ['invalid value']", str(ctx.exception)) self.assertIn("[] != ['invalid value']", str(ctx.exception))
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.valid(), 0, "field", "invalid value", msg_prefix=msg_prefix TestFormset.valid(), 0, "field", "invalid value", msg_prefix=msg_prefix
) )
@ -1608,11 +1617,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
"valid=False total_forms=1> don't match." "valid=False total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(TestFormset.invalid(), 0, "field", "other error") self.assertFormSetError(TestFormset.invalid(), 0, "field", "other error")
self.assertIn("['invalid value'] != ['other error']", str(ctx.exception)) self.assertIn("['invalid value'] != ['other error']", str(ctx.exception))
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(), 0, "field", "other error", msg_prefix=msg_prefix TestFormset.invalid(), 0, "field", "other error", msg_prefix=msg_prefix
) )
@ -1622,7 +1631,7 @@ class AssertFormsetErrorTests(SimpleTestCase):
"bound, it will never have any errors." "bound, it will never have any errors."
) )
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(TestFormset(), 0, "field", []) self.assertFormSetError(TestFormset(), 0, "field", [])
def test_empty_errors_invalid_formset(self): def test_empty_errors_invalid_formset(self):
msg = ( msg = (
@ -1630,11 +1639,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
"valid=False total_forms=1> don't match." "valid=False total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(TestFormset.invalid(), 0, "field", []) self.assertFormSetError(TestFormset.invalid(), 0, "field", [])
self.assertIn("['invalid value'] != []", str(ctx.exception)) self.assertIn("['invalid value'] != []", str(ctx.exception))
def test_non_field_errors(self): def test_non_field_errors(self):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(nonfield=True), 0, None, "non-field error" TestFormset.invalid(nonfield=True), 0, None, "non-field error"
) )
@ -1644,7 +1653,7 @@ class AssertFormsetErrorTests(SimpleTestCase):
"valid=False total_forms=1> don't match." "valid=False total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(nonfield=True), 0, None, "other non-field error" TestFormset.invalid(nonfield=True), 0, None, "other non-field error"
) )
self.assertIn( self.assertIn(
@ -1652,7 +1661,7 @@ class AssertFormsetErrorTests(SimpleTestCase):
) )
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(nonfield=True), TestFormset.invalid(nonfield=True),
0, 0,
None, None,
@ -1666,16 +1675,16 @@ class AssertFormsetErrorTests(SimpleTestCase):
"valid=False total_forms=1> don't match." "valid=False total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(TestFormset.invalid(), 0, None, "non-field error") self.assertFormSetError(TestFormset.invalid(), 0, None, "non-field error")
self.assertIn("[] != ['non-field error']", str(ctx.exception)) self.assertIn("[] != ['non-field error']", str(ctx.exception))
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(), 0, None, "non-field error", msg_prefix=msg_prefix TestFormset.invalid(), 0, None, "non-field error", msg_prefix=msg_prefix
) )
def test_non_form_errors(self): def test_non_form_errors(self):
self.assertFormsetError(TestFormset.invalid(nonform=True), None, None, "error") self.assertFormSetError(TestFormset.invalid(nonform=True), None, None, "error")
def test_different_non_form_errors(self): def test_different_non_form_errors(self):
msg = ( msg = (
@ -1683,13 +1692,13 @@ class AssertFormsetErrorTests(SimpleTestCase):
"total_forms=0> don't match." "total_forms=0> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(nonform=True), None, None, "other error" TestFormset.invalid(nonform=True), None, None, "other error"
) )
self.assertIn("['error'] != ['other error']", str(ctx.exception)) self.assertIn("['error'] != ['other error']", str(ctx.exception))
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(nonform=True), TestFormset.invalid(nonform=True),
None, None,
None, None,
@ -1703,11 +1712,11 @@ class AssertFormsetErrorTests(SimpleTestCase):
"total_forms=1> don't match." "total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg) as ctx: with self.assertRaisesMessage(AssertionError, msg) as ctx:
self.assertFormsetError(TestFormset.invalid(), None, None, "error") self.assertFormSetError(TestFormset.invalid(), None, None, "error")
self.assertIn("[] != ['error']", str(ctx.exception)) self.assertIn("[] != ['error']", str(ctx.exception))
msg_prefix = "Custom prefix" msg_prefix = "Custom prefix"
with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"): with self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {msg}"):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(), TestFormset.invalid(),
None, None,
None, None,
@ -1718,7 +1727,7 @@ class AssertFormsetErrorTests(SimpleTestCase):
def test_non_form_errors_with_field(self): def test_non_form_errors_with_field(self):
msg = "You must use field=None with form_index=None." msg = "You must use field=None with form_index=None."
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
self.assertFormsetError( self.assertFormSetError(
TestFormset.invalid(nonform=True), None, "field", "error" TestFormset.invalid(nonform=True), None, "field", "error"
) )
@ -1728,7 +1737,7 @@ class AssertFormsetErrorTests(SimpleTestCase):
"1 form." "1 form."
) )
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(TestFormset.invalid(), 2, "field", "error") self.assertFormSetError(TestFormset.invalid(), 2, "field", "error")
def test_form_index_too_big_plural(self): def test_form_index_too_big_plural(self):
formset = TestFormset( formset = TestFormset(
@ -1745,7 +1754,7 @@ class AssertFormsetErrorTests(SimpleTestCase):
"forms." "forms."
) )
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(formset, 2, "field", "error") self.assertFormSetError(formset, 2, "field", "error")
# RemovedInDjango50Warning # RemovedInDjango50Warning
@ -1845,15 +1854,15 @@ class AssertFormErrorDeprecationTests(SimpleTestCase):
"valid=False total_forms=1> don't match." "valid=False total_forms=1> don't match."
) )
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
self.assertFormsetError(TestFormset.invalid(), 0, "field", None) self.assertFormSetError(TestFormset.invalid(), 0, "field", None)
def test_assert_formset_error_errors_none_warning(self): def test_assert_formset_error_errors_none_warning(self):
msg = ( msg = (
"Passing errors=None to assertFormsetError() is deprecated, use " "Passing errors=None to assertFormSetError() is deprecated, use "
"errors=[] instead." "errors=[] instead."
) )
with self.assertWarnsMessage(RemovedInDjango50Warning, msg): with self.assertWarnsMessage(RemovedInDjango50Warning, msg):
self.assertFormsetError(TestFormset.valid(), 0, "field", None) self.assertFormSetError(TestFormset.valid(), 0, "field", None)
def _assert_formset_error_old_api_cases( def _assert_formset_error_old_api_cases(
self, formset, form_index, field, errors, msg_prefix self, formset, form_index, field, errors, msg_prefix
@ -1933,8 +1942,8 @@ class AssertFormErrorDeprecationTests(SimpleTestCase):
def test_assert_formset_error_old_api(self): def test_assert_formset_error_old_api(self):
deprecation_msg = ( deprecation_msg = (
"Passing response to assertFormsetError() is deprecated. Use the formset " "Passing response to assertFormSetError() is deprecated. Use the formset "
"object directly: assertFormsetError(response.context['formset'], 0, ...)" "object directly: assertFormSetError(response.context['formset'], 0, ...)"
) )
for args, kwargs in self._assert_formset_error_old_api_cases( for args, kwargs in self._assert_formset_error_old_api_cases(
formset="formset", formset="formset",
@ -1945,7 +1954,7 @@ class AssertFormErrorDeprecationTests(SimpleTestCase):
): ):
with self.subTest(args=args, kwargs=kwargs): with self.subTest(args=args, kwargs=kwargs):
with self.assertWarnsMessage(RemovedInDjango50Warning, deprecation_msg): with self.assertWarnsMessage(RemovedInDjango50Warning, deprecation_msg):
self.assertFormsetError(*args, **kwargs) self.assertFormSetError(*args, **kwargs)
@ignore_warnings(category=RemovedInDjango50Warning) @ignore_warnings(category=RemovedInDjango50Warning)
def test_assert_formset_error_old_api_assertion_error(self): def test_assert_formset_error_old_api_assertion_error(self):
@ -1958,7 +1967,7 @@ class AssertFormErrorDeprecationTests(SimpleTestCase):
): ):
with self.subTest(args=args, kwargs=kwargs): with self.subTest(args=args, kwargs=kwargs):
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
self.assertFormsetError(*args, **kwargs) self.assertFormSetError(*args, **kwargs)
class FirstUrls: class FirstUrls: