2008-05-29 21:11:23 +08:00
|
|
|
"""
|
|
|
|
Test cases for the template loaders
|
2008-06-26 12:22:12 +08:00
|
|
|
|
|
|
|
Note: This test requires setuptools!
|
2008-05-29 21:11:23 +08:00
|
|
|
"""
|
|
|
|
|
2008-07-29 14:05:15 +08:00
|
|
|
import os.path
|
2013-07-01 20:22:27 +08:00
|
|
|
import sys
|
2014-01-19 14:58:22 +08:00
|
|
|
import types
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2008-05-29 21:11:23 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template import Context, TemplateDoesNotExist, loader
|
2014-11-20 06:23:58 +08:00
|
|
|
from django.template.engine import Engine
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template.loaders import cached, eggs
|
2014-12-22 04:19:05 +08:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings, override_settings
|
2013-07-01 20:22:27 +08:00
|
|
|
from django.utils import six
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2012-08-07 21:41:54 +08:00
|
|
|
from django.utils.six import StringIO
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
try:
|
|
|
|
import pkg_resources
|
|
|
|
except ImportError:
|
|
|
|
pkg_resources = None
|
2008-05-29 21:11:23 +08:00
|
|
|
|
2014-12-18 05:10:57 +08:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2008-06-26 12:22:12 +08:00
|
|
|
# Mock classes and objects for pkg_resources functions.
|
|
|
|
class MockLoader(object):
|
|
|
|
pass
|
2008-05-29 21:11:23 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-05-29 21:11:23 +08:00
|
|
|
def create_egg(name, resources):
|
|
|
|
"""
|
2008-06-26 12:22:12 +08:00
|
|
|
Creates a mock egg with a list of resources.
|
|
|
|
|
|
|
|
name: The name of the module.
|
2009-05-18 00:45:28 +08:00
|
|
|
resources: A dictionary of resources. Keys are the names and values the data.
|
2008-05-29 21:11:23 +08:00
|
|
|
"""
|
2014-01-19 14:58:22 +08:00
|
|
|
egg = types.ModuleType(name)
|
2008-05-29 21:11:23 +08:00
|
|
|
egg.__loader__ = MockLoader()
|
2014-01-28 04:28:53 +08:00
|
|
|
egg.__path__ = ['/some/bogus/path/']
|
|
|
|
egg.__file__ = '/some/bogus/path/__init__.pyc'
|
2008-05-29 21:11:23 +08:00
|
|
|
egg._resources = resources
|
|
|
|
sys.modules[name] = egg
|
|
|
|
|
2010-09-13 05:53:35 +08:00
|
|
|
|
2013-07-26 06:17:40 +08:00
|
|
|
@unittest.skipUnless(pkg_resources, 'setuptools is not installed')
|
2014-12-04 04:36:17 +08:00
|
|
|
class EggLoaderTest(SimpleTestCase):
|
2014-12-18 05:10:57 +08:00
|
|
|
|
2008-05-29 21:11:23 +08:00
|
|
|
def setUp(self):
|
2014-12-18 05:10:57 +08:00
|
|
|
self.loader = eggs.Loader(Engine.get_default())
|
|
|
|
|
2013-07-26 06:17:40 +08:00
|
|
|
# Defined here b/c at module scope we may not have pkg_resources
|
|
|
|
class MockProvider(pkg_resources.NullProvider):
|
|
|
|
def __init__(self, module):
|
|
|
|
pkg_resources.NullProvider.__init__(self, module)
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
def _has(self, path):
|
|
|
|
return path in self.module._resources
|
|
|
|
|
|
|
|
def _isdir(self, path):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_resource_stream(self, manager, resource_name):
|
|
|
|
return self.module._resources[resource_name]
|
|
|
|
|
|
|
|
def _get(self, path):
|
|
|
|
return self.module._resources[path].read()
|
|
|
|
|
2014-01-28 04:28:53 +08:00
|
|
|
def _fn(self, base, resource_name):
|
2014-02-05 04:14:41 +08:00
|
|
|
return os.path.normcase(resource_name)
|
2014-01-28 04:28:53 +08:00
|
|
|
|
2008-05-29 21:11:23 +08:00
|
|
|
pkg_resources._provider_factories[MockLoader] = MockProvider
|
2008-06-26 12:22:12 +08:00
|
|
|
|
2008-05-29 21:11:23 +08:00
|
|
|
self.empty_egg = create_egg("egg_empty", {})
|
|
|
|
self.egg_1 = create_egg("egg_1", {
|
2012-09-08 01:17:09 +08:00
|
|
|
os.path.normcase('templates/y.html'): StringIO("y"),
|
|
|
|
os.path.normcase('templates/x.txt'): StringIO("x"),
|
2008-05-29 21:11:23 +08:00
|
|
|
})
|
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['egg_empty'])
|
2008-05-29 21:11:23 +08:00
|
|
|
def test_empty(self):
|
|
|
|
"Loading any template on an empty egg should fail"
|
2014-12-18 05:10:57 +08:00
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.loader.load_template_source("not-existing.html")
|
2008-05-29 21:11:23 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['egg_1'])
|
2008-05-29 21:11:23 +08:00
|
|
|
def test_non_existing(self):
|
|
|
|
"Template loading fails if the template is not in the egg"
|
2014-12-18 05:10:57 +08:00
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.loader.load_template_source("not-existing.html")
|
2008-06-26 12:22:12 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['egg_1'])
|
2008-05-29 21:11:23 +08:00
|
|
|
def test_existing(self):
|
|
|
|
"A template can be loaded from an egg"
|
2014-12-18 05:10:57 +08:00
|
|
|
contents, template_name = self.loader.load_template_source("y.html")
|
2013-12-23 17:37:34 +08:00
|
|
|
self.assertEqual(contents, "y")
|
|
|
|
self.assertEqual(template_name, "egg:egg_1:templates/y.html")
|
2008-06-26 12:22:12 +08:00
|
|
|
|
2008-05-29 21:11:23 +08:00
|
|
|
def test_not_installed(self):
|
2013-12-19 22:57:23 +08:00
|
|
|
"Loading an existent template from an egg not included in any app should fail"
|
2014-12-18 05:10:57 +08:00
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.loader.load_template_source("y.html")
|
2008-05-29 21:11:23 +08:00
|
|
|
|
2013-08-22 02:23:53 +08:00
|
|
|
|
2014-12-04 04:36:17 +08:00
|
|
|
class CachedLoader(SimpleTestCase):
|
2014-12-18 05:10:57 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.loader = cached.Loader(Engine.get_default(), [
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
])
|
|
|
|
|
2010-05-21 16:54:15 +08:00
|
|
|
def test_templatedir_caching(self):
|
|
|
|
"Check that the template directories form part of the template cache key. Refs #13573"
|
2014-03-02 22:25:53 +08:00
|
|
|
# Retrieve a template specifying a template directory to check
|
2014-12-18 05:10:57 +08:00
|
|
|
t1, name = self.loader.find_template('test.html', (os.path.join(TEMPLATES_DIR, 'first'),))
|
2010-05-21 16:54:15 +08:00
|
|
|
# Now retrieve the same template name, but from a different directory
|
2014-12-18 05:10:57 +08:00
|
|
|
t2, name = self.loader.find_template('test.html', (os.path.join(TEMPLATES_DIR, 'second'),))
|
2010-05-21 16:54:15 +08:00
|
|
|
|
|
|
|
# The two templates should not have the same content
|
|
|
|
self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
|
|
|
|
|
2013-08-22 02:23:53 +08:00
|
|
|
def test_missing_template_is_cached(self):
|
2013-08-25 08:26:34 +08:00
|
|
|
"#19949 -- Check that the missing template is cached."
|
|
|
|
# Check that 'missing.html' isn't already in cache before 'missing.html' is loaded
|
2014-12-18 05:10:57 +08:00
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
self.loader.template_cache["missing.html"]
|
2013-08-25 08:26:34 +08:00
|
|
|
# Try to load it, it should fail
|
2014-12-18 05:10:57 +08:00
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.loader.load_template("missing.html")
|
2013-08-25 08:26:34 +08:00
|
|
|
# Verify that the fact that the missing template, which hasn't been found, has actually
|
|
|
|
# been cached:
|
2014-12-18 05:10:57 +08:00
|
|
|
cached_miss = self.loader.template_cache["missing.html"]
|
|
|
|
self.assertEqual(cached_miss, TemplateDoesNotExist,
|
2013-08-25 08:26:34 +08:00
|
|
|
"Cached template loader doesn't cache file lookup misses. It should.")
|
2013-08-22 02:23:53 +08:00
|
|
|
|
|
|
|
|
2014-12-18 05:10:57 +08:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [TEMPLATES_DIR],
|
|
|
|
}])
|
2014-12-04 04:36:17 +08:00
|
|
|
class RenderToStringTest(SimpleTestCase):
|
2011-02-20 12:55:11 +08:00
|
|
|
def test_basic(self):
|
2014-08-13 08:16:04 +08:00
|
|
|
self.assertEqual(loader.render_to_string('test_context.html'), 'obj:\n')
|
2011-02-20 12:55:11 +08:00
|
|
|
|
|
|
|
def test_basic_context(self):
|
|
|
|
self.assertEqual(loader.render_to_string('test_context.html',
|
2014-08-13 08:16:04 +08:00
|
|
|
{'obj': 'test'}), 'obj:test\n')
|
2011-02-20 12:55:11 +08:00
|
|
|
|
2011-09-21 22:20:18 +08:00
|
|
|
def test_empty_list(self):
|
2012-09-08 01:17:09 +08:00
|
|
|
six.assertRaisesRegex(self, TemplateDoesNotExist,
|
2013-11-03 05:34:05 +08:00
|
|
|
'No template names provided$',
|
|
|
|
loader.render_to_string, [])
|
2011-09-21 22:20:18 +08:00
|
|
|
|
|
|
|
def test_select_templates_from_empty_list(self):
|
2012-09-08 01:17:09 +08:00
|
|
|
six.assertRaisesRegex(self, TemplateDoesNotExist,
|
2013-11-03 05:34:05 +08:00
|
|
|
'No template names provided$',
|
|
|
|
loader.select_template, [])
|
2013-09-16 05:51:24 +08:00
|
|
|
|
2014-11-29 06:50:34 +08:00
|
|
|
|
2014-12-22 04:19:05 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2014-12-18 05:10:57 +08:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [TEMPLATES_DIR],
|
|
|
|
}])
|
2014-12-22 04:19:05 +08:00
|
|
|
class DeprecatedRenderToStringTest(SimpleTestCase):
|
2014-11-29 06:50:34 +08:00
|
|
|
|
|
|
|
def test_existing_context_kept_clean(self):
|
|
|
|
context = Context({'obj': 'before'})
|
|
|
|
output = loader.render_to_string('test_context.html', {'obj': 'after'},
|
|
|
|
context_instance=context)
|
|
|
|
self.assertEqual(output, 'obj:after\n')
|
|
|
|
self.assertEqual(context['obj'], 'before')
|
|
|
|
|
2014-02-23 05:28:27 +08:00
|
|
|
def test_no_empty_dict_pushed_to_stack(self):
|
|
|
|
"""
|
|
|
|
No empty dict should be pushed to the context stack when render_to_string
|
|
|
|
is called without any argument (#21741).
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The stack should have a length of 1, corresponding to the builtins
|
|
|
|
self.assertEqual('1',
|
|
|
|
loader.render_to_string('test_context_stack.html').strip())
|
|
|
|
self.assertEqual('1',
|
|
|
|
loader.render_to_string('test_context_stack.html', context_instance=Context()).strip())
|
|
|
|
|
2013-09-16 05:51:24 +08:00
|
|
|
|
2014-12-22 04:19:05 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2014-12-18 05:10:57 +08:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
}])
|
2014-12-22 04:19:05 +08:00
|
|
|
class TemplateDirsOverrideTest(SimpleTestCase):
|
2013-09-16 05:51:24 +08:00
|
|
|
|
|
|
|
dirs_tuple = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
|
|
|
|
dirs_list = list(dirs_tuple)
|
|
|
|
dirs_iter = (dirs_tuple, dirs_list)
|
|
|
|
|
|
|
|
def test_render_to_string(self):
|
|
|
|
for dirs in self.dirs_iter:
|
|
|
|
self.assertEqual(loader.render_to_string('test_dirs.html', dirs=dirs), 'spam eggs\n')
|
|
|
|
|
|
|
|
def test_get_template(self):
|
|
|
|
for dirs in self.dirs_iter:
|
|
|
|
template = loader.get_template('test_dirs.html', dirs=dirs)
|
2015-01-08 22:03:43 +08:00
|
|
|
self.assertEqual(template.render(), 'spam eggs\n')
|
2013-09-16 05:51:24 +08:00
|
|
|
|
|
|
|
def test_select_template(self):
|
|
|
|
for dirs in self.dirs_iter:
|
|
|
|
template = loader.select_template(['test_dirs.html'], dirs=dirs)
|
2015-01-08 22:03:43 +08:00
|
|
|
self.assertEqual(template.render(), 'spam eggs\n')
|
2013-11-20 00:49:16 +08:00
|
|
|
|
|
|
|
|
2014-12-18 05:10:57 +08:00
|
|
|
@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',
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}])
|
2014-12-04 04:36:17 +08:00
|
|
|
class PriorityCacheLoader(SimpleTestCase):
|
2013-11-20 00:49:16 +08:00
|
|
|
def test_basic(self):
|
|
|
|
"""
|
|
|
|
Check that the order of template loader works. Refs #21460.
|
|
|
|
"""
|
2014-11-28 04:39:33 +08:00
|
|
|
t1 = loader.get_template('priority/foo.html')
|
2015-01-08 22:03:43 +08:00
|
|
|
self.assertEqual(t1.render(), 'priority\n')
|
2013-11-20 00:49:16 +08:00
|
|
|
|
|
|
|
|
2014-12-18 05:10:57 +08:00
|
|
|
@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',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}])
|
2014-12-04 04:36:17 +08:00
|
|
|
class PriorityLoader(SimpleTestCase):
|
2013-11-20 00:49:16 +08:00
|
|
|
def test_basic(self):
|
|
|
|
"""
|
|
|
|
Check that the order of template loader works. Refs #21460.
|
|
|
|
"""
|
2014-11-28 04:39:33 +08:00
|
|
|
t1 = loader.get_template('priority/foo.html')
|
2015-01-08 22:03:43 +08:00
|
|
|
self.assertEqual(t1.render(), 'priority\n')
|