Refs #33348 -- Deprecated passing errors=None to SimpleTestCase.assertFormError()/assertFormsetErrors().

This commit is contained in:
Baptiste Mispelon 2022-01-04 13:02:14 +01:00 committed by Mariusz Felisiak
parent bc174e6ea0
commit c67e1cf44f
4 changed files with 61 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import posixpath
import sys import sys
import threading import threading
import unittest import unittest
import warnings
from collections import Counter from collections import Counter
from contextlib import contextmanager from contextlib import contextmanager
from copy import copy, deepcopy from copy import copy, deepcopy
@ -41,6 +42,7 @@ from django.test.utils import (
CaptureQueriesContext, ContextList, compare_xml, modify_settings, CaptureQueriesContext, ContextList, compare_xml, modify_settings,
override_settings, override_settings,
) )
from django.utils.deprecation import RemovedInDjango50Warning
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
@ -50,13 +52,8 @@ __all__ = ('TestCase', 'TransactionTestCase',
def to_list(value): def to_list(value):
""" """Put value into a list if it's not already one."""
Put value into a list if it's not already one. Return an empty list if if not isinstance(value, list):
value is None.
"""
if value is None:
value = []
elif not isinstance(value, list):
value = [value] value = [value]
return value return value
@ -493,10 +490,18 @@ class SimpleTestCase(unittest.TestCase):
msg_prefix += ": " msg_prefix += ": "
# Put context(s) into a list to simplify processing. # Put context(s) into a list to simplify processing.
contexts = to_list(response.context) contexts = [] if response.context is None else to_list(response.context)
if not contexts: if not contexts:
self.fail(msg_prefix + "Response did not use any contexts to render the response") self.fail(msg_prefix + "Response did not use any contexts to render the response")
if errors is None:
warnings.warn(
'Passing errors=None to assertFormError() is deprecated, use '
'errors=[] instead.',
RemovedInDjango50Warning,
stacklevel=2,
)
errors = []
# Put error(s) into a list to simplify processing. # Put error(s) into a list to simplify processing.
errors = to_list(errors) errors = to_list(errors)
@ -556,11 +561,19 @@ class SimpleTestCase(unittest.TestCase):
msg_prefix += ": " msg_prefix += ": "
# Put context(s) into a list to simplify processing. # Put context(s) into a list to simplify processing.
contexts = to_list(response.context) contexts = [] if response.context is None else to_list(response.context)
if not contexts: if not contexts:
self.fail(msg_prefix + 'Response did not use any contexts to ' self.fail(msg_prefix + 'Response did not use any contexts to '
'render the response') 'render the response')
if errors is None:
warnings.warn(
'Passing errors=None to assertFormsetError() is deprecated, '
'use errors=[] instead.',
RemovedInDjango50Warning,
stacklevel=2,
)
errors = []
# Put error(s) into a list to simplify processing. # Put error(s) into a list to simplify processing.
errors = to_list(errors) errors = to_list(errors)

View File

@ -75,6 +75,10 @@ details on these changes.
* The ``opclasses`` argument of * The ``opclasses`` argument of
``django.contrib.postgres.constraints.ExclusionConstraint`` will be removed. ``django.contrib.postgres.constraints.ExclusionConstraint`` will be removed.
* The undocumented ability to pass ``errors=None`` to
``SimpleTestCase.assertFormError()`` and ``assertFormsetError()`` will be
removed.
.. _deprecation-removed-in-4.1: .. _deprecation-removed-in-4.1:
4.1 4.1

View File

@ -385,6 +385,11 @@ Miscellaneous
), ),
] ]
* The undocumented ability to pass ``errors=None`` to
:meth:`.SimpleTestCase.assertFormError` and
:meth:`~.SimpleTestCase.assertFormsetError` is deprecated. Use ``errors=[]``
instead.
Features removed in 4.1 Features removed in 4.1
======================= =======================

View File

@ -26,10 +26,11 @@ from django.test import (
from django.test.html import HTMLParseError, parse_html from django.test.html import HTMLParseError, parse_html
from django.test.testcases import DatabaseOperationForbidden from django.test.testcases import DatabaseOperationForbidden
from django.test.utils import ( from django.test.utils import (
CaptureQueriesContext, TestContextDecorator, isolate_apps, CaptureQueriesContext, TestContextDecorator, ignore_warnings, isolate_apps,
override_settings, setup_test_environment, override_settings, 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.log import DEFAULT_LOGGING from django.utils.log import DEFAULT_LOGGING
from .models import Car, Person, PossessedCar from .models import Car, Person, PossessedCar
@ -1418,6 +1419,20 @@ class AssertFormErrorTests(SimpleTestCase):
response = mock.Mock(context=[{'form': TestForm.invalid(nonfield=True)}]) response = mock.Mock(context=[{'form': TestForm.invalid(nonfield=True)}])
self.assertFormError(response, 'form', None, 'non-field error') self.assertFormError(response, 'form', None, 'non-field error')
@ignore_warnings(category=RemovedInDjango50Warning)
def test_errors_none(self):
response = mock.Mock(context=[{'form': TestForm.invalid()}])
self.assertFormError(response, 'form', 'field', None)
def test_errors_none_warning(self):
response = mock.Mock(context=[{'form': TestForm.invalid()}])
msg = (
'Passing errors=None to assertFormError() is deprecated, use '
'errors=[] instead.'
)
with self.assertWarnsMessage(RemovedInDjango50Warning, msg):
self.assertFormError(response, 'form', 'value', None)
class AssertFormsetErrorTests(SimpleTestCase): class AssertFormsetErrorTests(SimpleTestCase):
def _get_formset_data(self, field_value): def _get_formset_data(self, field_value):
@ -1560,6 +1575,20 @@ class AssertFormsetErrorTests(SimpleTestCase):
]) ])
self.assertFormsetError(response, 'form', 0, 'field', 'invalid value') self.assertFormsetError(response, 'form', 0, 'field', 'invalid value')
@ignore_warnings(category=RemovedInDjango50Warning)
def test_errors_none(self):
response = mock.Mock(context=[{'formset': TestFormset.invalid()}])
self.assertFormsetError(response, 'formset', 0, 'field', None)
def test_errors_none_warning(self):
response = mock.Mock(context=[{'formset': TestFormset.invalid()}])
msg = (
'Passing errors=None to assertFormsetError() is deprecated, use '
'errors=[] instead.'
)
with self.assertWarnsMessage(RemovedInDjango50Warning, msg):
self.assertFormsetError(response, 'formset', 0, 'field', None)
class FirstUrls: class FirstUrls:
urlpatterns = [path('first/', empty_response, name='first')] urlpatterns = [path('first/', empty_response, name='first')]