2015-02-18 21:51:05 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.template import Context
|
|
|
|
from django.template.engine import Engine
|
2015-09-04 02:01:30 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
from .utils import ROOT, TEMPLATE_DIR
|
|
|
|
|
|
|
|
OTHER_DIR = os.path.join(ROOT, 'other_templates')
|
|
|
|
|
|
|
|
|
2015-09-04 02:01:30 +00:00
|
|
|
class RenderToStringTest(SimpleTestCase):
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2015-09-04 02:01:30 +00:00
|
|
|
self.engine = Engine(dirs=[TEMPLATE_DIR])
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
def test_basic_context(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self.engine.render_to_string('test_context.html', {'obj': 'test'}),
|
|
|
|
'obj:test\n',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LoaderTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_origin(self):
|
|
|
|
engine = Engine(dirs=[TEMPLATE_DIR], debug=True)
|
|
|
|
template = engine.get_template('index.html')
|
2015-03-03 21:48:26 +00:00
|
|
|
self.assertEqual(template.origin.template_name, 'index.html')
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
def test_loader_priority(self):
|
|
|
|
"""
|
|
|
|
#21460 -- Check that the order of template loader works.
|
|
|
|
"""
|
|
|
|
loaders = [
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
'django.template.loaders.app_directories.Loader',
|
|
|
|
]
|
|
|
|
engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
|
|
|
|
template = engine.get_template('priority/foo.html')
|
|
|
|
self.assertEqual(template.render(Context()), 'priority\n')
|
|
|
|
|
|
|
|
def test_cached_loader_priority(self):
|
|
|
|
"""
|
|
|
|
Check that the order of template loader works. Refs #21460.
|
|
|
|
"""
|
|
|
|
loaders = [
|
|
|
|
('django.template.loaders.cached.Loader', [
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
'django.template.loaders.app_directories.Loader',
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
|
|
|
|
|
|
|
|
template = engine.get_template('priority/foo.html')
|
|
|
|
self.assertEqual(template.render(Context()), 'priority\n')
|
|
|
|
|
|
|
|
template = engine.get_template('priority/foo.html')
|
|
|
|
self.assertEqual(template.render(Context()), 'priority\n')
|