Refs #33301 -- Made SimpleTestCase.assertFormError()/assertFormsetErrors() raise ValueError for non test client responses.
This commit is contained in:
parent
68144f4049
commit
9ac92b1efc
|
@ -477,11 +477,23 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(real_count, 0, msg_prefix + "Response should not contain %s" % text_repr)
|
self.assertEqual(real_count, 0, msg_prefix + "Response should not contain %s" % text_repr)
|
||||||
|
|
||||||
|
def _check_test_client_response(self, response, attribute, method_name):
|
||||||
|
"""
|
||||||
|
Raise a ValueError if the given response doesn't have the required
|
||||||
|
attribute.
|
||||||
|
"""
|
||||||
|
if not hasattr(response, attribute):
|
||||||
|
raise ValueError(
|
||||||
|
f"{method_name}() is only usable on responses fetched using "
|
||||||
|
"the Django test Client."
|
||||||
|
)
|
||||||
|
|
||||||
def assertFormError(self, response, form, field, errors, msg_prefix=''):
|
def assertFormError(self, response, form, field, errors, msg_prefix=''):
|
||||||
"""
|
"""
|
||||||
Assert that a form used to render the response has a specific field
|
Assert that a form used to render the response has a specific field
|
||||||
error.
|
error.
|
||||||
"""
|
"""
|
||||||
|
self._check_test_client_response(response, 'context', 'assertFormError')
|
||||||
if msg_prefix:
|
if msg_prefix:
|
||||||
msg_prefix += ": "
|
msg_prefix += ": "
|
||||||
|
|
||||||
|
@ -543,6 +555,7 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
For non-form errors, specify ``form_index`` as None and the ``field``
|
For non-form errors, specify ``form_index`` as None and the ``field``
|
||||||
as None.
|
as None.
|
||||||
"""
|
"""
|
||||||
|
self._check_test_client_response(response, 'context', 'assertFormsetError')
|
||||||
# Add punctuation to msg_prefix
|
# Add punctuation to msg_prefix
|
||||||
if msg_prefix:
|
if msg_prefix:
|
||||||
msg_prefix += ": "
|
msg_prefix += ": "
|
||||||
|
@ -612,7 +625,7 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
if not found_formset:
|
if not found_formset:
|
||||||
self.fail(msg_prefix + "The formset '%s' was not used to render the response" % formset)
|
self.fail(msg_prefix + "The formset '%s' was not used to render the response" % formset)
|
||||||
|
|
||||||
def _assert_template_used(self, response, template_name, msg_prefix):
|
def _assert_template_used(self, response, template_name, msg_prefix, method_name):
|
||||||
|
|
||||||
if response is None and template_name is None:
|
if response is None and template_name is None:
|
||||||
raise TypeError('response and/or template_name argument must be provided')
|
raise TypeError('response and/or template_name argument must be provided')
|
||||||
|
@ -620,11 +633,8 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
if msg_prefix:
|
if msg_prefix:
|
||||||
msg_prefix += ": "
|
msg_prefix += ": "
|
||||||
|
|
||||||
if template_name is not None and response is not None and not hasattr(response, 'templates'):
|
if template_name is not None and response is not None:
|
||||||
raise ValueError(
|
self._check_test_client_response(response, 'templates', method_name)
|
||||||
"assertTemplateUsed() and assertTemplateNotUsed() are only "
|
|
||||||
"usable on responses fetched using the Django test Client."
|
|
||||||
)
|
|
||||||
|
|
||||||
if not hasattr(response, 'templates') or (response is None and template_name):
|
if not hasattr(response, 'templates') or (response is None and template_name):
|
||||||
if response:
|
if response:
|
||||||
|
@ -642,8 +652,8 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
the response. Also usable as context manager.
|
the response. Also usable as context manager.
|
||||||
"""
|
"""
|
||||||
context_mgr_template, template_names, msg_prefix = self._assert_template_used(
|
context_mgr_template, template_names, msg_prefix = self._assert_template_used(
|
||||||
response, template_name, msg_prefix)
|
response, template_name, msg_prefix, 'assertTemplateUsed',
|
||||||
|
)
|
||||||
if context_mgr_template:
|
if context_mgr_template:
|
||||||
# Use assertTemplateUsed as context manager.
|
# Use assertTemplateUsed as context manager.
|
||||||
return _AssertTemplateUsedContext(self, context_mgr_template)
|
return _AssertTemplateUsedContext(self, context_mgr_template)
|
||||||
|
@ -671,7 +681,7 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
rendering the response. Also usable as context manager.
|
rendering the response. Also usable as context manager.
|
||||||
"""
|
"""
|
||||||
context_mgr_template, template_names, msg_prefix = self._assert_template_used(
|
context_mgr_template, template_names, msg_prefix = self._assert_template_used(
|
||||||
response, template_name, msg_prefix
|
response, template_name, msg_prefix, 'assertTemplateNotUsed',
|
||||||
)
|
)
|
||||||
if context_mgr_template:
|
if context_mgr_template:
|
||||||
# Use assertTemplateNotUsed as context manager.
|
# Use assertTemplateNotUsed as context manager.
|
||||||
|
|
|
@ -558,14 +558,10 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_assert_used_on_http_response(self):
|
def test_assert_used_on_http_response(self):
|
||||||
response = HttpResponse()
|
response = HttpResponse()
|
||||||
error_msg = (
|
msg = '%s() is only usable on responses fetched using the Django test Client.'
|
||||||
'assertTemplateUsed() and assertTemplateNotUsed() are only '
|
with self.assertRaisesMessage(ValueError, msg % 'assertTemplateUsed'):
|
||||||
'usable on responses fetched using the Django test Client.'
|
|
||||||
)
|
|
||||||
with self.assertRaisesMessage(ValueError, error_msg):
|
|
||||||
self.assertTemplateUsed(response, 'template.html')
|
self.assertTemplateUsed(response, 'template.html')
|
||||||
|
with self.assertRaisesMessage(ValueError, msg % 'assertTemplateNotUsed'):
|
||||||
with self.assertRaisesMessage(ValueError, error_msg):
|
|
||||||
self.assertTemplateNotUsed(response, 'template.html')
|
self.assertTemplateNotUsed(response, 'template.html')
|
||||||
|
|
||||||
|
|
||||||
|
@ -1288,6 +1284,15 @@ class TestFormset(formset_factory(TestForm)):
|
||||||
|
|
||||||
|
|
||||||
class AssertFormErrorTests(SimpleTestCase):
|
class AssertFormErrorTests(SimpleTestCase):
|
||||||
|
def test_non_client_response(self):
|
||||||
|
msg = (
|
||||||
|
'assertFormError() is only usable on responses fetched using the '
|
||||||
|
'Django test Client.'
|
||||||
|
)
|
||||||
|
response = HttpResponse()
|
||||||
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
self.assertFormError(response, 'formset', 0, 'field', 'invalid value')
|
||||||
|
|
||||||
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=[])
|
||||||
|
@ -1384,6 +1389,15 @@ class AssertFormsetErrorTests(SimpleTestCase):
|
||||||
'form-0-field': field_value,
|
'form-0-field': field_value,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def test_non_client_response(self):
|
||||||
|
msg = (
|
||||||
|
'assertFormsetError() is only usable on responses fetched using '
|
||||||
|
'the Django test Client.'
|
||||||
|
)
|
||||||
|
response = HttpResponse()
|
||||||
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
self.assertFormsetError(response, 'formset', 0, 'field', 'invalid value')
|
||||||
|
|
||||||
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=[])
|
||||||
|
|
Loading…
Reference in New Issue