File: test_templates.py

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (215 lines) | stat: -rw-r--r-- 7,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from copy import deepcopy
from itertools import chain

from django.core.checks import Error, Warning
from django.core.checks.templates import check_templates
from django.template import engines
from django.template.backends.base import BaseEngine
from django.test import SimpleTestCase
from django.test.utils import override_settings


class ErrorEngine(BaseEngine):
    def __init__(self, params):
        params.pop("OPTIONS")
        super().__init__(params)

    def check(self, **kwargs):
        return [Error("Example")]


class CheckTemplatesTests(SimpleTestCase):
    @override_settings(
        TEMPLATES=[
            {"BACKEND": f"{__name__}.{ErrorEngine.__qualname__}", "NAME": "backend_1"},
            {"BACKEND": f"{__name__}.{ErrorEngine.__qualname__}", "NAME": "backend_2"},
        ]
    )
    def test_errors_aggregated(self):
        errors = check_templates(None)
        self.assertEqual(errors, [Error("Example")] * 2)


class CheckTemplateStringIfInvalidTest(SimpleTestCase):
    TEMPLATES_STRING_IF_INVALID = [
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "NAME": "backend_1",
            "OPTIONS": {
                "string_if_invalid": False,
            },
        },
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "NAME": "backend_2",
            "OPTIONS": {
                "string_if_invalid": 42,
            },
        },
    ]

    def _get_error_for_engine(self, engine):
        value = engine.engine.string_if_invalid
        return Error(
            "'string_if_invalid' in TEMPLATES OPTIONS must be a string but got: %r "
            "(%s)." % (value, type(value)),
            obj=engine,
            id="templates.E002",
        )

    def _check_engines(self, engines):
        return list(
            chain.from_iterable(e._check_string_if_invalid_is_string() for e in engines)
        )

    @override_settings(TEMPLATES=TEMPLATES_STRING_IF_INVALID)
    def test_string_if_invalid_not_string(self):
        _engines = engines.all()
        errors = [
            self._get_error_for_engine(_engines[0]),
            self._get_error_for_engine(_engines[1]),
        ]
        self.assertEqual(self._check_engines(_engines), errors)

    def test_string_if_invalid_first_is_string(self):
        TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
        TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "test"
        with self.settings(TEMPLATES=TEMPLATES):
            _engines = engines.all()
            errors = [self._get_error_for_engine(_engines[1])]
            self.assertEqual(self._check_engines(_engines), errors)

    def test_string_if_invalid_both_are_strings(self):
        TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
        TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "test"
        TEMPLATES[1]["OPTIONS"]["string_if_invalid"] = "test"
        with self.settings(TEMPLATES=TEMPLATES):
            self.assertEqual(self._check_engines(engines.all()), [])

    def test_string_if_invalid_not_specified(self):
        TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
        del TEMPLATES[1]["OPTIONS"]["string_if_invalid"]
        with self.settings(TEMPLATES=TEMPLATES):
            _engines = engines.all()
            errors = [self._get_error_for_engine(_engines[0])]
            self.assertEqual(self._check_engines(_engines), errors)


class CheckTemplateTagLibrariesWithSameName(SimpleTestCase):
    def get_settings(self, module_name, module_path, name="django"):
        return {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "NAME": name,
            "OPTIONS": {
                "libraries": {
                    module_name: f"check_framework.template_test_apps.{module_path}",
                },
            },
        }

    def _get_error_for_engine(self, engine, modules):
        return Warning(
            f"'same_tags' is used for multiple template tag modules: {modules}",
            obj=engine,
            id="templates.W003",
        )

    def _check_engines(self, engines):
        return list(
            chain.from_iterable(
                e._check_for_template_tags_with_the_same_name() for e in engines
            )
        )

    @override_settings(
        INSTALLED_APPS=[
            "check_framework.template_test_apps.same_tags_app_1",
            "check_framework.template_test_apps.same_tags_app_2",
        ]
    )
    def test_template_tags_with_same_name(self):
        _engines = engines.all()
        modules = (
            "'check_framework.template_test_apps.same_tags_app_1.templatetags"
            ".same_tags', 'check_framework.template_test_apps.same_tags_app_2"
            ".templatetags.same_tags'"
        )
        errors = [self._get_error_for_engine(_engines[0], modules)]
        self.assertEqual(self._check_engines(_engines), errors)

    def test_template_tags_for_separate_backends(self):
        # The "libraries" names are the same, but the backends are different.
        with self.settings(
            TEMPLATES=[
                self.get_settings(
                    "same_tags",
                    "same_tags_app_1.templatetags.same_tags",
                    name="backend_1",
                ),
                self.get_settings(
                    "same_tags",
                    "same_tags_app_2.templatetags.same_tags",
                    name="backend_2",
                ),
            ]
        ):
            self.assertEqual(self._check_engines(engines.all()), [])

    @override_settings(
        INSTALLED_APPS=["check_framework.template_test_apps.same_tags_app_1"]
    )
    def test_template_tags_same_library_in_installed_apps_libraries(self):
        with self.settings(
            TEMPLATES=[
                self.get_settings(
                    "same_tags", "same_tags_app_1.templatetags.same_tags"
                ),
            ]
        ):
            self.assertEqual(self._check_engines(engines.all()), [])

    @override_settings(
        INSTALLED_APPS=["check_framework.template_test_apps.same_tags_app_1"]
    )
    def test_template_tags_with_same_library_name_and_module_name(self):
        modules = (
            "'check_framework.template_test_apps.different_tags_app.templatetags"
            ".different_tags', 'check_framework.template_test_apps.same_tags_app_1"
            ".templatetags.same_tags'"
        )
        with self.settings(
            TEMPLATES=[
                self.get_settings(
                    "same_tags", "different_tags_app.templatetags.different_tags"
                ),
            ]
        ):
            _engines = engines.all()
            errors = [self._get_error_for_engine(_engines[0], modules)]
            self.assertEqual(self._check_engines(_engines), errors)

    def test_template_tags_with_different_library_name(self):
        with self.settings(
            TEMPLATES=[
                self.get_settings(
                    "same_tags",
                    "same_tags_app_1.templatetags.same_tags",
                    name="backend_1",
                ),
                self.get_settings(
                    "not_same_tags",
                    "same_tags_app_2.templatetags.same_tags",
                    name="backend_2",
                ),
            ]
        ):
            self.assertEqual(self._check_engines(engines.all()), [])

    @override_settings(
        INSTALLED_APPS=[
            "check_framework.template_test_apps.same_tags_app_1",
            "check_framework.template_test_apps.different_tags_app",
        ]
    )
    def test_template_tags_with_different_name(self):
        self.assertEqual(self._check_engines(engines.all()), [])