Debian Codegraph @

tests/check_framework/test_templates.py

raw
1 from copy import deepcopy
2 from itertools import chain
3
4 from django.core.checks import Error, Warning
5 from django.core.checks.templates import check_templates
6 from django.template import engines
7 from django.template.backends.base import BaseEngine
8 from django.test import SimpleTestCase
9 from django.test.utils import override_settings
10
11
12 class ErrorEngine(BaseEngine):
13 def __init__(self, params):
14 params.pop("OPTIONS")
15 super().__init__(params)
16
17 def check(self, **kwargs):
18 return [Error("Example")]
19
20
21 class CheckTemplatesTests(SimpleTestCase):
22 @override_settings(
23 TEMPLATES=[
24 {"BACKEND": f"{__name__}.{ErrorEngine.__qualname__}", "NAME": "backend_1"},
25 {"BACKEND": f"{__name__}.{ErrorEngine.__qualname__}", "NAME": "backend_2"},
26 ]
27 )
28 def test_errors_aggregated(self):
29 errors = check_templates(None)
30 self.assertEqual(errors, [Error("Example")] * 2)
31
32
33 class CheckTemplateStringIfInvalidTest(SimpleTestCase):
34 TEMPLATES_STRING_IF_INVALID = [
35 {
36 "BACKEND": "django.template.backends.django.DjangoTemplates",
37 "NAME": "backend_1",
38 "OPTIONS": {
39 "string_if_invalid": False,
40 },
41 },
42 {
43 "BACKEND": "django.template.backends.django.DjangoTemplates",
44 "NAME": "backend_2",
45 "OPTIONS": {
46 "string_if_invalid": 42,
47 },
48 },
49 ]
50
51 def _get_error_for_engine(self, engine):
52 value = engine.engine.string_if_invalid
53 return Error(
54 "'string_if_invalid' in TEMPLATES OPTIONS must be a string but got: %r "
55 "(%s)." % (value, type(value)),
56 obj=engine,
57 id="templates.E002",
58 )
59
60 def _check_engines(self, engines):
61 return list(
62 chain.from_iterable(e._check_string_if_invalid_is_string() for e in engines)
63 )
64
65 @override_settings(TEMPLATES=TEMPLATES_STRING_IF_INVALID)
66 def test_string_if_invalid_not_string(self):
67 _engines = engines.all()
68 errors = [
69 self._get_error_for_engine(_engines[0]),
70 self._get_error_for_engine(_engines[1]),
71 ]
72 self.assertEqual(self._check_engines(_engines), errors)
73
74 def test_string_if_invalid_first_is_string(self):
75 TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
76 TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "test"
77 with self.settings(TEMPLATES=TEMPLATES):
78 _engines = engines.all()
79 errors = [self._get_error_for_engine(_engines[1])]
80 self.assertEqual(self._check_engines(_engines), errors)
81
82 def test_string_if_invalid_both_are_strings(self):
83 TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
84 TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "test"
85 TEMPLATES[1]["OPTIONS"]["string_if_invalid"] = "test"
86 with self.settings(TEMPLATES=TEMPLATES):
87 self.assertEqual(self._check_engines(engines.all()), [])
88
89 def test_string_if_invalid_not_specified(self):
90 TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
91 del TEMPLATES[1]["OPTIONS"]["string_if_invalid"]
92 with self.settings(TEMPLATES=TEMPLATES):
93 _engines = engines.all()
94 errors = [self._get_error_for_engine(_engines[0])]
95 self.assertEqual(self._check_engines(_engines), errors)
96
97
98 class CheckTemplateTagLibrariesWithSameName(SimpleTestCase):
99 def get_settings(self, module_name, module_path, name="django"):
100 return {
101 "BACKEND": "django.template.backends.django.DjangoTemplates",
102 "NAME": name,
103 "OPTIONS": {
104 "libraries": {
105 module_name: f"check_framework.template_test_apps.{module_path}",
106 },
107 },
108 }
109
110 def _get_error_for_engine(self, engine, modules):
111 return Warning(
112 f"'same_tags' is used for multiple template tag modules: {modules}",
113 obj=engine,
114 id="templates.W003",
115 )
116
117 def _check_engines(self, engines):
118 return list(
119 chain.from_iterable(
120 e._check_for_template_tags_with_the_same_name() for e in engines
121 )
122 )
123
124 @override_settings(
125 INSTALLED_APPS=[
126 "check_framework.template_test_apps.same_tags_app_1",
127 "check_framework.template_test_apps.same_tags_app_2",
128 ]
129 )
130 def test_template_tags_with_same_name(self):
131 _engines = engines.all()
132 modules = (
133 "'check_framework.template_test_apps.same_tags_app_1.templatetags"
134 ".same_tags', 'check_framework.template_test_apps.same_tags_app_2"
135 ".templatetags.same_tags'"
136 )
137 errors = [self._get_error_for_engine(_engines[0], modules)]
138 self.assertEqual(self._check_engines(_engines), errors)
139
140 def test_template_tags_for_separate_backends(self):
141 # The "libraries" names are the same, but the backends are different.
142 with self.settings(
143 TEMPLATES=[
144 self.get_settings(
145 "same_tags",
146 "same_tags_app_1.templatetags.same_tags",
147 name="backend_1",
148 ),
149 self.get_settings(
150 "same_tags",
151 "same_tags_app_2.templatetags.same_tags",
152 name="backend_2",
153 ),
154 ]
155 ):
156 self.assertEqual(self._check_engines(engines.all()), [])
157
158 @override_settings(
159 INSTALLED_APPS=["check_framework.template_test_apps.same_tags_app_1"]
160 )
161 def test_template_tags_same_library_in_installed_apps_libraries(self):
162 with self.settings(
163 TEMPLATES=[
164 self.get_settings(
165 "same_tags", "same_tags_app_1.templatetags.same_tags"
166 ),
167 ]
168 ):
169 self.assertEqual(self._check_engines(engines.all()), [])
170
171 @override_settings(
172 INSTALLED_APPS=["check_framework.template_test_apps.same_tags_app_1"]
173 )
174 def test_template_tags_with_same_library_name_and_module_name(self):
175 modules = (
176 "'check_framework.template_test_apps.different_tags_app.templatetags"
177 ".different_tags', 'check_framework.template_test_apps.same_tags_app_1"
178 ".templatetags.same_tags'"
179 )
180 with self.settings(
181 TEMPLATES=[
182 self.get_settings(
183 "same_tags", "different_tags_app.templatetags.different_tags"
184 ),
185 ]
186 ):
187 _engines = engines.all()
188 errors = [self._get_error_for_engine(_engines[0], modules)]
189 self.assertEqual(self._check_engines(_engines), errors)
190
191 def test_template_tags_with_different_library_name(self):
192 with self.settings(
193 TEMPLATES=[
194 self.get_settings(
195 "same_tags",
196 "same_tags_app_1.templatetags.same_tags",
197 name="backend_1",
198 ),
199 self.get_settings(
200 "not_same_tags",
201 "same_tags_app_2.templatetags.same_tags",
202 name="backend_2",
203 ),
204 ]
205 ):
206 self.assertEqual(self._check_engines(engines.all()), [])
207
208 @override_settings(
209 INSTALLED_APPS=[
210 "check_framework.template_test_apps.same_tags_app_1",
211 "check_framework.template_test_apps.different_tags_app",
212 ]
213 )
214 def test_template_tags_with_different_name(self):
215 self.assertEqual(self._check_engines(engines.all()), [])