2008-05-29 15:11:23 +02:00
|
|
|
"""
|
|
|
|
Test cases for the template loaders
|
2008-06-26 06:22:12 +02:00
|
|
|
|
|
|
|
Note: This test requires setuptools!
|
2008-05-29 15:11:23 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
settings.configure()
|
|
|
|
|
|
|
|
import imp
|
2008-07-29 08:05:15 +02:00
|
|
|
import os.path
|
2013-07-01 14:22:27 +02:00
|
|
|
import pkg_resources
|
|
|
|
import sys
|
|
|
|
import unittest
|
2008-05-29 15:11:23 +02:00
|
|
|
|
2010-05-21 10:54:15 +02:00
|
|
|
from django.template import TemplateDoesNotExist, Context
|
2010-09-12 23:53:35 +02:00
|
|
|
from django.template.loaders.eggs import Loader as EggLoader
|
2010-05-21 10:54:15 +02:00
|
|
|
from django.template import loader
|
2013-07-01 14:22:27 +02:00
|
|
|
from django.utils import six
|
2012-12-08 11:13:52 +01:00
|
|
|
from django.utils._os import upath
|
2012-08-07 15:41:54 +02:00
|
|
|
from django.utils.six import StringIO
|
2010-10-11 14:55:17 +02:00
|
|
|
|
2008-05-29 15:11:23 +02:00
|
|
|
|
2008-06-26 06:22:12 +02:00
|
|
|
# Mock classes and objects for pkg_resources functions.
|
2008-05-29 15:11:23 +02:00
|
|
|
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
|
|
|
|
|
2012-09-07 19:17:09 +02:00
|
|
|
def _isdir(self, path):
|
2008-05-29 15:11:23 +02:00
|
|
|
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()
|
|
|
|
|
2008-06-26 06:22:12 +02:00
|
|
|
class MockLoader(object):
|
|
|
|
pass
|
2008-05-29 15:11:23 +02:00
|
|
|
|
|
|
|
def create_egg(name, resources):
|
|
|
|
"""
|
2008-06-26 06:22:12 +02:00
|
|
|
Creates a mock egg with a list of resources.
|
|
|
|
|
|
|
|
name: The name of the module.
|
2009-05-17 18:45:28 +02:00
|
|
|
resources: A dictionary of resources. Keys are the names and values the data.
|
2008-05-29 15:11:23 +02:00
|
|
|
"""
|
|
|
|
egg = imp.new_module(name)
|
|
|
|
egg.__loader__ = MockLoader()
|
|
|
|
egg._resources = resources
|
|
|
|
sys.modules[name] = egg
|
|
|
|
|
2010-09-12 23:53:35 +02:00
|
|
|
|
|
|
|
class EggLoaderTest(unittest.TestCase):
|
2008-05-29 15:11:23 +02:00
|
|
|
def setUp(self):
|
|
|
|
pkg_resources._provider_factories[MockLoader] = MockProvider
|
2008-06-26 06:22:12 +02:00
|
|
|
|
2008-05-29 15:11:23 +02:00
|
|
|
self.empty_egg = create_egg("egg_empty", {})
|
|
|
|
self.egg_1 = create_egg("egg_1", {
|
2012-09-07 19:17:09 +02:00
|
|
|
os.path.normcase('templates/y.html'): StringIO("y"),
|
|
|
|
os.path.normcase('templates/x.txt'): StringIO("x"),
|
2008-05-29 15:11:23 +02:00
|
|
|
})
|
2008-05-31 01:24:23 +02:00
|
|
|
self._old_installed_apps = settings.INSTALLED_APPS
|
2008-05-29 15:11:23 +02:00
|
|
|
settings.INSTALLED_APPS = []
|
2008-06-26 06:22:12 +02:00
|
|
|
|
2008-05-31 01:24:23 +02:00
|
|
|
def tearDown(self):
|
|
|
|
settings.INSTALLED_APPS = self._old_installed_apps
|
2008-05-29 15:11:23 +02:00
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
"Loading any template on an empty egg should fail"
|
|
|
|
settings.INSTALLED_APPS = ['egg_empty']
|
2010-09-12 23:53:35 +02:00
|
|
|
egg_loader = EggLoader()
|
|
|
|
self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
|
2008-05-29 15:11:23 +02:00
|
|
|
|
|
|
|
def test_non_existing(self):
|
|
|
|
"Template loading fails if the template is not in the egg"
|
|
|
|
settings.INSTALLED_APPS = ['egg_1']
|
2010-09-12 23:53:35 +02:00
|
|
|
egg_loader = EggLoader()
|
|
|
|
self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
|
2008-06-26 06:22:12 +02:00
|
|
|
|
2008-05-29 15:11:23 +02:00
|
|
|
def test_existing(self):
|
|
|
|
"A template can be loaded from an egg"
|
|
|
|
settings.INSTALLED_APPS = ['egg_1']
|
2010-09-12 23:53:35 +02:00
|
|
|
egg_loader = EggLoader()
|
|
|
|
contents, template_name = egg_loader.load_template_source("y.html")
|
2008-05-29 15:11:23 +02:00
|
|
|
self.assertEqual(contents, "y")
|
|
|
|
self.assertEqual(template_name, "egg:egg_1:templates/y.html")
|
2008-06-26 06:22:12 +02:00
|
|
|
|
2008-05-29 15:11:23 +02:00
|
|
|
def test_not_installed(self):
|
|
|
|
"Loading an existent template from an egg not included in INSTALLED_APPS should fail"
|
|
|
|
settings.INSTALLED_APPS = []
|
2010-09-12 23:53:35 +02:00
|
|
|
egg_loader = EggLoader()
|
|
|
|
self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")
|
2008-05-29 15:11:23 +02:00
|
|
|
|
2010-05-21 10:54:15 +02:00
|
|
|
class CachedLoader(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
|
|
|
|
settings.TEMPLATE_LOADERS = (
|
|
|
|
('django.template.loaders.cached.Loader', (
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def tearDown(self):
|
|
|
|
settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS
|
|
|
|
|
|
|
|
def test_templatedir_caching(self):
|
|
|
|
"Check that the template directories form part of the template cache key. Refs #13573"
|
|
|
|
# Retrive a template specifying a template directory to check
|
2012-12-08 11:13:52 +01:00
|
|
|
t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'first'),))
|
2010-05-21 10:54:15 +02:00
|
|
|
# Now retrieve the same template name, but from a different directory
|
2012-12-08 11:13:52 +01:00
|
|
|
t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'second'),))
|
2010-05-21 10:54:15 +02:00
|
|
|
|
|
|
|
# The two templates should not have the same content
|
|
|
|
self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
|
|
|
|
|
2011-02-20 05:55:11 +01:00
|
|
|
class RenderToStringTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
|
|
|
|
settings.TEMPLATE_DIRS = (
|
2012-12-08 11:13:52 +01:00
|
|
|
os.path.join(os.path.dirname(upath(__file__)), 'templates'),
|
2011-02-20 05:55:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
settings.TEMPLATE_DIRS = self._old_TEMPLATE_DIRS
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
self.assertEqual(loader.render_to_string('test_context.html'), 'obj:')
|
|
|
|
|
|
|
|
def test_basic_context(self):
|
|
|
|
self.assertEqual(loader.render_to_string('test_context.html',
|
|
|
|
{'obj': 'test'}), 'obj:test')
|
|
|
|
|
|
|
|
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')
|
|
|
|
self.assertEqual(context['obj'], 'before')
|
|
|
|
|
2011-09-21 16:20:18 +02:00
|
|
|
def test_empty_list(self):
|
2012-09-07 19:17:09 +02:00
|
|
|
six.assertRaisesRegex(self, TemplateDoesNotExist,
|
2011-09-21 16:20:18 +02:00
|
|
|
'No template names provided$',
|
|
|
|
loader.render_to_string, [])
|
|
|
|
|
|
|
|
|
|
|
|
def test_select_templates_from_empty_list(self):
|
2012-09-07 19:17:09 +02:00
|
|
|
six.assertRaisesRegex(self, TemplateDoesNotExist,
|
2011-09-21 16:20:18 +02:00
|
|
|
'No template names provided$',
|
|
|
|
loader.select_template, [])
|