From a678e9ea6589bd95d9d30e86a50d3694ebf60908 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Tue, 31 Jan 2012 19:23:09 +0000 Subject: Fixed #17604 - Added context-manager capability to assertTemplateUsed and assertTemplateNotUsed. Thanks Greg Müllegger. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@17412 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../templates/template_used/alternative.html | 0 .../test_utils/templates/template_used/base.html | 0 .../templates/template_used/extends.html | 1 + .../templates/template_used/include.html | 1 + tests/regressiontests/test_utils/tests.py | 87 ++++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 tests/regressiontests/test_utils/templates/template_used/alternative.html create mode 100644 tests/regressiontests/test_utils/templates/template_used/base.html create mode 100644 tests/regressiontests/test_utils/templates/template_used/extends.html create mode 100644 tests/regressiontests/test_utils/templates/template_used/include.html (limited to 'tests/regressiontests/test_utils') diff --git a/tests/regressiontests/test_utils/templates/template_used/alternative.html b/tests/regressiontests/test_utils/templates/template_used/alternative.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/test_utils/templates/template_used/base.html b/tests/regressiontests/test_utils/templates/template_used/base.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/test_utils/templates/template_used/extends.html b/tests/regressiontests/test_utils/templates/template_used/extends.html new file mode 100644 index 0000000000..d14bfa2779 --- /dev/null +++ b/tests/regressiontests/test_utils/templates/template_used/extends.html @@ -0,0 +1 @@ +{% extends "template_used/base.html" %} diff --git a/tests/regressiontests/test_utils/templates/template_used/include.html b/tests/regressiontests/test_utils/templates/template_used/include.html new file mode 100644 index 0000000000..2d6c954f38 --- /dev/null +++ b/tests/regressiontests/test_utils/templates/template_used/include.html @@ -0,0 +1 @@ +{% include "template_used/base.html" %} diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py index eab6895f0d..b578bffc1a 100644 --- a/tests/regressiontests/test_utils/tests.py +++ b/tests/regressiontests/test_utils/tests.py @@ -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): """ -- cgit v1.3