Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 89e25403ae9dfcdc3eabd66469226d3036c6cc06 1 parent d3c08fd
Chris Beaven authored April 17, 2011
43  django/test/utils.py
@@ -6,12 +6,15 @@
6 6
 from django.core import mail
7 7
 from django.core.mail.backends import locmem
8 8
 from django.test import signals
9  
-from django.template import Template
  9
+from django.template import Template, loader, TemplateDoesNotExist
  10
+from django.template.loaders import cached
10 11
 from django.utils.translation import deactivate
11 12
 
12 13
 __all__ = ('Approximate', 'ContextList', 'setup_test_environment',
13 14
        'teardown_test_environment', 'get_runner')
14 15
 
  16
+RESTORE_LOADERS_ATTR = '_original_template_source_loaders'
  17
+
15 18
 
16 19
 class Approximate(object):
17 20
     def __init__(self, val, places=7):
@@ -119,3 +122,41 @@ def get_runner(settings):
119 122
     test_module = __import__(test_module_name, {}, {}, test_path[-1])
120 123
     test_runner = getattr(test_module, test_path[-1])
121 124
     return test_runner
  125
+
  126
+
  127
+def setup_test_template_loader(templates_dict, use_cached_loader=False):
  128
+    """
  129
+    Changes Django to only find templates from within a dictionary (where each
  130
+    key is the template name and each value is the corresponding template
  131
+    content to return).
  132
+
  133
+    Use meth:`restore_template_loaders` to restore the original loaders.
  134
+    """
  135
+    if hasattr(loader, RESTORE_LOADERS_ATTR):
  136
+        raise Exception("loader.%s already exists" % RESTORE_LOADERS_ATTR)
  137
+
  138
+    def test_template_loader(template_name, template_dirs=None):
  139
+        "A custom template loader that loads templates from a dictionary."
  140
+        try:
  141
+            return (templates_dict[template_name], "test:%s" % template_name)
  142
+        except KeyError:
  143
+            raise TemplateDoesNotExist(template_name)
  144
+
  145
+    if use_cached_loader:
  146
+        template_loader = cached.Loader(('test_template_loader',))
  147
+        template_loader._cached_loaders = (test_template_loader,)
  148
+    else:
  149
+        template_loader = test_template_loader
  150
+
  151
+    setattr(loader, RESTORE_LOADERS_ATTR, loader.template_source_loaders)
  152
+    loader.template_source_loaders = (template_loader,)
  153
+    return template_loader
  154
+
  155
+
  156
+def restore_template_loaders():
  157
+    """
  158
+    Restores the original template loaders after
  159
+    :meth:`setup_test_template_loader` has been run.
  160
+    """
  161
+    loader.template_source_loaders = getattr(loader, RESTORE_LOADERS_ATTR)
  162
+    delattr(loader, RESTORE_LOADERS_ATTR)
22  tests/regressiontests/templates/tests.py
@@ -18,7 +18,8 @@
18 18
 from django.core import urlresolvers
19 19
 from django.template import loader
20 20
 from django.template.loaders import app_directories, filesystem, cached
21  
-from django.test.utils import get_warnings_state, restore_warnings_state
  21
+from django.test.utils import get_warnings_state, restore_warnings_state,\
  22
+    setup_test_template_loader, restore_template_loaders
22 23
 from django.utils import unittest
23 24
 from django.utils.translation import activate, deactivate, ugettext as _
24 25
 from django.utils.safestring import mark_safe
@@ -390,19 +391,10 @@ def test_templates(self):
390 391
 
391 392
         template_tests.update(filter_tests)
392 393
 
393  
-        # Register our custom template loader.
394  
-        def test_template_loader(template_name, template_dirs=None):
395  
-            "A custom template loader that loads the unit-test templates."
396  
-            try:
397  
-                return (template_tests[template_name][0] , "test:%s" % template_name)
398  
-            except KeyError:
399  
-                raise template.TemplateDoesNotExist(template_name)
400  
-
401  
-        cache_loader = cached.Loader(('test_template_loader',))
402  
-        cache_loader._cached_loaders = (test_template_loader,)
403  
-
404  
-        old_template_loaders = loader.template_source_loaders
405  
-        loader.template_source_loaders = [cache_loader]
  394
+        cache_loader = setup_test_template_loader(
  395
+            dict([(name, t[0]) for name, t in template_tests.iteritems()]),
  396
+            use_cached_loader=True,
  397
+        )
406 398
 
407 399
         failures = []
408 400
         tests = template_tests.items()
@@ -490,7 +482,7 @@ def test_template_loader(template_name, template_dirs=None):
490 482
                 expected_invalid_str = 'INVALID'
491 483
                 template_base.invalid_var_format_string = False
492 484
 
493  
-        loader.template_source_loaders = old_template_loaders
  485
+        restore_template_loaders()
494 486
         deactivate()
495 487
         settings.TEMPLATE_DEBUG = old_td
496 488
         settings.TEMPLATE_STRING_IF_INVALID = old_invalid

0 notes on commit 89e2540

Please sign in to comment.
Something went wrong with that request. Please try again.