|
1 """ |
|
2 Test cases for the template loaders |
|
3 |
|
4 Note: This test requires setuptools! |
|
5 """ |
|
6 |
|
7 from django.conf import settings |
|
8 |
|
9 if __name__ == '__main__': |
|
10 settings.configure() |
|
11 |
|
12 import unittest |
|
13 import sys |
|
14 import pkg_resources |
|
15 import imp |
|
16 import StringIO |
|
17 import os.path |
|
18 import warnings |
|
19 |
|
20 from django.template import TemplateDoesNotExist, Context |
|
21 from django.template.loaders.eggs import load_template_source as lts_egg |
|
22 from django.template.loaders.eggs import Loader as EggLoader |
|
23 from django.template import loader |
|
24 from django.test.utils import get_warnings_state, restore_warnings_state |
|
25 |
|
26 # Mock classes and objects for pkg_resources functions. |
|
27 class MockProvider(pkg_resources.NullProvider): |
|
28 def __init__(self, module): |
|
29 pkg_resources.NullProvider.__init__(self, module) |
|
30 self.module = module |
|
31 |
|
32 def _has(self, path): |
|
33 return path in self.module._resources |
|
34 |
|
35 def _isdir(self,path): |
|
36 return False |
|
37 |
|
38 def get_resource_stream(self, manager, resource_name): |
|
39 return self.module._resources[resource_name] |
|
40 |
|
41 def _get(self, path): |
|
42 return self.module._resources[path].read() |
|
43 |
|
44 class MockLoader(object): |
|
45 pass |
|
46 |
|
47 def create_egg(name, resources): |
|
48 """ |
|
49 Creates a mock egg with a list of resources. |
|
50 |
|
51 name: The name of the module. |
|
52 resources: A dictionary of resources. Keys are the names and values the data. |
|
53 """ |
|
54 egg = imp.new_module(name) |
|
55 egg.__loader__ = MockLoader() |
|
56 egg._resources = resources |
|
57 sys.modules[name] = egg |
|
58 |
|
59 class DeprecatedEggLoaderTest(unittest.TestCase): |
|
60 "Test the deprecated load_template_source interface to the egg loader" |
|
61 def setUp(self): |
|
62 pkg_resources._provider_factories[MockLoader] = MockProvider |
|
63 |
|
64 self.empty_egg = create_egg("egg_empty", {}) |
|
65 self.egg_1 = create_egg("egg_1", { |
|
66 os.path.normcase('templates/y.html') : StringIO.StringIO("y"), |
|
67 os.path.normcase('templates/x.txt') : StringIO.StringIO("x"), |
|
68 }) |
|
69 self._old_installed_apps = settings.INSTALLED_APPS |
|
70 settings.INSTALLED_APPS = [] |
|
71 self._warnings_state = get_warnings_state() |
|
72 warnings.simplefilter("ignore", PendingDeprecationWarning) |
|
73 |
|
74 def tearDown(self): |
|
75 settings.INSTALLED_APPS = self._old_installed_apps |
|
76 restore_warnings_state(self._warnings_state) |
|
77 |
|
78 def test_existing(self): |
|
79 "A template can be loaded from an egg" |
|
80 settings.INSTALLED_APPS = ['egg_1'] |
|
81 contents, template_name = lts_egg("y.html") |
|
82 self.assertEqual(contents, "y") |
|
83 self.assertEqual(template_name, "egg:egg_1:templates/y.html") |
|
84 |
|
85 |
|
86 class EggLoaderTest(unittest.TestCase): |
|
87 def setUp(self): |
|
88 pkg_resources._provider_factories[MockLoader] = MockProvider |
|
89 |
|
90 self.empty_egg = create_egg("egg_empty", {}) |
|
91 self.egg_1 = create_egg("egg_1", { |
|
92 os.path.normcase('templates/y.html') : StringIO.StringIO("y"), |
|
93 os.path.normcase('templates/x.txt') : StringIO.StringIO("x"), |
|
94 }) |
|
95 self._old_installed_apps = settings.INSTALLED_APPS |
|
96 settings.INSTALLED_APPS = [] |
|
97 |
|
98 def tearDown(self): |
|
99 settings.INSTALLED_APPS = self._old_installed_apps |
|
100 |
|
101 def test_empty(self): |
|
102 "Loading any template on an empty egg should fail" |
|
103 settings.INSTALLED_APPS = ['egg_empty'] |
|
104 egg_loader = EggLoader() |
|
105 self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html") |
|
106 |
|
107 def test_non_existing(self): |
|
108 "Template loading fails if the template is not in the egg" |
|
109 settings.INSTALLED_APPS = ['egg_1'] |
|
110 egg_loader = EggLoader() |
|
111 self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html") |
|
112 |
|
113 def test_existing(self): |
|
114 "A template can be loaded from an egg" |
|
115 settings.INSTALLED_APPS = ['egg_1'] |
|
116 egg_loader = EggLoader() |
|
117 contents, template_name = egg_loader.load_template_source("y.html") |
|
118 self.assertEqual(contents, "y") |
|
119 self.assertEqual(template_name, "egg:egg_1:templates/y.html") |
|
120 |
|
121 def test_not_installed(self): |
|
122 "Loading an existent template from an egg not included in INSTALLED_APPS should fail" |
|
123 settings.INSTALLED_APPS = [] |
|
124 egg_loader = EggLoader() |
|
125 self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html") |
|
126 |
|
127 class CachedLoader(unittest.TestCase): |
|
128 def setUp(self): |
|
129 self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS |
|
130 settings.TEMPLATE_LOADERS = ( |
|
131 ('django.template.loaders.cached.Loader', ( |
|
132 'django.template.loaders.filesystem.Loader', |
|
133 ) |
|
134 ), |
|
135 ) |
|
136 def tearDown(self): |
|
137 settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS |
|
138 |
|
139 def test_templatedir_caching(self): |
|
140 "Check that the template directories form part of the template cache key. Refs #13573" |
|
141 # Retrive a template specifying a template directory to check |
|
142 t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'first'),)) |
|
143 # Now retrieve the same template name, but from a different directory |
|
144 t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'second'),)) |
|
145 |
|
146 # The two templates should not have the same content |
|
147 self.assertNotEqual(t1.render(Context({})), t2.render(Context({}))) |
|
148 |
|
149 if __name__ == "__main__": |
|
150 unittest.main() |