tests.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import sys
  4. from django.contrib.auth.models import Group
  5. from django.core import urlresolvers
  6. from django.template import Context, Engine, TemplateSyntaxError
  7. from django.template.base import UNKNOWN_SOURCE
  8. from django.test import SimpleTestCase, override_settings
  9. class TemplateTests(SimpleTestCase):
  10. def test_string_origin(self):
  11. template = Engine().from_string('string template')
  12. self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
  13. self.assertEqual(template.origin.loader_name, None)
  14. self.assertEqual(template.source, 'string template')
  15. @override_settings(SETTINGS_MODULE=None)
  16. def test_url_reverse_no_settings_module(self):
  17. """
  18. #9005 -- url tag shouldn't require settings.SETTINGS_MODULE to
  19. be set.
  20. """
  21. t = Engine(debug=True).from_string('{% url will_not_match %}')
  22. c = Context()
  23. with self.assertRaises(urlresolvers.NoReverseMatch):
  24. t.render(c)
  25. def test_url_reverse_view_name(self):
  26. """
  27. #19827 -- url tag should keep original strack trace when reraising
  28. exception.
  29. """
  30. t = Engine().from_string('{% url will_not_match %}')
  31. c = Context()
  32. try:
  33. t.render(c)
  34. except urlresolvers.NoReverseMatch:
  35. tb = sys.exc_info()[2]
  36. depth = 0
  37. while tb.tb_next is not None:
  38. tb = tb.tb_next
  39. depth += 1
  40. self.assertGreater(depth, 5,
  41. "The traceback context was lost when reraising the traceback. See #19827")
  42. def test_no_wrapped_exception(self):
  43. """
  44. # 16770 -- The template system doesn't wrap exceptions, but annotates
  45. them.
  46. """
  47. engine = Engine(debug=True)
  48. c = Context({"coconuts": lambda: 42 / 0})
  49. t = engine.from_string("{{ coconuts }}")
  50. with self.assertRaises(ZeroDivisionError) as e:
  51. t.render(c)
  52. debug = e.exception.template_debug
  53. self.assertEqual(debug['start'], 0)
  54. self.assertEqual(debug['end'], 14)
  55. def test_invalid_block_suggestion(self):
  56. """
  57. #7876 -- Error messages should include the unexpected block name.
  58. """
  59. engine = Engine()
  60. with self.assertRaises(TemplateSyntaxError) as e:
  61. engine.from_string("{% if 1 %}lala{% endblock %}{% endif %}")
  62. self.assertEqual(
  63. e.exception.args[0],
  64. "Invalid block tag on line 1: 'endblock', expected 'elif', 'else' or 'endif'",
  65. )
  66. def test_compile_filter_expression_error(self):
  67. """
  68. 19819 -- Make sure the correct token is highlighted for
  69. FilterExpression errors.
  70. """
  71. engine = Engine(debug=True)
  72. msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
  73. with self.assertRaisesMessage(TemplateSyntaxError, msg) as e:
  74. engine.from_string("{% if 1 %}{{ foo@bar }}{% endif %}")
  75. debug = e.exception.template_debug
  76. self.assertEqual((debug['start'], debug['end']), (10, 23))
  77. self.assertEqual((debug['during']), '{{ foo@bar }}')
  78. def test_compile_tag_error(self):
  79. """
  80. Errors raised while compiling nodes should include the token
  81. information.
  82. """
  83. engine = Engine(
  84. debug=True,
  85. libraries={'bad_tag': 'template_tests.templatetags.bad_tag'},
  86. )
  87. with self.assertRaises(RuntimeError) as e:
  88. engine.from_string("{% load bad_tag %}{% badtag %}")
  89. self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
  90. def test_super_errors(self):
  91. """
  92. #18169 -- NoReverseMatch should not be silence in block.super.
  93. """
  94. engine = Engine(app_dirs=True)
  95. t = engine.get_template('included_content.html')
  96. with self.assertRaises(urlresolvers.NoReverseMatch):
  97. t.render(Context())
  98. def test_debug_tag_non_ascii(self):
  99. """
  100. #23060 -- Test non-ASCII model representation in debug output.
  101. """
  102. group = Group(name="清風")
  103. c1 = Context({"objs": [group]})
  104. t1 = Engine().from_string('{% debug %}')
  105. self.assertIn("清風", t1.render(c1))
  106. def test_extends_generic_template(self):
  107. """
  108. #24338 -- Allow extending django.template.backends.django.Template
  109. objects.
  110. """
  111. engine = Engine()
  112. parent = engine.from_string('{% block content %}parent{% endblock %}')
  113. child = engine.from_string(
  114. '{% extends parent %}{% block content %}child{% endblock %}')
  115. self.assertEqual(child.render(Context({'parent': parent})), 'child')