Debian Codegraph @

tests/template_tests/syntax_tests/test_cache.py

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