tests.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from django.template import TemplateDoesNotExist
  2. from django.template.loader import (
  3. get_template, render_to_string, select_template,
  4. )
  5. from django.test import SimpleTestCase, override_settings
  6. from django.test.client import RequestFactory
  7. @override_settings(TEMPLATES=[{
  8. 'BACKEND': 'django.template.backends.dummy.TemplateStrings',
  9. 'APP_DIRS': True,
  10. }, {
  11. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  12. 'APP_DIRS': True,
  13. 'OPTIONS': {
  14. 'context_processors': [
  15. 'django.template.context_processors.request',
  16. ],
  17. },
  18. }])
  19. class TemplateLoaderTests(SimpleTestCase):
  20. def test_get_template_first_engine(self):
  21. template = get_template("template_loader/hello.html")
  22. self.assertEqual(template.render(), "Hello! (template strings)\n")
  23. def test_get_template_second_engine(self):
  24. template = get_template("template_loader/goodbye.html")
  25. self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
  26. def test_get_template_using_engine(self):
  27. template = get_template("template_loader/hello.html", using="django")
  28. self.assertEqual(template.render(), "Hello! (Django templates)\n")
  29. def test_get_template_not_found(self):
  30. with self.assertRaises(TemplateDoesNotExist) as e:
  31. get_template("template_loader/unknown.html")
  32. self.assertEqual(
  33. e.exception.chain[-1].tried[0][0].template_name,
  34. 'template_loader/unknown.html',
  35. )
  36. self.assertEqual(e.exception.chain[-1].backend.name, 'django')
  37. def test_select_template_first_engine(self):
  38. template = select_template(["template_loader/unknown.html",
  39. "template_loader/hello.html"])
  40. self.assertEqual(template.render(), "Hello! (template strings)\n")
  41. def test_select_template_second_engine(self):
  42. template = select_template(["template_loader/unknown.html",
  43. "template_loader/goodbye.html"])
  44. self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
  45. def test_select_template_using_engine(self):
  46. template = select_template(["template_loader/unknown.html",
  47. "template_loader/hello.html"], using="django")
  48. self.assertEqual(template.render(), "Hello! (Django templates)\n")
  49. def test_select_template_empty(self):
  50. with self.assertRaises(TemplateDoesNotExist):
  51. select_template([])
  52. def test_select_template_not_found(self):
  53. with self.assertRaises(TemplateDoesNotExist) as e:
  54. select_template(["template_loader/unknown.html",
  55. "template_loader/missing.html"])
  56. self.assertEqual(
  57. e.exception.chain[0].tried[0][0].template_name,
  58. 'template_loader/unknown.html',
  59. )
  60. self.assertEqual(e.exception.chain[0].backend.name, 'dummy')
  61. self.assertEqual(
  62. e.exception.chain[-1].tried[0][0].template_name,
  63. 'template_loader/missing.html',
  64. )
  65. self.assertEqual(e.exception.chain[-1].backend.name, 'django')
  66. def test_select_template_tries_all_engines_before_names(self):
  67. template = select_template(["template_loader/goodbye.html",
  68. "template_loader/hello.html"])
  69. self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
  70. def test_render_to_string_first_engine(self):
  71. content = render_to_string("template_loader/hello.html")
  72. self.assertEqual(content, "Hello! (template strings)\n")
  73. def test_render_to_string_second_engine(self):
  74. content = render_to_string("template_loader/goodbye.html")
  75. self.assertEqual(content, "Goodbye! (Django templates)\n")
  76. def test_render_to_string_with_request(self):
  77. request = RequestFactory().get('/foobar/')
  78. content = render_to_string("template_loader/request.html", request=request)
  79. self.assertEqual(content, "/foobar/\n")
  80. def test_render_to_string_using_engine(self):
  81. content = render_to_string("template_loader/hello.html", using="django")
  82. self.assertEqual(content, "Hello! (Django templates)\n")
  83. def test_render_to_string_not_found(self):
  84. with self.assertRaises(TemplateDoesNotExist) as e:
  85. render_to_string("template_loader/unknown.html")
  86. self.assertEqual(
  87. e.exception.chain[-1].tried[0][0].template_name,
  88. 'template_loader/unknown.html',
  89. )
  90. self.assertEqual(e.exception.chain[-1].backend.name, 'django')
  91. def test_render_to_string_with_list_first_engine(self):
  92. content = render_to_string(["template_loader/unknown.html",
  93. "template_loader/hello.html"])
  94. self.assertEqual(content, "Hello! (template strings)\n")
  95. def test_render_to_string_with_list_second_engine(self):
  96. content = render_to_string(["template_loader/unknown.html",
  97. "template_loader/goodbye.html"])
  98. self.assertEqual(content, "Goodbye! (Django templates)\n")
  99. def test_render_to_string_with_list_using_engine(self):
  100. content = render_to_string(["template_loader/unknown.html",
  101. "template_loader/hello.html"], using="django")
  102. self.assertEqual(content, "Hello! (Django templates)\n")
  103. def test_render_to_string_with_list_empty(self):
  104. with self.assertRaises(TemplateDoesNotExist):
  105. render_to_string([])
  106. def test_render_to_string_with_list_not_found(self):
  107. with self.assertRaises(TemplateDoesNotExist) as e:
  108. render_to_string(["template_loader/unknown.html",
  109. "template_loader/missing.html"])
  110. self.assertEqual(
  111. e.exception.chain[0].tried[0][0].template_name,
  112. 'template_loader/unknown.html',
  113. )
  114. self.assertEqual(e.exception.chain[0].backend.name, 'dummy')
  115. self.assertEqual(
  116. e.exception.chain[1].tried[0][0].template_name,
  117. 'template_loader/unknown.html',
  118. )
  119. self.assertEqual(e.exception.chain[1].backend.name, 'django')
  120. self.assertEqual(
  121. e.exception.chain[2].tried[0][0].template_name,
  122. 'template_loader/missing.html',
  123. )
  124. self.assertEqual(e.exception.chain[2].backend.name, 'dummy')
  125. self.assertEqual(
  126. e.exception.chain[3].tried[0][0].template_name,
  127. 'template_loader/missing.html',
  128. )
  129. self.assertEqual(e.exception.chain[3].backend.name, 'django')
  130. def test_render_to_string_with_list_tries_all_engines_before_names(self):
  131. content = render_to_string(["template_loader/goodbye.html",
  132. "template_loader/hello.html"])
  133. self.assertEqual(content, "Goodbye! (Django templates)\n")