Fixed #15826 -- Made `assertContains` and `assertNotContains` work with `SimpleTemplateResponse` by forcing it to be rendered if it hasn't been rendered yet. Thanks to bmihelac for the report, to mmcnickle for the initial patch and tests, and to Carl Meyer for the guidance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17025 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Julien Phalip 2011-10-22 09:15:50 +00:00
parent ac88f048c9
commit 4f00a2d316
2 changed files with 47 additions and 0 deletions

View File

@ -500,6 +500,13 @@ class TransactionTestCase(SimpleTestCase):
If ``count`` is None, the count doesn't matter - the assertion is true If ``count`` is None, the count doesn't matter - the assertion is true
if the text occurs at least once in the response. if the text occurs at least once in the response.
""" """
# If the response supports deferred rendering and hasn't been rendered
# yet, then ensure that it does get rendered before proceeding further.
if (hasattr(response, 'render') and callable(response.render)
and not response.is_rendered):
response.render()
if msg_prefix: if msg_prefix:
msg_prefix += ": " msg_prefix += ": "
@ -523,6 +530,13 @@ class TransactionTestCase(SimpleTestCase):
successfully, (i.e., the HTTP status code was as expected), and that successfully, (i.e., the HTTP status code was as expected), and that
``text`` doesn't occurs in the content of the response. ``text`` doesn't occurs in the content of the response.
""" """
# If the response supports deferred rendering and hasn't been rendered
# yet, then ensure that it does get rendered before proceeding further.
if (hasattr(response, 'render') and callable(response.render)
and not response.is_rendered):
response.render()
if msg_prefix: if msg_prefix:
msg_prefix += ": " msg_prefix += ": "

View File

@ -14,6 +14,8 @@ import django.template.context
from django.test import Client, TestCase from django.test import Client, TestCase
from django.test.client import encode_file, RequestFactory from django.test.client import encode_file, RequestFactory
from django.test.utils import ContextList, override_settings from django.test.utils import ContextList, override_settings
from django.template.response import SimpleTemplateResponse
from django.http import HttpResponse
class AssertContainsTests(TestCase): class AssertContainsTests(TestCase):
@ -130,6 +132,37 @@ class AssertContainsTests(TestCase):
self.assertNotContains(r, u'はたけ') self.assertNotContains(r, u'はたけ')
self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8')) self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
def test_assert_contains_renders_template_response(self):
""" Test that we can pass in an unrendered SimpleTemplateReponse
without throwing an error.
Refs #15826.
"""
response = SimpleTemplateResponse(Template('Hello'), status=200)
self.assertContains(response, 'Hello')
def test_assert_contains_using_non_template_response(self):
""" Test that auto-rendering does not affect responses that aren't
instances (or subclasses) of SimpleTemplateResponse.
Refs #15826.
"""
response = HttpResponse('Hello')
self.assertContains(response, 'Hello')
def test_assert_not_contains_renders_template_response(self):
""" Test that we can pass in an unrendered SimpleTemplateReponse
without throwing an error.
Refs #15826.
"""
response = SimpleTemplateResponse(Template('Hello'), status=200)
self.assertNotContains(response, 'Bye')
def test_assert_not_contains_using_non_template_response(self):
""" Test that auto-rendering does not affect responses that aren't
instances (or subclasses) of SimpleTemplateResponse.
Refs #15826.
"""
response = HttpResponse('Hello')
self.assertNotContains(response, 'Bye')
class AssertTemplateUsedTests(TestCase): class AssertTemplateUsedTests(TestCase):
fixtures = ['testdata.json'] fixtures = ['testdata.json']