test_loaders.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. """
  2. Test cases for the template loaders
  3. Note: This test requires setuptools!
  4. """
  5. from django.conf import settings
  6. if __name__ == '__main__':
  7. settings.configure()
  8. import imp
  9. import os.path
  10. import sys
  11. import unittest
  12. try:
  13. import pkg_resources
  14. except ImportError:
  15. pkg_resources = None
  16. from django.core.apps import app_cache
  17. from django.template import TemplateDoesNotExist, Context
  18. from django.template.loaders.eggs import Loader as EggLoader
  19. from django.template import loader
  20. from django.test import TestCase
  21. from django.test.utils import override_settings
  22. from django.utils import six
  23. from django.utils._os import upath
  24. from django.utils.six import StringIO
  25. # Mock classes and objects for pkg_resources functions.
  26. class MockLoader(object):
  27. pass
  28. def create_egg(name, resources):
  29. """
  30. Creates a mock egg with a list of resources.
  31. name: The name of the module.
  32. resources: A dictionary of resources. Keys are the names and values the data.
  33. """
  34. egg = imp.new_module(name)
  35. egg.__loader__ = MockLoader()
  36. egg._resources = resources
  37. sys.modules[name] = egg
  38. @unittest.skipUnless(pkg_resources, 'setuptools is not installed')
  39. class EggLoaderTest(TestCase):
  40. def setUp(self):
  41. # Defined here b/c at module scope we may not have pkg_resources
  42. class MockProvider(pkg_resources.NullProvider):
  43. def __init__(self, module):
  44. pkg_resources.NullProvider.__init__(self, module)
  45. self.module = module
  46. def _has(self, path):
  47. return path in self.module._resources
  48. def _isdir(self, path):
  49. return False
  50. def get_resource_stream(self, manager, resource_name):
  51. return self.module._resources[resource_name]
  52. def _get(self, path):
  53. return self.module._resources[path].read()
  54. pkg_resources._provider_factories[MockLoader] = MockProvider
  55. self.empty_egg = create_egg("egg_empty", {})
  56. self.egg_1 = create_egg("egg_1", {
  57. os.path.normcase('templates/y.html'): StringIO("y"),
  58. os.path.normcase('templates/x.txt'): StringIO("x"),
  59. })
  60. def test_empty(self):
  61. "Loading any template on an empty egg should fail"
  62. with app_cache._with_app('egg_empty'):
  63. egg_loader = EggLoader()
  64. self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
  65. def test_non_existing(self):
  66. "Template loading fails if the template is not in the egg"
  67. with app_cache._with_app('egg_1'):
  68. egg_loader = EggLoader()
  69. self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
  70. def test_existing(self):
  71. "A template can be loaded from an egg"
  72. with app_cache._with_app('egg_1'):
  73. egg_loader = EggLoader()
  74. contents, template_name = egg_loader.load_template_source("y.html")
  75. self.assertEqual(contents, "y")
  76. self.assertEqual(template_name, "egg:egg_1:templates/y.html")
  77. def test_not_installed(self):
  78. "Loading an existent template from an egg not included in any app should fail"
  79. egg_loader = EggLoader()
  80. self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")
  81. @override_settings(
  82. TEMPLATE_LOADERS=(
  83. ('django.template.loaders.cached.Loader', (
  84. 'django.template.loaders.filesystem.Loader',
  85. )),
  86. )
  87. )
  88. class CachedLoader(TestCase):
  89. def test_templatedir_caching(self):
  90. "Check that the template directories form part of the template cache key. Refs #13573"
  91. # Retrive a template specifying a template directory to check
  92. t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'first'),))
  93. # Now retrieve the same template name, but from a different directory
  94. t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'second'),))
  95. # The two templates should not have the same content
  96. self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
  97. def test_missing_template_is_cached(self):
  98. "#19949 -- Check that the missing template is cached."
  99. template_loader = loader.find_template_loader(settings.TEMPLATE_LOADERS[0])
  100. # Empty cache, which may be filled from previous tests.
  101. template_loader.reset()
  102. # Check that 'missing.html' isn't already in cache before 'missing.html' is loaded
  103. self.assertRaises(KeyError, lambda: template_loader.template_cache["missing.html"])
  104. # Try to load it, it should fail
  105. self.assertRaises(TemplateDoesNotExist, template_loader.load_template, "missing.html")
  106. # Verify that the fact that the missing template, which hasn't been found, has actually
  107. # been cached:
  108. self.assertEqual(template_loader.template_cache.get("missing.html"),
  109. TemplateDoesNotExist,
  110. "Cached template loader doesn't cache file lookup misses. It should.")
  111. @override_settings(
  112. TEMPLATE_DIRS=(
  113. os.path.join(os.path.dirname(upath(__file__)), 'templates'),
  114. )
  115. )
  116. class RenderToStringTest(TestCase):
  117. def test_basic(self):
  118. self.assertEqual(loader.render_to_string('test_context.html'), 'obj:')
  119. def test_basic_context(self):
  120. self.assertEqual(loader.render_to_string('test_context.html',
  121. {'obj': 'test'}), 'obj:test')
  122. def test_existing_context_kept_clean(self):
  123. context = Context({'obj': 'before'})
  124. output = loader.render_to_string('test_context.html', {'obj': 'after'},
  125. context_instance=context)
  126. self.assertEqual(output, 'obj:after')
  127. self.assertEqual(context['obj'], 'before')
  128. def test_empty_list(self):
  129. six.assertRaisesRegex(self, TemplateDoesNotExist,
  130. 'No template names provided$',
  131. loader.render_to_string, [])
  132. def test_select_templates_from_empty_list(self):
  133. six.assertRaisesRegex(self, TemplateDoesNotExist,
  134. 'No template names provided$',
  135. loader.select_template, [])
  136. class TemplateDirsOverrideTest(unittest.TestCase):
  137. dirs_tuple = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
  138. dirs_list = list(dirs_tuple)
  139. dirs_iter = (dirs_tuple, dirs_list)
  140. def test_render_to_string(self):
  141. for dirs in self.dirs_iter:
  142. self.assertEqual(loader.render_to_string('test_dirs.html', dirs=dirs), 'spam eggs\n')
  143. def test_get_template(self):
  144. for dirs in self.dirs_iter:
  145. template = loader.get_template('test_dirs.html', dirs=dirs)
  146. self.assertEqual(template.render(Context({})), 'spam eggs\n')
  147. def test_select_template(self):
  148. for dirs in self.dirs_iter:
  149. template = loader.select_template(['test_dirs.html'], dirs=dirs)
  150. self.assertEqual(template.render(Context({})), 'spam eggs\n')
  151. @override_settings(
  152. TEMPLATE_LOADERS=(
  153. ('django.template.loaders.cached.Loader', (
  154. 'django.template.loaders.filesystem.Loader',
  155. 'django.template.loaders.app_directories.Loader',
  156. )),
  157. )
  158. )
  159. class PriorityCacheLoader(TestCase):
  160. def test_basic(self):
  161. """
  162. Check that the order of template loader works. Refs #21460.
  163. """
  164. t1, name = loader.find_template('priority/foo.html')
  165. self.assertEqual(t1.render(Context({})), 'priority\n')
  166. @override_settings(
  167. TEMPLATE_LOADERS=('django.template.loaders.filesystem.Loader',
  168. 'django.template.loaders.app_directories.Loader',),
  169. )
  170. class PriorityLoader(TestCase):
  171. def test_basic(self):
  172. """
  173. Check that the order of template loader works. Refs #21460.
  174. """
  175. t1, name = loader.find_template('priority/foo.html')
  176. self.assertEqual(t1.render(Context({})), 'priority\n')