Debian Codegraph @

tests/template_tests/test_callables.py

raw
1 from unittest import TestCase
2
3 from django.db.models.utils import AltersData
4 from django.template import Context, Engine
5
6
7 class CallableVariablesTests(TestCase):
8 @classmethod
9 def setUpClass(cls):
10 cls.engine = Engine()
11 super().setUpClass()
12
13 def test_callable(self):
14 class Doodad:
15 def __init__(self, value):
16 self.num_calls = 0
17 self.value = value
18
19 def __call__(self):
20 self.num_calls += 1
21 return {"the_value": self.value}
22
23 my_doodad = Doodad(42)
24 c = Context({"my_doodad": my_doodad})
25
26 # We can't access ``my_doodad.value`` in the template, because
27 # ``my_doodad.__call__`` will be invoked first, yielding a dictionary
28 # without a key ``value``.
29 t = self.engine.from_string("{{ my_doodad.value }}")
30 self.assertEqual(t.render(c), "")
31
32 # We can confirm that the doodad has been called
33 self.assertEqual(my_doodad.num_calls, 1)
34
35 # But we can access keys on the dict that's returned
36 # by ``__call__``, instead.
37 t = self.engine.from_string("{{ my_doodad.the_value }}")
38 self.assertEqual(t.render(c), "42")
39 self.assertEqual(my_doodad.num_calls, 2)
40
41 def test_alters_data(self):
42 class Doodad:
43 alters_data = True
44
45 def __init__(self, value):
46 self.num_calls = 0
47 self.value = value
48
49 def __call__(self):
50 self.num_calls += 1
51 return {"the_value": self.value}
52
53 my_doodad = Doodad(42)
54 c = Context({"my_doodad": my_doodad})
55
56 # Since ``my_doodad.alters_data`` is True, the template system will not
57 # try to call our doodad but will use string_if_invalid
58 t = self.engine.from_string("{{ my_doodad.value }}")
59 self.assertEqual(t.render(c), "")
60 t = self.engine.from_string("{{ my_doodad.the_value }}")
61 self.assertEqual(t.render(c), "")
62
63 # Double-check that the object was really never called during the
64 # template rendering.
65 self.assertEqual(my_doodad.num_calls, 0)
66
67 def test_alters_data_propagation(self):
68 class GrandParentLeft(AltersData):
69 def my_method(self):
70 return 42
71
72 my_method.alters_data = True
73
74 class ParentLeft(GrandParentLeft):
75 def change_alters_data_method(self):
76 return 63
77
78 change_alters_data_method.alters_data = True
79
80 def sub_non_callable_method(self):
81 return 64
82
83 sub_non_callable_method.alters_data = True
84
85 class ParentRight(AltersData):
86 def other_method(self):
87 return 52
88
89 other_method.alters_data = True
90
91 class Child(ParentLeft, ParentRight):
92 def my_method(self):
93 return 101
94
95 def other_method(self):
96 return 102
97
98 def change_alters_data_method(self):
99 return 103
100
101 change_alters_data_method.alters_data = False
102
103 sub_non_callable_method = 104
104
105 class GrandChild(Child):
106 pass
107
108 child = Child()
109 self.assertIs(child.my_method.alters_data, True)
110 self.assertIs(child.other_method.alters_data, True)
111 self.assertIs(child.change_alters_data_method.alters_data, False)
112
113 grand_child = GrandChild()
114 self.assertIs(grand_child.my_method.alters_data, True)
115 self.assertIs(grand_child.other_method.alters_data, True)
116 self.assertIs(grand_child.change_alters_data_method.alters_data, False)
117
118 c = Context({"element": grand_child})
119
120 t = self.engine.from_string("{{ element.my_method }}")
121 self.assertEqual(t.render(c), "")
122 t = self.engine.from_string("{{ element.other_method }}")
123 self.assertEqual(t.render(c), "")
124 t = self.engine.from_string("{{ element.change_alters_data_method }}")
125 self.assertEqual(t.render(c), "103")
126 t = self.engine.from_string("{{ element.sub_non_callable_method }}")
127 self.assertEqual(t.render(c), "104")
128
129 def test_do_not_call(self):
130 class Doodad:
131 do_not_call_in_templates = True
132
133 def __init__(self, value):
134 self.num_calls = 0
135 self.value = value
136
137 def __call__(self):
138 self.num_calls += 1
139 return {"the_value": self.value}
140
141 my_doodad = Doodad(42)
142 c = Context({"my_doodad": my_doodad})
143
144 # Since ``my_doodad.do_not_call_in_templates`` is True, the template
145 # system will not try to call our doodad. We can access its attributes
146 # as normal, and we don't have access to the dict that it returns when
147 # called.
148 t = self.engine.from_string("{{ my_doodad.value }}")
149 self.assertEqual(t.render(c), "42")
150 t = self.engine.from_string("{{ my_doodad.the_value }}")
151 self.assertEqual(t.render(c), "")
152
153 # Double-check that the object was really never called during the
154 # template rendering.
155 self.assertEqual(my_doodad.num_calls, 0)
156
157 def test_do_not_call_and_alters_data(self):
158 # If we combine ``alters_data`` and ``do_not_call_in_templates``, the
159 # ``alters_data`` attribute will not make any difference in the
160 # template system's behavior.
161
162 class Doodad:
163 do_not_call_in_templates = True
164 alters_data = True
165
166 def __init__(self, value):
167 self.num_calls = 0
168 self.value = value
169
170 def __call__(self):
171 self.num_calls += 1
172 return {"the_value": self.value}
173
174 my_doodad = Doodad(42)
175 c = Context({"my_doodad": my_doodad})
176
177 t = self.engine.from_string("{{ my_doodad.value }}")
178 self.assertEqual(t.render(c), "42")
179 t = self.engine.from_string("{{ my_doodad.the_value }}")
180 self.assertEqual(t.render(c), "")
181
182 # Double-check that the object was really never called during the
183 # template rendering.
184 self.assertEqual(my_doodad.num_calls, 0)