test_cache.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. from django.core.cache import cache
  2. from django.template import Context, Engine, TemplateSyntaxError
  3. from django.test import SimpleTestCase, override_settings
  4. from ..utils import setup
  5. class CacheTagTests(SimpleTestCase):
  6. libraries = {
  7. 'cache': 'django.templatetags.cache',
  8. 'custom': 'template_tests.templatetags.custom',
  9. }
  10. def tearDown(self):
  11. cache.clear()
  12. @setup({'cache03': '{% load cache %}{% cache 2 test %}cache03{% endcache %}'})
  13. def test_cache03(self):
  14. output = self.engine.render_to_string('cache03')
  15. self.assertEqual(output, 'cache03')
  16. @setup({
  17. 'cache03': '{% load cache %}{% cache 2 test %}cache03{% endcache %}',
  18. 'cache04': '{% load cache %}{% cache 2 test %}cache04{% endcache %}',
  19. })
  20. def test_cache04(self):
  21. self.engine.render_to_string('cache03')
  22. output = self.engine.render_to_string('cache04')
  23. self.assertEqual(output, 'cache03')
  24. @setup({'cache05': '{% load cache %}{% cache 2 test foo %}cache05{% endcache %}'})
  25. def test_cache05(self):
  26. output = self.engine.render_to_string('cache05', {'foo': 1})
  27. self.assertEqual(output, 'cache05')
  28. @setup({'cache06': '{% load cache %}{% cache 2 test foo %}cache06{% endcache %}'})
  29. def test_cache06(self):
  30. output = self.engine.render_to_string('cache06', {'foo': 2})
  31. self.assertEqual(output, 'cache06')
  32. @setup({
  33. 'cache05': '{% load cache %}{% cache 2 test foo %}cache05{% endcache %}',
  34. 'cache07': '{% load cache %}{% cache 2 test foo %}cache07{% endcache %}',
  35. })
  36. def test_cache07(self):
  37. context = {'foo': 1}
  38. self.engine.render_to_string('cache05', context)
  39. output = self.engine.render_to_string('cache07', context)
  40. self.assertEqual(output, 'cache05')
  41. @setup({
  42. 'cache06': '{% load cache %}{% cache 2 test foo %}cache06{% endcache %}',
  43. 'cache08': '{% load cache %}{% cache time test foo %}cache08{% endcache %}',
  44. })
  45. def test_cache08(self):
  46. """
  47. Allow first argument to be a variable.
  48. """
  49. context = {'foo': 2, 'time': 2}
  50. self.engine.render_to_string('cache06', context)
  51. output = self.engine.render_to_string('cache08', context)
  52. self.assertEqual(output, 'cache06')
  53. # Raise exception if we don't have at least 2 args, first one integer.
  54. @setup({'cache11': '{% load cache %}{% cache %}{% endcache %}'})
  55. def test_cache11(self):
  56. with self.assertRaises(TemplateSyntaxError):
  57. self.engine.get_template('cache11')
  58. @setup({'cache12': '{% load cache %}{% cache 1 %}{% endcache %}'})
  59. def test_cache12(self):
  60. with self.assertRaises(TemplateSyntaxError):
  61. self.engine.get_template('cache12')
  62. @setup({'cache13': '{% load cache %}{% cache foo bar %}{% endcache %}'})
  63. def test_cache13(self):
  64. with self.assertRaises(TemplateSyntaxError):
  65. self.engine.render_to_string('cache13')
  66. @setup({'cache14': '{% load cache %}{% cache foo bar %}{% endcache %}'})
  67. def test_cache14(self):
  68. with self.assertRaises(TemplateSyntaxError):
  69. self.engine.render_to_string('cache14', {'foo': 'fail'})
  70. @setup({'cache15': '{% load cache %}{% cache foo bar %}{% endcache %}'})
  71. def test_cache15(self):
  72. with self.assertRaises(TemplateSyntaxError):
  73. self.engine.render_to_string('cache15', {'foo': []})
  74. @setup({'cache16': '{% load cache %}{% cache 1 foo bar %}{% endcache %}'})
  75. def test_cache16(self):
  76. """
  77. Regression test for #7460.
  78. """
  79. output = self.engine.render_to_string('cache16', {'foo': 'foo', 'bar': 'with spaces'})
  80. self.assertEqual(output, '')
  81. @setup({'cache17': '{% load cache %}{% cache 10 long_cache_key poem %}Some Content{% endcache %}'})
  82. def test_cache17(self):
  83. """
  84. Regression test for #11270.
  85. """
  86. output = self.engine.render_to_string(
  87. 'cache17',
  88. {
  89. 'poem': (
  90. 'Oh freddled gruntbuggly/Thy micturations are to me/'
  91. 'As plurdled gabbleblotchits/On a lurgid bee/'
  92. 'That mordiously hath bitled out/Its earted jurtles/'
  93. 'Into a rancid festering/Or else I shall rend thee in the gobberwarts'
  94. 'with my blurglecruncheon/See if I dont.'
  95. ),
  96. }
  97. )
  98. self.assertEqual(output, 'Some Content')
  99. @setup({'cache18': '{% load cache custom %}{% cache 2|noop:"x y" cache18 %}cache18{% endcache %}'})
  100. def test_cache18(self):
  101. """
  102. Test whitespace in filter arguments
  103. """
  104. output = self.engine.render_to_string('cache18')
  105. self.assertEqual(output, 'cache18')
  106. @setup({
  107. 'first': '{% load cache %}{% cache None fragment19 %}content{% endcache %}',
  108. 'second': '{% load cache %}{% cache None fragment19 %}not rendered{% endcache %}'
  109. })
  110. def test_none_timeout(self):
  111. """A timeout of None means "cache forever"."""
  112. output = self.engine.render_to_string('first')
  113. self.assertEqual(output, 'content')
  114. output = self.engine.render_to_string('second')
  115. self.assertEqual(output, 'content')
  116. class CacheTests(SimpleTestCase):
  117. @classmethod
  118. def setUpClass(cls):
  119. cls.engine = Engine(libraries={'cache': 'django.templatetags.cache'})
  120. super().setUpClass()
  121. def test_cache_regression_20130(self):
  122. t = self.engine.from_string('{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}')
  123. cachenode = t.nodelist[1]
  124. self.assertEqual(cachenode.fragment_name, 'regression_20130')
  125. @override_settings(CACHES={
  126. 'default': {
  127. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  128. 'LOCATION': 'default',
  129. },
  130. 'template_fragments': {
  131. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  132. 'LOCATION': 'fragments',
  133. },
  134. })
  135. def test_cache_fragment_cache(self):
  136. """
  137. When a cache called "template_fragments" is present, the cache tag
  138. will use it in preference to 'default'
  139. """
  140. t1 = self.engine.from_string('{% load cache %}{% cache 1 fragment %}foo{% endcache %}')
  141. t2 = self.engine.from_string('{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}')
  142. ctx = Context()
  143. o1 = t1.render(ctx)
  144. o2 = t2.render(ctx)
  145. self.assertEqual(o1, 'foo')
  146. self.assertEqual(o2, 'bar')
  147. def test_cache_missing_backend(self):
  148. """
  149. When a cache that doesn't exist is specified, the cache tag will
  150. raise a TemplateSyntaxError
  151. '"""
  152. t = self.engine.from_string('{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}')
  153. ctx = Context()
  154. with self.assertRaises(TemplateSyntaxError):
  155. t.render(ctx)