summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPreston Timmons <prestontimmons@gmail.com>2015-03-03 15:48:26 -0600
committerPreston Timmons <prestontimmons@gmail.com>2015-04-22 14:49:00 -0500
commitfc2147152637e21bc73f991b50fa06254af02739 (patch)
treec9f80c73aabf680e67c6646361d8de3a29a62f93 /tests
parent1b1b58bc7b2917b203ee3eb3b9e8b3a1fafaee64 (diff)
Fixed #15053 -- Enabled recursive template loading.
Diffstat (limited to 'tests')
-rw-r--r--tests/template_loader/tests.py32
-rw-r--r--tests/template_tests/recursive_templates/fs/extend-missing.html1
-rw-r--r--tests/template_tests/recursive_templates/fs/one.html3
-rw-r--r--tests/template_tests/recursive_templates/fs/other-recursive.html1
-rw-r--r--tests/template_tests/recursive_templates/fs/recursive.html3
-rw-r--r--tests/template_tests/recursive_templates/fs/self.html1
-rw-r--r--tests/template_tests/recursive_templates/fs/three.html1
-rw-r--r--tests/template_tests/recursive_templates/fs/two.html3
-rw-r--r--tests/template_tests/recursive_templates/fs2/recursive.html3
-rw-r--r--tests/template_tests/recursive_templates/fs3/recursive.html1
-rw-r--r--tests/template_tests/test_engine.py2
-rw-r--r--tests/template_tests/test_extends.py178
-rw-r--r--tests/template_tests/test_loaders.py178
-rw-r--r--tests/template_tests/tests.py5
14 files changed, 372 insertions, 40 deletions
diff --git a/tests/template_loader/tests.py b/tests/template_loader/tests.py
index a4a4542f32..c698c0a398 100644
--- a/tests/template_loader/tests.py
+++ b/tests/template_loader/tests.py
@@ -33,8 +33,12 @@ class TemplateLoaderTests(SimpleTestCase):
self.assertEqual(template.render(), "Hello! (Django templates)\n")
def test_get_template_not_found(self):
- with self.assertRaises(TemplateDoesNotExist):
+ with self.assertRaises(TemplateDoesNotExist) as e:
get_template("template_loader/unknown.html")
+ self.assertEqual(
+ e.exception.tried[-1][0].template_name,
+ 'template_loader/unknown.html',
+ )
def test_select_template_first_engine(self):
template = select_template(["template_loader/unknown.html",
@@ -56,9 +60,17 @@ class TemplateLoaderTests(SimpleTestCase):
select_template([])
def test_select_template_not_found(self):
- with self.assertRaises(TemplateDoesNotExist):
+ with self.assertRaises(TemplateDoesNotExist) as e:
select_template(["template_loader/unknown.html",
"template_loader/missing.html"])
+ self.assertEqual(
+ e.exception.tried[0][0].template_name,
+ 'template_loader/unknown.html',
+ )
+ self.assertEqual(
+ e.exception.tried[-1][0].template_name,
+ 'template_loader/missing.html',
+ )
def test_select_template_tries_all_engines_before_names(self):
template = select_template(["template_loader/goodbye.html",
@@ -83,8 +95,12 @@ class TemplateLoaderTests(SimpleTestCase):
self.assertEqual(content, "Hello! (Django templates)\n")
def test_render_to_string_not_found(self):
- with self.assertRaises(TemplateDoesNotExist):
+ with self.assertRaises(TemplateDoesNotExist) as e:
render_to_string("template_loader/unknown.html")
+ self.assertEqual(
+ e.exception.tried[-1][0].template_name,
+ 'template_loader/unknown.html',
+ )
def test_render_to_string_with_list_first_engine(self):
content = render_to_string(["template_loader/unknown.html",
@@ -106,9 +122,17 @@ class TemplateLoaderTests(SimpleTestCase):
render_to_string([])
def test_render_to_string_with_list_not_found(self):
- with self.assertRaises(TemplateDoesNotExist):
+ with self.assertRaises(TemplateDoesNotExist) as e:
render_to_string(["template_loader/unknown.html",
"template_loader/missing.html"])
+ self.assertEqual(
+ e.exception.tried[0][0].template_name,
+ 'template_loader/unknown.html',
+ )
+ self.assertEqual(
+ e.exception.tried[-1][0].template_name,
+ 'template_loader/missing.html',
+ )
def test_render_to_string_with_list_tries_all_engines_before_names(self):
content = render_to_string(["template_loader/goodbye.html",
diff --git a/tests/template_tests/recursive_templates/fs/extend-missing.html b/tests/template_tests/recursive_templates/fs/extend-missing.html
new file mode 100644
index 0000000000..e3c106e2c1
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/extend-missing.html
@@ -0,0 +1 @@
+{% extends "missing.html" %}
diff --git a/tests/template_tests/recursive_templates/fs/one.html b/tests/template_tests/recursive_templates/fs/one.html
new file mode 100644
index 0000000000..f72e72e1a6
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/one.html
@@ -0,0 +1,3 @@
+{% extends "two.html" %}
+
+{% block content %}{{ block.super }} one{% endblock %}
diff --git a/tests/template_tests/recursive_templates/fs/other-recursive.html b/tests/template_tests/recursive_templates/fs/other-recursive.html
new file mode 100644
index 0000000000..84e5ac9eae
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/other-recursive.html
@@ -0,0 +1 @@
+{% extends "recursive.html" %}
diff --git a/tests/template_tests/recursive_templates/fs/recursive.html b/tests/template_tests/recursive_templates/fs/recursive.html
new file mode 100644
index 0000000000..cbf242d8d5
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/recursive.html
@@ -0,0 +1,3 @@
+{% extends "recursive.html" %}
+
+{% block content %}{{ block.super }} fs/recursive{% endblock %}
diff --git a/tests/template_tests/recursive_templates/fs/self.html b/tests/template_tests/recursive_templates/fs/self.html
new file mode 100644
index 0000000000..f3e5bbf301
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/self.html
@@ -0,0 +1 @@
+{% extends "self.html" %}
diff --git a/tests/template_tests/recursive_templates/fs/three.html b/tests/template_tests/recursive_templates/fs/three.html
new file mode 100644
index 0000000000..360aeeea5e
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/three.html
@@ -0,0 +1 @@
+{% block content %}three{% endblock %}
diff --git a/tests/template_tests/recursive_templates/fs/two.html b/tests/template_tests/recursive_templates/fs/two.html
new file mode 100644
index 0000000000..b9b80ec7a0
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs/two.html
@@ -0,0 +1,3 @@
+{% extends "three.html" %}
+
+{% block content %}{{ block.super }} two{% endblock %}
diff --git a/tests/template_tests/recursive_templates/fs2/recursive.html b/tests/template_tests/recursive_templates/fs2/recursive.html
new file mode 100644
index 0000000000..52a338ca9f
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs2/recursive.html
@@ -0,0 +1,3 @@
+{% extends "recursive.html" %}
+
+{% block content %}{{ block.super }} fs2/recursive{% endblock %}
diff --git a/tests/template_tests/recursive_templates/fs3/recursive.html b/tests/template_tests/recursive_templates/fs3/recursive.html
new file mode 100644
index 0000000000..aefbad4582
--- /dev/null
+++ b/tests/template_tests/recursive_templates/fs3/recursive.html
@@ -0,0 +1 @@
+{% block content %}fs3/recursive{% endblock %}
diff --git a/tests/template_tests/test_engine.py b/tests/template_tests/test_engine.py
index 6b43fd9dd2..d9929660cc 100644
--- a/tests/template_tests/test_engine.py
+++ b/tests/template_tests/test_engine.py
@@ -55,7 +55,7 @@ class LoaderTests(SimpleTestCase):
def test_origin(self):
engine = Engine(dirs=[TEMPLATE_DIR], debug=True)
template = engine.get_template('index.html')
- self.assertEqual(template.origin.loadname, 'index.html')
+ self.assertEqual(template.origin.template_name, 'index.html')
def test_loader_priority(self):
"""
diff --git a/tests/template_tests/test_extends.py b/tests/template_tests/test_extends.py
new file mode 100644
index 0000000000..48290547e7
--- /dev/null
+++ b/tests/template_tests/test_extends.py
@@ -0,0 +1,178 @@
+import os
+
+from django.template import Context, Engine, TemplateDoesNotExist
+from django.template.loader_tags import ExtendsError
+from django.template.loaders.base import Loader
+from django.test import SimpleTestCase, ignore_warnings
+from django.utils.deprecation import RemovedInDjango21Warning
+
+from .utils import ROOT
+
+RECURSIVE = os.path.join(ROOT, 'recursive_templates')
+
+
+class ExtendsBehaviorTests(SimpleTestCase):
+
+ def test_normal_extend(self):
+ engine = Engine(dirs=[os.path.join(RECURSIVE, 'fs')])
+ template = engine.get_template('one.html')
+ output = template.render(Context({}))
+ self.assertEqual(output.strip(), 'three two one')
+
+ def test_extend_recursive(self):
+ engine = Engine(dirs=[
+ os.path.join(RECURSIVE, 'fs'),
+ os.path.join(RECURSIVE, 'fs2'),
+ os.path.join(RECURSIVE, 'fs3'),
+ ])
+ template = engine.get_template('recursive.html')
+ output = template.render(Context({}))
+ self.assertEqual(output.strip(), 'fs3/recursive fs2/recursive fs/recursive')
+
+ def test_extend_missing(self):
+ engine = Engine(dirs=[os.path.join(RECURSIVE, 'fs')])
+ template = engine.get_template('extend-missing.html')
+ with self.assertRaises(TemplateDoesNotExist) as e:
+ template.render(Context({}))
+
+ tried = e.exception.tried
+ self.assertEqual(len(tried), 1)
+ self.assertEqual(tried[0][0].template_name, 'missing.html')
+
+ def test_recursive_multiple_loaders(self):
+ engine = Engine(
+ dirs=[os.path.join(RECURSIVE, 'fs')],
+ loaders=[
+ ('django.template.loaders.locmem.Loader', {
+ 'one.html': '{% extends "one.html" %}{% block content %}{{ block.super }} locmem-one{% endblock %}',
+ 'two.html': '{% extends "two.html" %}{% block content %}{{ block.super }} locmem-two{% endblock %}',
+ 'three.html': (
+ '{% extends "three.html" %}{% block content %}{{ block.super }} locmem-three{% endblock %}'
+ ),
+ }),
+ 'django.template.loaders.filesystem.Loader',
+ ],
+ )
+ template = engine.get_template('one.html')
+ output = template.render(Context({}))
+ self.assertEqual(output.strip(), 'three locmem-three two locmem-two one locmem-one')
+
+ def test_extend_self_error(self):
+ """
+ Catch if a template extends itself and no other matching
+ templates are found.
+ """
+ engine = Engine(dirs=[os.path.join(RECURSIVE, 'fs')])
+ template = engine.get_template('self.html')
+ with self.assertRaises(TemplateDoesNotExist):
+ template.render(Context({}))
+
+ def test_extend_cached(self):
+ engine = Engine(
+ dirs=[
+ os.path.join(RECURSIVE, 'fs'),
+ os.path.join(RECURSIVE, 'fs2'),
+ os.path.join(RECURSIVE, 'fs3'),
+ ],
+ loaders=[
+ ('django.template.loaders.cached.Loader', [
+ 'django.template.loaders.filesystem.Loader',
+ ]),
+ ],
+ )
+ template = engine.get_template('recursive.html')
+ output = template.render(Context({}))
+ self.assertEqual(output.strip(), 'fs3/recursive fs2/recursive fs/recursive')
+
+ cache = engine.template_loaders[0].get_template_cache
+ self.assertEqual(len(cache), 3)
+ self.assertTrue(cache['recursive.html'].origin.name.endswith('fs/recursive.html'))
+
+ # Render another path that uses the same templates from the cache
+ template = engine.get_template('other-recursive.html')
+ output = template.render(Context({}))
+ self.assertEqual(output.strip(), 'fs3/recursive fs2/recursive fs/recursive')
+
+ # Template objects should not be duplicated.
+ self.assertEqual(len(cache), 4)
+ self.assertTrue(cache['other-recursive.html'].origin.name.endswith('fs/other-recursive.html'))
+
+ def test_unique_history_per_loader(self):
+ """
+ Extending should continue even if two loaders return the same
+ name for a template.
+ """
+ engine = Engine(
+ loaders=[
+ ['django.template.loaders.locmem.Loader', {
+ 'base.html': '{% extends "base.html" %}{% block content %}{{ block.super }} loader1{% endblock %}',
+ }],
+ ['django.template.loaders.locmem.Loader', {
+ 'base.html': '{% block content %}loader2{% endblock %}',
+ }],
+ ]
+ )
+ template = engine.get_template('base.html')
+ output = template.render(Context({}))
+ self.assertEqual(output.strip(), 'loader2 loader1')
+
+
+class NonRecursiveLoader(Loader):
+
+ def __init__(self, engine, templates_dict):
+ self.templates_dict = templates_dict
+ super(NonRecursiveLoader, self).__init__(engine)
+
+ def load_template_source(self, template_name, template_dirs=None):
+ try:
+ return self.templates_dict[template_name], template_name
+ except KeyError:
+ raise TemplateDoesNotExist(template_name)
+
+
+@ignore_warnings(category=RemovedInDjango21Warning)
+class NonRecursiveLoaderExtendsTests(SimpleTestCase):
+
+ loaders = [
+ ('template_tests.test_extends.NonRecursiveLoader', {
+ 'base.html': 'base',
+ 'index.html': '{% extends "base.html" %}',
+ 'recursive.html': '{% extends "recursive.html" %}',
+ 'other-recursive.html': '{% extends "recursive.html" %}',
+ 'a.html': '{% extends "b.html" %}',
+ 'b.html': '{% extends "a.html" %}',
+ }),
+ ]
+
+ def test_extend(self):
+ engine = Engine(loaders=self.loaders)
+ output = engine.render_to_string('index.html')
+ self.assertEqual(output, 'base')
+
+ def test_extend_cached(self):
+ engine = Engine(loaders=[
+ ('django.template.loaders.cached.Loader', self.loaders),
+ ])
+ output = engine.render_to_string('index.html')
+ self.assertEqual(output, 'base')
+
+ cache = engine.template_loaders[0].template_cache
+ self.assertTrue('base.html' in cache)
+ self.assertTrue('index.html' in cache)
+
+ # Render a second time from cache
+ output = engine.render_to_string('index.html')
+ self.assertEqual(output, 'base')
+
+ def test_extend_error(self):
+ engine = Engine(loaders=self.loaders)
+ msg = 'Cannot extend templates recursively when using non-recursive template loaders'
+
+ with self.assertRaisesMessage(ExtendsError, msg):
+ engine.render_to_string('recursive.html')
+
+ with self.assertRaisesMessage(ExtendsError, msg):
+ engine.render_to_string('other-recursive.html')
+
+ with self.assertRaisesMessage(ExtendsError, msg):
+ engine.render_to_string('a.html')
diff --git a/tests/template_tests/test_loaders.py b/tests/template_tests/test_loaders.py
index caa0b85db3..41cac5cb9d 100644
--- a/tests/template_tests/test_loaders.py
+++ b/tests/template_tests/test_loaders.py
@@ -10,8 +10,9 @@ from contextlib import contextmanager
from django.template import Context, TemplateDoesNotExist
from django.template.engine import Engine
-from django.test import SimpleTestCase, override_settings
+from django.test import SimpleTestCase, ignore_warnings, override_settings
from django.utils import six
+from django.utils.deprecation import RemovedInDjango21Warning
from .utils import TEMPLATE_DIR
@@ -23,8 +24,9 @@ except ImportError:
class CachedLoaderTests(SimpleTestCase):
- def create_engine(self, **kwargs):
- return Engine(
+ def setUp(self):
+ self.engine = Engine(
+ dirs=[TEMPLATE_DIR],
loaders=[
('django.template.loaders.cached.Loader', [
'django.template.loaders.filesystem.Loader',
@@ -32,26 +34,47 @@ class CachedLoaderTests(SimpleTestCase):
],
)
- def test_templatedir_caching(self):
- """
- #13573 -- Template directories should be part of the cache key.
- """
- engine = self.create_engine()
+ def test_get_template(self):
+ template = self.engine.get_template('index.html')
+ self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, 'index.html'))
+ self.assertEqual(template.origin.template_name, 'index.html')
+ self.assertEqual(template.origin.loader, self.engine.template_loaders[0].loaders[0])
- # Retrieve a template specifying a template directory to check
- t1, name = engine.find_template('test.html', (os.path.join(TEMPLATE_DIR, 'first'),))
- # Now retrieve the same template name, but from a different directory
- t2, name = engine.find_template('test.html', (os.path.join(TEMPLATE_DIR, 'second'),))
+ cache = self.engine.template_loaders[0].get_template_cache
+ self.assertEqual(cache['index.html'], template)
- # The two templates should not have the same content
- self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
+ # Run a second time from cache
+ template = self.engine.get_template('index.html')
+ self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, 'index.html'))
+ self.assertEqual(template.origin.template_name, 'index.html')
+ self.assertEqual(template.origin.loader, self.engine.template_loaders[0].loaders[0])
+
+ def test_get_template_missing(self):
+ with self.assertRaises(TemplateDoesNotExist):
+ self.engine.get_template('doesnotexist.html')
+ e = self.engine.template_loaders[0].get_template_cache['doesnotexist.html']
+ self.assertEqual(e.args[0], 'doesnotexist.html')
+
+ @ignore_warnings(category=RemovedInDjango21Warning)
+ def test_load_template(self):
+ loader = self.engine.template_loaders[0]
+ template, origin = loader.load_template('index.html')
+ self.assertEqual(template.origin.template_name, 'index.html')
+
+ cache = self.engine.template_loaders[0].template_cache
+ self.assertEqual(cache['index.html'][0], template)
+
+ # Run a second time from cache
+ loader = self.engine.template_loaders[0]
+ source, name = loader.load_template('index.html')
+ self.assertEqual(template.origin.template_name, 'index.html')
- def test_missing_template_is_cached(self):
+ @ignore_warnings(category=RemovedInDjango21Warning)
+ def test_load_template_missing(self):
"""
#19949 -- TemplateDoesNotExist exceptions should be cached.
"""
- engine = self.create_engine()
- loader = engine.template_loaders[0]
+ loader = self.engine.template_loaders[0]
self.assertFalse('missing.html' in loader.template_cache)
@@ -64,6 +87,18 @@ class CachedLoaderTests(SimpleTestCase):
"Cached loader failed to cache the TemplateDoesNotExist exception",
)
+ def test_templatedir_caching(self):
+ """
+ #13573 -- Template directories should be part of the cache key.
+ """
+ # Retrieve a template specifying a template directory to check
+ t1, name = self.engine.find_template('test.html', (os.path.join(TEMPLATE_DIR, 'first'),))
+ # Now retrieve the same template name, but from a different directory
+ t2, name = self.engine.find_template('test.html', (os.path.join(TEMPLATE_DIR, 'second'),))
+
+ # The two templates should not have the same content
+ self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
+
@unittest.skipUnless(pkg_resources, 'setuptools is not installed')
class EggLoaderTests(SimpleTestCase):
@@ -117,22 +152,43 @@ class EggLoaderTests(SimpleTestCase):
del sys.modules[name]
del pkg_resources._provider_factories[MockLoader]
- def setUp(self):
- engine = Engine(loaders=[
+ @classmethod
+ def setUpClass(cls):
+ cls.engine = Engine(loaders=[
'django.template.loaders.eggs.Loader',
])
- self.loader = engine.template_loaders[0]
+ cls.loader = cls.engine.template_loaders[0]
+ super(EggLoaderTests, cls).setUpClass()
- def test_existing(self):
+ def test_get_template(self):
templates = {
os.path.normcase('templates/y.html'): six.StringIO("y"),
}
with self.create_egg('egg', templates):
with override_settings(INSTALLED_APPS=['egg']):
- contents, template_name = self.loader.load_template_source("y.html")
- self.assertEqual(contents, "y")
- self.assertEqual(template_name, "egg:egg:templates/y.html")
+ template = self.engine.get_template("y.html")
+
+ self.assertEqual(template.origin.name, 'egg:egg:templates/y.html')
+ self.assertEqual(template.origin.template_name, 'y.html')
+ self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
+
+ output = template.render(Context({}))
+ self.assertEqual(output, "y")
+
+ @ignore_warnings(category=RemovedInDjango21Warning)
+ def test_load_template_source(self):
+ loader = self.engine.template_loaders[0]
+ templates = {
+ os.path.normcase('templates/y.html'): six.StringIO("y"),
+ }
+
+ with self.create_egg('egg', templates):
+ with override_settings(INSTALLED_APPS=['egg']):
+ source, name = loader.load_template_source('y.html')
+
+ self.assertEqual(source.strip(), 'y')
+ self.assertEqual(name, 'egg:egg:templates/y.html')
def test_non_existing(self):
"""
@@ -141,7 +197,7 @@ class EggLoaderTests(SimpleTestCase):
with self.create_egg('egg', {}):
with override_settings(INSTALLED_APPS=['egg']):
with self.assertRaises(TemplateDoesNotExist):
- self.loader.load_template_source("not-existing.html")
+ self.engine.get_template('not-existing.html')
def test_not_installed(self):
"""
@@ -153,13 +209,15 @@ class EggLoaderTests(SimpleTestCase):
with self.create_egg('egg', templates):
with self.assertRaises(TemplateDoesNotExist):
- self.loader.load_template_source("y.html")
+ self.engine.get_template('y.html')
class FileSystemLoaderTests(SimpleTestCase):
- def setUp(self):
- self.engine = Engine(dirs=[TEMPLATE_DIR])
+ @classmethod
+ def setUpClass(cls):
+ cls.engine = Engine(dirs=[TEMPLATE_DIR])
+ super(FileSystemLoaderTests, cls).setUpClass()
@contextmanager
def set_dirs(self, dirs):
@@ -177,13 +235,27 @@ class FileSystemLoaderTests(SimpleTestCase):
def check_sources(path, expected_sources):
expected_sources = [os.path.abspath(s) for s in expected_sources]
self.assertEqual(
- list(loader.get_template_sources(path)),
+ [origin.name for origin in loader.get_template_sources(path)],
expected_sources,
)
with self.set_dirs(dirs):
yield check_sources
+ def test_get_template(self):
+ template = self.engine.get_template('index.html')
+ self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, 'index.html'))
+ self.assertEqual(template.origin.template_name, 'index.html')
+ self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
+ self.assertEqual(template.origin.loader_name, 'django.template.loaders.filesystem.Loader')
+
+ @ignore_warnings(category=RemovedInDjango21Warning)
+ def test_load_template_source(self):
+ loader = self.engine.template_loaders[0]
+ source, name = loader.load_template_source('index.html')
+ self.assertEqual(source.strip(), 'index')
+ self.assertEqual(name, os.path.join(TEMPLATE_DIR, 'index.html'))
+
def test_directory_security(self):
with self.source_checker(['/dir1', '/dir2']) as check_sources:
check_sources('index.html', ['/dir1/index.html', '/dir2/index.html'])
@@ -248,18 +320,56 @@ class FileSystemLoaderTests(SimpleTestCase):
self.engine.get_template('first')
-class AppDirectoriesLoaderTest(SimpleTestCase):
+class AppDirectoriesLoaderTests(SimpleTestCase):
- def setUp(self):
- self.engine = Engine(
+ @classmethod
+ def setUpClass(cls):
+ cls.engine = Engine(
loaders=['django.template.loaders.app_directories.Loader'],
)
+ super(AppDirectoriesLoaderTests, cls).setUpClass()
@override_settings(INSTALLED_APPS=['template_tests'])
- def test_load_template(self):
- self.engine.get_template('index.html')
+ def test_get_template(self):
+ template = self.engine.get_template('index.html')
+ self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, 'index.html'))
+ self.assertEqual(template.origin.template_name, 'index.html')
+ self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
+
+ @ignore_warnings(category=RemovedInDjango21Warning)
+ @override_settings(INSTALLED_APPS=['template_tests'])
+ def test_load_template_source(self):
+ loader = self.engine.template_loaders[0]
+ source, name = loader.load_template_source('index.html')
+ self.assertEqual(source.strip(), 'index')
+ self.assertEqual(name, os.path.join(TEMPLATE_DIR, 'index.html'))
@override_settings(INSTALLED_APPS=[])
def test_not_installed(self):
with self.assertRaises(TemplateDoesNotExist):
self.engine.get_template('index.html')
+
+
+class LocmemLoaderTests(SimpleTestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ cls.engine = Engine(
+ loaders=[('django.template.loaders.locmem.Loader', {
+ 'index.html': 'index',
+ })],
+ )
+ super(LocmemLoaderTests, cls).setUpClass()
+
+ def test_get_template(self):
+ template = self.engine.get_template('index.html')
+ self.assertEqual(template.origin.name, 'index.html')
+ self.assertEqual(template.origin.template_name, 'index.html')
+ self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
+
+ @ignore_warnings(category=RemovedInDjango21Warning)
+ def test_load_template_source(self):
+ loader = self.engine.template_loaders[0]
+ source, name = loader.load_template_source('index.html')
+ self.assertEqual(source.strip(), 'index')
+ self.assertEqual(name, 'index.html')
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index dec21559ae..0a71489f3e 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -6,6 +6,7 @@ import sys
from django.contrib.auth.models import Group
from django.core import urlresolvers
from django.template import Context, Engine, TemplateSyntaxError
+from django.template.base import UNKNOWN_SOURCE
from django.test import SimpleTestCase, override_settings
@@ -13,7 +14,9 @@ class TemplateTests(SimpleTestCase):
def test_string_origin(self):
template = Engine().from_string('string template')
- self.assertEqual(template.origin.source, 'string template')
+ self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
+ self.assertEqual(template.origin.loader_name, None)
+ self.assertEqual(template.source, 'string template')
@override_settings(SETTINGS_MODULE=None)
def test_url_reverse_no_settings_module(self):