1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #17604 - Added context-manager capability to assertTemplateUsed and assertTemplateNotUsed. Thanks Greg Müllegger.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17412 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Carl Meyer
2012-01-31 19:23:09 +00:00
parent f1dc83cb98
commit a678e9ea65
8 changed files with 198 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import with_statement, absolute_import
from django.forms import EmailField, IntegerField
from django.template.loader import render_to_string
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
from django.utils.unittest import skip
@@ -88,6 +89,92 @@ class AssertNumQueriesContextManagerTests(TestCase):
self.client.get("/test_utils/get_person/%s/" % person.pk)
class AssertTemplateUsedContextManagerTests(TestCase):
def test_usage(self):
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/base.html')
with self.assertTemplateUsed(template_name='template_used/base.html'):
render_to_string('template_used/base.html')
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/include.html')
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/extends.html')
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/base.html')
render_to_string('template_used/base.html')
def test_nested_usage(self):
with self.assertTemplateUsed('template_used/base.html'):
with self.assertTemplateUsed('template_used/include.html'):
render_to_string('template_used/include.html')
with self.assertTemplateUsed('template_used/extends.html'):
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/extends.html')
with self.assertTemplateUsed('template_used/base.html'):
with self.assertTemplateUsed('template_used/alternative.html'):
render_to_string('template_used/alternative.html')
render_to_string('template_used/base.html')
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/extends.html')
with self.assertTemplateNotUsed('template_used/base.html'):
render_to_string('template_used/alternative.html')
render_to_string('template_used/base.html')
def test_not_used(self):
with self.assertTemplateNotUsed('template_used/base.html'):
pass
with self.assertTemplateNotUsed('template_used/alternative.html'):
pass
def test_error_message(self):
try:
with self.assertTemplateUsed('template_used/base.html'):
pass
except AssertionError, e:
self.assertTrue('template_used/base.html' in e.message)
try:
with self.assertTemplateUsed(template_name='template_used/base.html'):
pass
except AssertionError, e:
self.assertTrue('template_used/base.html' in e.message)
try:
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/alternative.html')
except AssertionError, e:
self.assertTrue('template_used/base.html' in e.message, e.message)
self.assertTrue('template_used/alternative.html' in e.message, e.message)
def test_failure(self):
with self.assertRaises(TypeError):
with self.assertTemplateUsed():
pass
with self.assertRaises(AssertionError):
with self.assertTemplateUsed(''):
pass
with self.assertRaises(AssertionError):
with self.assertTemplateUsed(''):
render_to_string('template_used/base.html')
with self.assertRaises(AssertionError):
with self.assertTemplateUsed(template_name=''):
pass
with self.assertRaises(AssertionError):
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/alternative.html')
class SaveRestoreWarningState(TestCase):
def test_save_restore_warnings_state(self):
"""