|
@@ -16,7 +16,7 @@ except ImportError:
|
|
|
|
|
|
|
|
|
from django.template import TemplateDoesNotExist, Context
|
|
|
-from django.template.loaders.eggs import Loader as EggLoader
|
|
|
+from django.template.loaders import cached, eggs
|
|
|
from django.template.engine import Engine
|
|
|
from django.template import loader
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
@@ -26,6 +26,11 @@ from django.utils._os import upath
|
|
|
from django.utils.six import StringIO
|
|
|
|
|
|
|
|
|
+TEMPLATES_DIR = os.path.join(os.path.dirname(upath(__file__)), 'templates')
|
|
|
+
|
|
|
+GLOBAL_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.dirname(upath(__file__))), 'templates')
|
|
|
+
|
|
|
+
|
|
|
# Mock classes and objects for pkg_resources functions.
|
|
|
class MockLoader(object):
|
|
|
pass
|
|
@@ -48,7 +53,10 @@ def create_egg(name, resources):
|
|
|
|
|
|
@unittest.skipUnless(pkg_resources, 'setuptools is not installed')
|
|
|
class EggLoaderTest(SimpleTestCase):
|
|
|
+
|
|
|
def setUp(self):
|
|
|
+ self.loader = eggs.Loader(Engine.get_default())
|
|
|
+
|
|
|
# Defined here b/c at module scope we may not have pkg_resources
|
|
|
class MockProvider(pkg_resources.NullProvider):
|
|
|
def __init__(self, module):
|
|
@@ -81,70 +89,64 @@ class EggLoaderTest(SimpleTestCase):
|
|
|
@override_settings(INSTALLED_APPS=['egg_empty'])
|
|
|
def test_empty(self):
|
|
|
"Loading any template on an empty egg should fail"
|
|
|
- egg_loader = EggLoader(Engine.get_default())
|
|
|
- self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
|
|
|
+ with self.assertRaises(TemplateDoesNotExist):
|
|
|
+ self.loader.load_template_source("not-existing.html")
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=['egg_1'])
|
|
|
def test_non_existing(self):
|
|
|
"Template loading fails if the template is not in the egg"
|
|
|
- egg_loader = EggLoader(Engine.get_default())
|
|
|
- self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
|
|
|
+ with self.assertRaises(TemplateDoesNotExist):
|
|
|
+ self.loader.load_template_source("not-existing.html")
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=['egg_1'])
|
|
|
def test_existing(self):
|
|
|
"A template can be loaded from an egg"
|
|
|
- egg_loader = EggLoader(Engine.get_default())
|
|
|
- contents, template_name = egg_loader.load_template_source("y.html")
|
|
|
+ contents, template_name = self.loader.load_template_source("y.html")
|
|
|
self.assertEqual(contents, "y")
|
|
|
self.assertEqual(template_name, "egg:egg_1:templates/y.html")
|
|
|
|
|
|
def test_not_installed(self):
|
|
|
"Loading an existent template from an egg not included in any app should fail"
|
|
|
- egg_loader = EggLoader(Engine.get_default())
|
|
|
- self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")
|
|
|
+ with self.assertRaises(TemplateDoesNotExist):
|
|
|
+ self.loader.load_template_source("y.html")
|
|
|
|
|
|
|
|
|
-@override_settings(
|
|
|
- TEMPLATE_LOADERS=(
|
|
|
- ('django.template.loaders.cached.Loader', (
|
|
|
- 'django.template.loaders.filesystem.Loader',
|
|
|
- )),
|
|
|
- )
|
|
|
-)
|
|
|
class CachedLoader(SimpleTestCase):
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ self.loader = cached.Loader(Engine.get_default(), [
|
|
|
+ 'django.template.loaders.filesystem.Loader',
|
|
|
+ ])
|
|
|
+
|
|
|
def test_templatedir_caching(self):
|
|
|
"Check that the template directories form part of the template cache key. Refs #13573"
|
|
|
- template_loader = Engine.get_default().template_loaders[0]
|
|
|
-
|
|
|
# Retrieve a template specifying a template directory to check
|
|
|
- t1, name = template_loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'first'),))
|
|
|
+ t1, name = self.loader.find_template('test.html', (os.path.join(TEMPLATES_DIR, 'first'),))
|
|
|
# Now retrieve the same template name, but from a different directory
|
|
|
- t2, name = template_loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'second'),))
|
|
|
+ t2, name = self.loader.find_template('test.html', (os.path.join(TEMPLATES_DIR, 'second'),))
|
|
|
|
|
|
# The two templates should not have the same content
|
|
|
self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
|
|
|
|
|
|
def test_missing_template_is_cached(self):
|
|
|
"#19949 -- Check that the missing template is cached."
|
|
|
- template_loader = Engine.get_default().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
|
|
|
- self.assertRaises(KeyError, lambda: template_loader.template_cache["missing.html"])
|
|
|
+ with self.assertRaises(KeyError):
|
|
|
+ self.loader.template_cache["missing.html"]
|
|
|
# Try to load it, it should fail
|
|
|
- self.assertRaises(TemplateDoesNotExist, template_loader.load_template, "missing.html")
|
|
|
+ with self.assertRaises(TemplateDoesNotExist):
|
|
|
+ self.loader.load_template("missing.html")
|
|
|
# Verify that the fact that the missing template, which hasn't been found, has actually
|
|
|
# been cached:
|
|
|
- self.assertEqual(template_loader.template_cache.get("missing.html"),
|
|
|
- TemplateDoesNotExist,
|
|
|
+ cached_miss = self.loader.template_cache["missing.html"]
|
|
|
+ self.assertEqual(cached_miss, TemplateDoesNotExist,
|
|
|
"Cached template loader doesn't cache file lookup misses. It should.")
|
|
|
|
|
|
|
|
|
-@override_settings(
|
|
|
- TEMPLATE_DIRS=(
|
|
|
- os.path.join(os.path.dirname(upath(__file__)), 'templates'),
|
|
|
- )
|
|
|
-)
|
|
|
+@override_settings(TEMPLATES=[{
|
|
|
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
+ 'DIRS': [TEMPLATES_DIR],
|
|
|
+}])
|
|
|
class RenderToStringTest(SimpleTestCase):
|
|
|
def test_basic(self):
|
|
|
self.assertEqual(loader.render_to_string('test_context.html'), 'obj:\n')
|
|
@@ -164,11 +166,10 @@ class RenderToStringTest(SimpleTestCase):
|
|
|
loader.select_template, [])
|
|
|
|
|
|
|
|
|
-@override_settings(
|
|
|
- TEMPLATE_DIRS=(
|
|
|
- os.path.join(os.path.dirname(upath(__file__)), 'templates'),
|
|
|
- )
|
|
|
-)
|
|
|
+@override_settings(TEMPLATES=[{
|
|
|
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
+ 'DIRS': [TEMPLATES_DIR],
|
|
|
+}])
|
|
|
class DeprecatedRenderToStringTest(IgnorePendingDeprecationWarningsMixin, SimpleTestCase):
|
|
|
|
|
|
def test_existing_context_kept_clean(self):
|
|
@@ -191,7 +192,10 @@ class DeprecatedRenderToStringTest(IgnorePendingDeprecationWarningsMixin, Simple
|
|
|
loader.render_to_string('test_context_stack.html', context_instance=Context()).strip())
|
|
|
|
|
|
|
|
|
-class TemplateDirsOverrideTest(IgnorePendingDeprecationWarningsMixin, unittest.TestCase):
|
|
|
+@override_settings(TEMPLATES=[{
|
|
|
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
+}])
|
|
|
+class TemplateDirsOverrideTest(IgnorePendingDeprecationWarningsMixin, SimpleTestCase):
|
|
|
|
|
|
dirs_tuple = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
|
|
|
dirs_list = list(dirs_tuple)
|
|
@@ -212,14 +216,18 @@ class TemplateDirsOverrideTest(IgnorePendingDeprecationWarningsMixin, unittest.T
|
|
|
self.assertEqual(template.render(Context({})), 'spam eggs\n')
|
|
|
|
|
|
|
|
|
-@override_settings(
|
|
|
- TEMPLATE_LOADERS=(
|
|
|
- ('django.template.loaders.cached.Loader', (
|
|
|
- 'django.template.loaders.filesystem.Loader',
|
|
|
- 'django.template.loaders.app_directories.Loader',
|
|
|
- )),
|
|
|
- )
|
|
|
-)
|
|
|
+@override_settings(TEMPLATES=[{
|
|
|
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
+ 'DIRS': [GLOBAL_TEMPLATES_DIR],
|
|
|
+ 'OPTIONS': {
|
|
|
+ 'loaders': [
|
|
|
+ ('django.template.loaders.cached.Loader', [
|
|
|
+ 'django.template.loaders.filesystem.Loader',
|
|
|
+ 'django.template.loaders.app_directories.Loader',
|
|
|
+ ]),
|
|
|
+ ],
|
|
|
+ },
|
|
|
+}])
|
|
|
class PriorityCacheLoader(SimpleTestCase):
|
|
|
def test_basic(self):
|
|
|
"""
|
|
@@ -229,10 +237,16 @@ class PriorityCacheLoader(SimpleTestCase):
|
|
|
self.assertEqual(t1.render(Context({})), 'priority\n')
|
|
|
|
|
|
|
|
|
-@override_settings(
|
|
|
- TEMPLATE_LOADERS=('django.template.loaders.filesystem.Loader',
|
|
|
- 'django.template.loaders.app_directories.Loader',),
|
|
|
-)
|
|
|
+@override_settings(TEMPLATES=[{
|
|
|
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
+ 'DIRS': [GLOBAL_TEMPLATES_DIR],
|
|
|
+ 'OPTIONS': {
|
|
|
+ 'loaders': [
|
|
|
+ 'django.template.loaders.filesystem.Loader',
|
|
|
+ 'django.template.loaders.app_directories.Loader',
|
|
|
+ ],
|
|
|
+ },
|
|
|
+}])
|
|
|
class PriorityLoader(SimpleTestCase):
|
|
|
def test_basic(self):
|
|
|
"""
|