summaryrefslogtreecommitdiff
path: root/django/test/utils.py
diff options
context:
space:
mode:
authorChris Beaven <smileychris@gmail.com>2011-04-17 04:52:17 +0000
committerChris Beaven <smileychris@gmail.com>2011-04-17 04:52:17 +0000
commit89e25403ae9dfcdc3eabd66469226d3036c6cc06 (patch)
tree492688866bb0779f5e91a9d16060763aa4090d0a /django/test/utils.py
parentd3c08fd8139416942da30bb22e529357e7708e2c (diff)
downloaddjango-89e25403ae9dfcdc3eabd66469226d3036c6cc06.tar.gz
Fixes #15814 -- Added a test util to abstract the process of using a test template loader
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16030 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/utils.py')
-rw-r--r--django/test/utils.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/django/test/utils.py b/django/test/utils.py
index 2672594916..bf1dc4f165 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -6,12 +6,15 @@ from django.conf import settings
from django.core import mail
from django.core.mail.backends import locmem
from django.test import signals
-from django.template import Template
+from django.template import Template, loader, TemplateDoesNotExist
+from django.template.loaders import cached
from django.utils.translation import deactivate
__all__ = ('Approximate', 'ContextList', 'setup_test_environment',
'teardown_test_environment', 'get_runner')
+RESTORE_LOADERS_ATTR = '_original_template_source_loaders'
+
class Approximate(object):
def __init__(self, val, places=7):
@@ -119,3 +122,41 @@ def get_runner(settings):
test_module = __import__(test_module_name, {}, {}, test_path[-1])
test_runner = getattr(test_module, test_path[-1])
return test_runner
+
+
+def setup_test_template_loader(templates_dict, use_cached_loader=False):
+ """
+ Changes Django to only find templates from within a dictionary (where each
+ key is the template name and each value is the corresponding template
+ content to return).
+
+ Use meth:`restore_template_loaders` to restore the original loaders.
+ """
+ if hasattr(loader, RESTORE_LOADERS_ATTR):
+ raise Exception("loader.%s already exists" % RESTORE_LOADERS_ATTR)
+
+ def test_template_loader(template_name, template_dirs=None):
+ "A custom template loader that loads templates from a dictionary."
+ try:
+ return (templates_dict[template_name], "test:%s" % template_name)
+ except KeyError:
+ raise TemplateDoesNotExist(template_name)
+
+ if use_cached_loader:
+ template_loader = cached.Loader(('test_template_loader',))
+ template_loader._cached_loaders = (test_template_loader,)
+ else:
+ template_loader = test_template_loader
+
+ setattr(loader, RESTORE_LOADERS_ATTR, loader.template_source_loaders)
+ loader.template_source_loaders = (template_loader,)
+ return template_loader
+
+
+def restore_template_loaders():
+ """
+ Restores the original template loaders after
+ :meth:`setup_test_template_loader` has been run.
+ """
+ loader.template_source_loaders = getattr(loader, RESTORE_LOADERS_ATTR)
+ delattr(loader, RESTORE_LOADERS_ATTR)