diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-11-15 20:58:26 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-11-16 21:41:44 +0100 |
| commit | 9eeb788cfb70d07d87b3d07434d6a149ab2d7471 (patch) | |
| tree | 10045a7dde41390e86c4e9c471f6955b69a8ce09 /tests | |
| parent | 1851dcf377d05a5d167cf482674e4c4a4e2502a5 (diff) | |
Refactored getting the list of template loaders.
This provides the opportunity to move utility functions specific to the
Django Template Language outside of django.template.loader.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/template_tests/test_loaders.py | 3 | ||||
| -rw-r--r-- | tests/template_tests/tests.py | 41 | ||||
| -rw-r--r-- | tests/test_client_regress/tests.py | 13 |
3 files changed, 30 insertions, 27 deletions
diff --git a/tests/template_tests/test_loaders.py b/tests/template_tests/test_loaders.py index e83b8060b1..c3c086f381 100644 --- a/tests/template_tests/test_loaders.py +++ b/tests/template_tests/test_loaders.py @@ -22,6 +22,7 @@ except ImportError: from django.template import TemplateDoesNotExist, Context from django.template.loaders.eggs import Loader as EggLoader +from django.template.loaders.utils import find_template_loader from django.template import loader from django.test import TestCase, override_settings from django.utils import six @@ -127,7 +128,7 @@ class CachedLoader(TestCase): def test_missing_template_is_cached(self): "#19949 -- Check that the missing template is cached." - template_loader = loader.find_template_loader(settings.TEMPLATE_LOADERS[0]) + template_loader = find_template_loader(settings.TEMPLATE_LOADERS[0]) # Empty cache, which may be filled from previous tests. template_loader.reset() # Check that 'missing.html' isn't already in cache before 'missing.html' is loaded diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 334dd31f7b..b8097fbde1 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -14,7 +14,8 @@ from django.contrib.auth.models import Group from django.core import urlresolvers from django.template import (base as template_base, loader, Context, RequestContext, Template, TemplateSyntaxError) -from django.template.loaders import app_directories, filesystem, cached +from django.template.loaders import app_directories, filesystem +from django.template.loaders.utils import get_template_loaders from django.test import RequestFactory, TestCase from django.test.utils import override_settings, extend_sys_path from django.utils.deprecation import RemovedInDjango19Warning, RemovedInDjango20Warning @@ -218,21 +219,27 @@ class TemplateLoaderTests(TestCase): @override_settings(TEMPLATE_DEBUG=True) def test_loader_debug_origin(self): # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to - # point to a directory containing a login.html file. Also that - # the file system and app directories loaders both inherit the - # load_template method from the base Loader class, so we only need - # to test one of them. + # point to a directory containing a login.html file. load_name = 'login.html' + + # We also rely on the fact the file system and app directories loaders + # both inherit the load_template method from the base Loader class, so + # we only need to test one of them. template = loader.get_template(load_name) template_name = template.nodelist[0].source[0].name self.assertTrue(template_name.endswith(load_name), 'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name) - # Also test the cached loader, since it overrides load_template - cache_loader = cached.Loader(('',)) - cache_loader._cached_loaders = loader.template_source_loaders - loader.template_source_loaders = (cache_loader,) + @override_settings(TEMPLATE_LOADERS=[ + ('django.template.loaders.cached.Loader', + ['django.template.loaders.filesystem.Loader']), + ]) + @override_settings(TEMPLATE_DEBUG=True) + def test_cached_loader_debug_origin(self): + # Same comment as in test_loader_debug_origin. + load_name = 'login.html' + # Test the cached loader separately since it overrides load_template. template = loader.get_template(load_name) template_name = template.nodelist[0].source[0].name self.assertTrue(template_name.endswith(load_name), @@ -243,15 +250,15 @@ class TemplateLoaderTests(TestCase): self.assertTrue(template_name.endswith(load_name), 'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name) + @override_settings(TEMPLATE_DEBUG=True) def test_loader_origin(self): - with self.settings(TEMPLATE_DEBUG=True): - template = loader.get_template('login.html') - self.assertEqual(template.origin.loadname, 'login.html') + template = loader.get_template('login.html') + self.assertEqual(template.origin.loadname, 'login.html') + @override_settings(TEMPLATE_DEBUG=True) def test_string_origin(self): - with self.settings(TEMPLATE_DEBUG=True): - template = Template('string template') - self.assertEqual(template.origin.source, 'string template') + template = Template('string template') + self.assertEqual(template.origin.source, 'string template') def test_debug_false_origin(self): template = loader.get_template('login.html') @@ -613,7 +620,9 @@ class TemplateTests(TestCase): if output != result: failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, template_debug, name, result, output)) - loader.template_source_loaders[0].reset() + # This relies on get_template_loaders() memoizing its + # result. All callers get the same iterable of loaders. + get_template_loaders()[0].reset() if template_base.invalid_var_format_string: expected_invalid_str = 'INVALID' diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 9ceba4992f..f7dcae15cf 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -8,8 +8,7 @@ import os import itertools from django.core.urlresolvers import reverse, NoReverseMatch -from django.template import (TemplateSyntaxError, - Context, Template, loader) +from django.template import TemplateSyntaxError, Context, Template import django.template.context from django.test import Client, TestCase, override_settings from django.test.client import encode_file, RequestFactory @@ -902,13 +901,6 @@ class ExceptionTests(TestCase): @override_settings(ROOT_URLCONF='test_client_regress.urls') class TemplateExceptionTests(TestCase): - def setUp(self): - # Reset the loaders so they don't try to render cached templates. - if loader.template_source_loaders is not None: - for template_loader in loader.template_source_loaders: - if hasattr(template_loader, 'reset'): - template_loader.reset() - @override_settings( TEMPLATE_DIRS=(os.path.join(os.path.dirname(upath(__file__)), 'bad_templates'),) ) @@ -916,9 +908,10 @@ class TemplateExceptionTests(TestCase): "Errors found when rendering 404 error templates are re-raised" try: self.client.get("/no_such_view/") - self.fail("Should get error about syntax error in template") except TemplateSyntaxError: pass + else: + self.fail("Should get error about syntax error in template") # We need two different tests to check URLconf substitution - one to check |
