test_engine.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. from django.template import Context
  3. from django.template.engine import Engine
  4. from django.test import SimpleTestCase, ignore_warnings
  5. from django.utils.deprecation import RemovedInDjango20Warning
  6. from .utils import ROOT, TEMPLATE_DIR
  7. OTHER_DIR = os.path.join(ROOT, 'other_templates')
  8. @ignore_warnings(category=RemovedInDjango20Warning)
  9. class DeprecatedRenderToStringTest(SimpleTestCase):
  10. def setUp(self):
  11. self.engine = Engine(
  12. dirs=[TEMPLATE_DIR],
  13. libraries={'custom': 'template_tests.templatetags.custom'},
  14. )
  15. def test_basic_context(self):
  16. self.assertEqual(
  17. self.engine.render_to_string('test_context.html', {'obj': 'test'}),
  18. 'obj:test\n',
  19. )
  20. def test_existing_context_kept_clean(self):
  21. context = Context({'obj': 'before'})
  22. output = self.engine.render_to_string(
  23. 'test_context.html', {'obj': 'after'}, context_instance=context,
  24. )
  25. self.assertEqual(output, 'obj:after\n')
  26. self.assertEqual(context['obj'], 'before')
  27. def test_no_empty_dict_pushed_to_stack(self):
  28. """
  29. #21741 -- An empty dict should not be pushed to the context stack when
  30. render_to_string is called without a context argument.
  31. """
  32. # The stack should have a length of 1, corresponding to the builtins
  33. self.assertEqual(
  34. '1',
  35. self.engine.render_to_string('test_context_stack.html').strip(),
  36. )
  37. self.assertEqual(
  38. '1',
  39. self.engine.render_to_string(
  40. 'test_context_stack.html',
  41. context_instance=Context()
  42. ).strip(),
  43. )
  44. class LoaderTests(SimpleTestCase):
  45. def test_origin(self):
  46. engine = Engine(dirs=[TEMPLATE_DIR], debug=True)
  47. template = engine.get_template('index.html')
  48. self.assertEqual(template.origin.template_name, 'index.html')
  49. def test_loader_priority(self):
  50. """
  51. #21460 -- Check that the order of template loader works.
  52. """
  53. loaders = [
  54. 'django.template.loaders.filesystem.Loader',
  55. 'django.template.loaders.app_directories.Loader',
  56. ]
  57. engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
  58. template = engine.get_template('priority/foo.html')
  59. self.assertEqual(template.render(Context()), 'priority\n')
  60. def test_cached_loader_priority(self):
  61. """
  62. Check that the order of template loader works. Refs #21460.
  63. """
  64. loaders = [
  65. ('django.template.loaders.cached.Loader', [
  66. 'django.template.loaders.filesystem.Loader',
  67. 'django.template.loaders.app_directories.Loader',
  68. ]),
  69. ]
  70. engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
  71. template = engine.get_template('priority/foo.html')
  72. self.assertEqual(template.render(Context()), 'priority\n')
  73. template = engine.get_template('priority/foo.html')
  74. self.assertEqual(template.render(Context()), 'priority\n')
  75. @ignore_warnings(category=RemovedInDjango20Warning)
  76. class TemplateDirsOverrideTests(SimpleTestCase):
  77. DIRS = ((OTHER_DIR, ), [OTHER_DIR])
  78. def setUp(self):
  79. self.engine = Engine()
  80. def test_render_to_string(self):
  81. for dirs in self.DIRS:
  82. self.assertEqual(
  83. self.engine.render_to_string('test_dirs.html', dirs=dirs),
  84. 'spam eggs\n',
  85. )
  86. def test_get_template(self):
  87. for dirs in self.DIRS:
  88. template = self.engine.get_template('test_dirs.html', dirs=dirs)
  89. self.assertEqual(template.render(Context()), 'spam eggs\n')
  90. def test_select_template(self):
  91. for dirs in self.DIRS:
  92. template = self.engine.select_template(['test_dirs.html'], dirs=dirs)
  93. self.assertEqual(template.render(Context()), 'spam eggs\n')