diff options
| author | Carl Meyer <carl@oddbird.net> | 2012-01-31 19:23:09 +0000 |
|---|---|---|
| committer | Carl Meyer <carl@oddbird.net> | 2012-01-31 19:23:09 +0000 |
| commit | a678e9ea6589bd95d9d30e86a50d3694ebf60908 (patch) | |
| tree | 9d3506f01167332d8a2f81345f0ae3d9bd932a7f /tests | |
| parent | f1dc83cb9877d349df88674a0752ddf42657485b (diff) | |
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
Diffstat (limited to 'tests')
5 files changed, 89 insertions, 0 deletions
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 --- /dev/null +++ b/tests/regressiontests/test_utils/templates/template_used/alternative.html 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 --- /dev/null +++ b/tests/regressiontests/test_utils/templates/template_used/base.html 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): """ |
