From 17c18844561431aabed89c3bd48de951db7d13ab Mon Sep 17 00:00:00 2001 From: "Jacob R. Rothenbuhler" Date: Mon, 14 Apr 2014 15:13:49 -0400 Subject: Fixed #22369 -- Added count parameter to assertTemplateUsed --- tests/test_client_regress/tests.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_client_regress/urls.py | 1 + tests/test_client_regress/views.py | 7 +++++++ 3 files changed, 42 insertions(+) (limited to 'tests/test_client_regress') diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 59cc46b567..da903dfcfd 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -213,6 +213,12 @@ class AssertTemplateUsedTests(TestCase): except AssertionError as e: self.assertIn("abc: No templates used to render the response", str(e)) + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed(response, 'GET Template', count=2) + self.assertIn( + "No templates used to render the response", + str(context.exception)) + def test_single_context(self): "Template assertions work when there is a single context" response = self.client.get('/post_view/', {}) @@ -237,6 +243,21 @@ class AssertTemplateUsedTests(TestCase): except AssertionError as e: self.assertIn("abc: Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template", str(e)) + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed(response, 'Empty GET Template', count=2) + self.assertIn( + "Template 'Empty GET Template' was expected to be rendered 2 " + "time(s) but was actually rendered 1 time(s).", + str(context.exception)) + + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed( + response, 'Empty GET Template', msg_prefix='abc', count=2) + self.assertIn( + "abc: Template 'Empty GET Template' was expected to be rendered 2 " + "time(s) but was actually rendered 1 time(s).", + str(context.exception)) + def test_multiple_context(self): "Template assertions work when there are multiple contexts" post_data = { @@ -263,6 +284,19 @@ class AssertTemplateUsedTests(TestCase): except AssertionError as e: self.assertIn("Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html", str(e)) + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed(response, 'base.html', count=2) + self.assertIn( + "Template 'base.html' was expected to be rendered 2 " + "time(s) but was actually rendered 1 time(s).", + str(context.exception)) + + def test_template_rendered_multiple_times(self): + """Template assertions work when a template is rendered multiple times.""" + response = self.client.get('/render_template_multiple_times/') + + self.assertTemplateUsed(response, 'base.html', count=2) + @override_settings(ROOT_URLCONF='test_client_regress.urls') class AssertRedirectsTests(TestCase): diff --git a/tests/test_client_regress/urls.py b/tests/test_client_regress/urls.py index 44bbebfcd8..1bd0e0bc5b 100644 --- a/tests/test_client_regress/urls.py +++ b/tests/test_client_regress/urls.py @@ -37,4 +37,5 @@ urlpatterns = [ url(r'^read_all/$', views.read_all), url(r'^read_buffer/$', views.read_buffer), url(r'^request_context_view/$', views.request_context_view), + url(r'^render_template_multiple_times/$', views.render_template_multiple_times), ] diff --git a/tests/test_client_regress/views.py b/tests/test_client_regress/views.py index db5de3bf87..656d7a4651 100644 --- a/tests/test_client_regress/views.py +++ b/tests/test_client_regress/views.py @@ -7,6 +7,7 @@ from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response from django.core.serializers.json import DjangoJSONEncoder from django.template import RequestContext +from django.template.loader import render_to_string from django.test import Client from django.test.client import CONTENT_TYPE_RE from django.test.utils import setup_test_environment @@ -151,3 +152,9 @@ def request_context_view(request): # Special attribute that won't be present on a plain HttpRequest request.special_path = request.path return render_to_response('request_context.html', context_instance=RequestContext(request, {})) + + +def render_template_multiple_times(request): + """A view that renders a template multiple times.""" + return HttpResponse( + render_to_string('base.html') + render_to_string('base.html')) -- cgit v1.2.1