File: test_partials.py

package info (click to toggle)
python-django 3%3A6.0~alpha1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 62,204 kB
  • sloc: python: 370,694; javascript: 19,376; xml: 211; makefile: 187; sh: 28
file content (604 lines) | stat: -rw-r--r-- 21,595 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import os
from unittest import mock

from django.http import HttpResponse
from django.template import (
    Context,
    NodeList,
    Origin,
    PartialTemplate,
    Template,
    TemplateDoesNotExist,
    TemplateSyntaxError,
    engines,
)
from django.template.backends.django import DjangoTemplates
from django.template.loader import render_to_string
from django.test import TestCase, override_settings
from django.urls import path, reverse

from .utils import setup

engine = engines["django"]


class PartialTagsTests(TestCase):

    def test_invalid_template_name_raises_template_does_not_exist(self):
        for template_name in [123, None, "", "#", "#name"]:
            with (
                self.subTest(template_name=template_name),
                self.assertRaisesMessage(TemplateDoesNotExist, str(template_name)),
            ):
                engine.get_template(template_name)

    def test_full_template_from_loader(self):
        template = engine.get_template("partial_examples.html")
        rendered = template.render({})

        # Check the partial was rendered twice
        self.assertEqual(2, rendered.count("TEST-PARTIAL-CONTENT"))
        self.assertEqual(1, rendered.count("INLINE-CONTENT"))

    def test_chained_exception_forwarded(self):
        with self.assertRaises(TemplateDoesNotExist) as ctx:
            engine.get_template("not_there.html#not-a-partial")

        exception = ctx.exception
        self.assertGreater(len(exception.tried), 0)
        origin, _ = exception.tried[0]
        self.assertEqual(origin.template_name, "not_there.html")

    def test_partials_use_cached_loader_when_configured(self):
        template_dir = os.path.join(os.path.dirname(__file__), "templates")
        backend = DjangoTemplates(
            {
                "NAME": "django",
                "DIRS": [template_dir],
                "APP_DIRS": False,
                "OPTIONS": {
                    "loaders": [
                        (
                            "django.template.loaders.cached.Loader",
                            ["django.template.loaders.filesystem.Loader"],
                        ),
                    ],
                },
            }
        )
        cached_loader = backend.engine.template_loaders[0]
        filesystem_loader = cached_loader.loaders[0]

        with mock.patch.object(
            filesystem_loader, "get_contents", wraps=filesystem_loader.get_contents
        ) as mock_get_contents:
            full_template = backend.get_template("partial_examples.html")
            self.assertIn("TEST-PARTIAL-CONTENT", full_template.render({}))

            partial_template = backend.get_template(
                "partial_examples.html#test-partial"
            )
            self.assertEqual(
                "TEST-PARTIAL-CONTENT", partial_template.render({}).strip()
            )

            mock_get_contents.assert_called_once()

    def test_context_available_in_response_for_partial_template(self):
        def sample_view(request):
            return HttpResponse(
                render_to_string("partial_examples.html#test-partial", {"foo": "bar"})
            )

        class PartialUrls:
            urlpatterns = [path("sample/", sample_view, name="sample-view")]

        with override_settings(ROOT_URLCONF=PartialUrls):
            response = self.client.get(reverse("sample-view"))

        self.assertContains(response, "TEST-PARTIAL-CONTENT")
        self.assertEqual(response.context.get("foo"), "bar")

    def test_response_with_multiple_parts(self):
        context = {}
        template_partials = ["partial_child.html", "partial_child.html#extra-content"]

        response_whole_content_at_once = HttpResponse(
            "".join(
                render_to_string(template_name, context)
                for template_name in template_partials
            )
        )

        response_with_multiple_writes = HttpResponse()
        for template_name in template_partials:
            response_with_multiple_writes.write(
                render_to_string(template_name, context)
            )

        response_with_generator = HttpResponse(
            render_to_string(template_name, context)
            for template_name in template_partials
        )

        for label, response in [
            ("response_whole_content_at_once", response_whole_content_at_once),
            ("response_with_multiple_writes", response_with_multiple_writes),
            ("response_with_generator", response_with_generator),
        ]:
            with self.subTest(response=label):
                self.assertIn(b"Main Content", response.content)
                self.assertIn(b"Extra Content", response.content)

    def test_partial_engine_assignment_with_real_template(self):
        template_with_partial = engine.get_template(
            "partial_examples.html#test-partial"
        )
        self.assertEqual(template_with_partial.template.engine, engine.engine)
        rendered_content = template_with_partial.render({})
        self.assertEqual("TEST-PARTIAL-CONTENT", rendered_content.strip())

    def test_template_source_warning(self):
        partial = engine.get_template("partial_examples.html#test-partial")
        with self.assertWarnsMessage(
            RuntimeWarning,
            "PartialTemplate.source is only available when template "
            "debugging is enabled.",
        ) as ctx:
            self.assertEqual(partial.template.source, "")
        self.assertEqual(ctx.filename, __file__)


class RobustPartialHandlingTests(TestCase):

    def override_get_template(self, **kwargs):
        class TemplateWithCustomAttrs:
            def __init__(self, **kwargs):
                for k, v in kwargs.items():
                    setattr(self, k, v)

            def render(self, context):
                return "rendered content"

        template = TemplateWithCustomAttrs(**kwargs)
        origin = self.id()
        return mock.patch.object(
            engine.engine,
            "find_template",
            return_value=(template, origin),
        )

    def test_template_without_extra_data_attribute(self):
        partial_name = "some_partial_name"
        with (
            self.override_get_template(),
            self.assertRaisesMessage(TemplateDoesNotExist, partial_name),
        ):
            engine.get_template(f"some_template.html#{partial_name}")

    def test_template_extract_extra_data_robust(self):
        partial_name = "some_partial_name"
        for extra_data in (
            None,
            0,
            [],
            {},
            {"wrong-key": {}},
            {"partials": None},
            {"partials": {}},
            {"partials": []},
            {"partials": 0},
        ):
            with (
                self.subTest(extra_data=extra_data),
                self.override_get_template(extra_data=extra_data),
                self.assertRaisesMessage(TemplateDoesNotExist, partial_name),
            ):
                engine.get_template(f"template.html#{partial_name}")

    def test_nested_partials_rendering_with_context(self):
        template_source = """
        {% partialdef outer inline %}
            Hello {{ name }}!
            {% partialdef inner inline %}
                Your age is {{ age }}.
            {% endpartialdef inner %}
            Nice to meet you.
        {% endpartialdef outer %}
        """
        template = Template(template_source, origin=Origin(name="template.html"))

        context = Context({"name": "Alice", "age": 25})
        rendered = template.render(context)

        self.assertIn("Hello Alice!", rendered)
        self.assertIn("Your age is 25.", rendered)
        self.assertIn("Nice to meet you.", rendered)


class FindPartialSourceTests(TestCase):

    @setup(
        {
            "partial_source_success_template": (
                "{% partialdef test-partial %}\n"
                "TEST-PARTIAL-CONTENT\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_success(self):
        template = self.engine.get_template("partial_source_success_template")
        partial_proxy = template.extra_data["partials"]["test-partial"]

        expected = """{% partialdef test-partial %}
TEST-PARTIAL-CONTENT
{% endpartialdef %}"""
        self.assertEqual(partial_proxy.source.strip(), expected.strip())

    @setup(
        {
            "partial_source_with_inline_template": (
                "{% partialdef inline-partial inline %}\n"
                "INLINE-CONTENT\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_with_inline(self):
        template = self.engine.get_template("partial_source_with_inline_template")
        partial_proxy = template.extra_data["partials"]["inline-partial"]

        expected = """{% partialdef inline-partial inline %}
INLINE-CONTENT
{% endpartialdef %}"""
        self.assertEqual(partial_proxy.source.strip(), expected.strip())

    def test_find_partial_source_fallback_cases(self):
        cases = {"None offsets": (None, None), "Out of bounds offsets": (10, 20)}
        for name, (source_start, source_end) in cases.items():
            with self.subTest(name):
                partial = PartialTemplate(
                    NodeList(),
                    Origin("test"),
                    "test",
                    source_start=source_start,
                    source_end=source_end,
                )
                result = partial.find_partial_source("nonexistent-partial")
                self.assertEqual(result, "")

    @setup(
        {
            "empty_partial_template": ("{% partialdef empty %}{% endpartialdef %}"),
        },
        debug_only=True,
    )
    def test_find_partial_source_empty_partial(self):
        template = self.engine.get_template("empty_partial_template")
        partial_proxy = template.extra_data["partials"]["empty"]

        result = partial_proxy.find_partial_source(template.source)
        self.assertEqual(result, "{% partialdef empty %}{% endpartialdef %}")

    @setup(
        {
            "consecutive_partials_template": (
                "{% partialdef empty %}{% endpartialdef %}"
                "{% partialdef other %}...{% endpartialdef %}"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_multiple_consecutive_partials(self):
        template = self.engine.get_template("consecutive_partials_template")

        empty_proxy = template.extra_data["partials"]["empty"]
        other_proxy = template.extra_data["partials"]["other"]

        empty_result = empty_proxy.find_partial_source(template.source)
        self.assertEqual(empty_result, "{% partialdef empty %}{% endpartialdef %}")

        other_result = other_proxy.find_partial_source(template.source)
        self.assertEqual(other_result, "{% partialdef other %}...{% endpartialdef %}")

    def test_partials_with_duplicate_names(self):
        test_cases = [
            (
                "nested",
                """
                {% partialdef duplicate %}{% partialdef duplicate %}
                CONTENT
                {% endpartialdef %}{% endpartialdef %}
                """,
            ),
            (
                "conditional",
                """
                {% if ... %}
                  {% partialdef duplicate %}
                  CONTENT
                  {% endpartialdef %}
                {% else %}
                  {% partialdef duplicate %}
                  OTHER-CONTENT
                  {% endpartialdef %}
                {% endif %}
                """,
            ),
        ]

        for test_name, template_source in test_cases:
            with self.subTest(test_name=test_name):
                with self.assertRaisesMessage(
                    TemplateSyntaxError,
                    "Partial 'duplicate' is already defined in the "
                    "'template.html' template.",
                ):
                    Template(template_source, origin=Origin(name="template.html"))

    @setup(
        {
            "named_end_tag_template": (
                "{% partialdef thing %}CONTENT{% endpartialdef thing %}"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_supports_named_end_tag(self):
        template = self.engine.get_template("named_end_tag_template")
        partial_proxy = template.extra_data["partials"]["thing"]

        result = partial_proxy.find_partial_source(template.source)
        self.assertEqual(
            result, "{% partialdef thing %}CONTENT{% endpartialdef thing %}"
        )

    @setup(
        {
            "nested_partials_basic_template": (
                "{% partialdef outer %}"
                "{% partialdef inner %}...{% endpartialdef %}"
                "{% endpartialdef %}"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_supports_nested_partials(self):
        template = self.engine.get_template("nested_partials_basic_template")

        empty_proxy = template.extra_data["partials"]["outer"]
        other_proxy = template.extra_data["partials"]["inner"]

        outer_result = empty_proxy.find_partial_source(template.source)
        self.assertEqual(
            outer_result,
            (
                "{% partialdef outer %}{% partialdef inner %}"
                "...{% endpartialdef %}{% endpartialdef %}"
            ),
        )

        inner_result = other_proxy.find_partial_source(template.source)
        self.assertEqual(inner_result, "{% partialdef inner %}...{% endpartialdef %}")

    @setup(
        {
            "nested_partials_named_end_template": (
                "{% partialdef outer %}"
                "{% partialdef inner %}...{% endpartialdef inner %}"
                "{% endpartialdef outer %}"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_supports_nested_partials_and_named_end_tags(self):
        template = self.engine.get_template("nested_partials_named_end_template")

        empty_proxy = template.extra_data["partials"]["outer"]
        other_proxy = template.extra_data["partials"]["inner"]

        outer_result = empty_proxy.find_partial_source(template.source)
        self.assertEqual(
            outer_result,
            (
                "{% partialdef outer %}{% partialdef inner %}"
                "...{% endpartialdef inner %}{% endpartialdef outer %}"
            ),
        )

        inner_result = other_proxy.find_partial_source(template.source)
        self.assertEqual(
            inner_result, "{% partialdef inner %}...{% endpartialdef inner %}"
        )

    @setup(
        {
            "nested_partials_mixed_end_1_template": (
                "{% partialdef outer %}"
                "{% partialdef inner %}...{% endpartialdef %}"
                "{% endpartialdef outer %}"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_supports_nested_partials_and_mixed_end_tags_1(self):
        template = self.engine.get_template("nested_partials_mixed_end_1_template")

        empty_proxy = template.extra_data["partials"]["outer"]
        other_proxy = template.extra_data["partials"]["inner"]

        outer_result = empty_proxy.find_partial_source(template.source)
        self.assertEqual(
            outer_result,
            (
                "{% partialdef outer %}{% partialdef inner %}"
                "...{% endpartialdef %}{% endpartialdef outer %}"
            ),
        )

        inner_result = other_proxy.find_partial_source(template.source)
        self.assertEqual(inner_result, "{% partialdef inner %}...{% endpartialdef %}")

    @setup(
        {
            "nested_partials_mixed_end_2_template": (
                "{% partialdef outer %}"
                "{% partialdef inner %}...{% endpartialdef inner %}"
                "{% endpartialdef %}"
            ),
        },
        debug_only=True,
    )
    def test_find_partial_source_supports_nested_partials_and_mixed_end_tags_2(self):
        template = self.engine.get_template("nested_partials_mixed_end_2_template")

        empty_proxy = template.extra_data["partials"]["outer"]
        other_proxy = template.extra_data["partials"]["inner"]

        outer_result = empty_proxy.find_partial_source(template.source)
        self.assertEqual(
            outer_result,
            (
                "{% partialdef outer %}{% partialdef inner %}"
                "...{% endpartialdef inner %}{% endpartialdef %}"
            ),
        )

        inner_result = other_proxy.find_partial_source(template.source)
        self.assertEqual(
            inner_result, "{% partialdef inner %}...{% endpartialdef inner %}"
        )

    @setup(
        {
            "partial_embedded_in_verbatim": (
                "{% verbatim %}\n"
                "{% partialdef testing-name %}\n"
                "<p>Should be ignored</p>"
                "{% endpartialdef testing-name %}\n"
                "{% endverbatim %}\n"
                "{% partialdef testing-name %}\n"
                "<p>Content</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_partial_template_embedded_in_verbatim(self):
        template = self.engine.get_template("partial_embedded_in_verbatim")
        partial_template = template.extra_data["partials"]["testing-name"]
        self.assertEqual(
            partial_template.source,
            "{% partialdef testing-name %}\n<p>Content</p>\n{% endpartialdef %}",
        )

    @setup(
        {
            "partial_debug_source": (
                "{% partialdef testing-name %}\n"
                "<p>Content</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_partial_source_uses_offsets_in_debug(self):
        template = self.engine.get_template("partial_debug_source")
        partial_template = template.extra_data["partials"]["testing-name"]

        self.assertEqual(partial_template._source_start, 0)
        self.assertEqual(partial_template._source_end, 64)
        expected = template.source[
            partial_template._source_start : partial_template._source_end
        ]
        self.assertEqual(partial_template.source, expected)

    @setup(
        {
            "partial_embedded_in_named_verbatim": (
                "{% verbatim block1 %}\n"
                "{% partialdef testing-name %}\n"
                "{% endverbatim block1 %}\n"
                "{% partialdef testing-name %}\n"
                "<p>Named Content</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_partial_template_embedded_in_named_verbatim(self):
        template = self.engine.get_template("partial_embedded_in_named_verbatim")
        partial_template = template.extra_data["partials"]["testing-name"]
        self.assertEqual(
            "{% partialdef testing-name %}\n<p>Named Content</p>\n{% endpartialdef %}",
            partial_template.source,
        )

    @setup(
        {
            "partial_embedded_in_comment_block": (
                "{% comment %}\n"
                "{% partialdef testing-name %}\n"
                "{% endcomment %}\n"
                "{% partialdef testing-name %}\n"
                "<p>Comment Content</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_partial_template_embedded_in_comment_block(self):
        template = self.engine.get_template("partial_embedded_in_comment_block")
        partial_template = template.extra_data["partials"]["testing-name"]
        self.assertEqual(
            partial_template.source,
            "{% partialdef testing-name %}\n"
            "<p>Comment Content</p>\n"
            "{% endpartialdef %}",
        )

    @setup(
        {
            "partial_embedded_in_inline_comment": (
                "{# {% partialdef testing-name %} #}\n"
                "{% partialdef testing-name %}\n"
                "<p>Inline Comment Content</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_partial_template_embedded_in_inline_comment(self):
        template = self.engine.get_template("partial_embedded_in_inline_comment")
        partial_template = template.extra_data["partials"]["testing-name"]
        self.assertEqual(
            partial_template.source,
            "{% partialdef testing-name %}\n"
            "<p>Inline Comment Content</p>\n"
            "{% endpartialdef %}",
        )

    @setup(
        {
            "partial_contains_fake_end_inside_verbatim": (
                "{% partialdef testing-name %}\n"
                "{% verbatim %}{% endpartialdef %}{% endverbatim %}\n"
                "<p>Body</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        debug_only=True,
    )
    def test_partial_template_contains_fake_end_inside_verbatim(self):
        template = self.engine.get_template("partial_contains_fake_end_inside_verbatim")
        partial_template = template.extra_data["partials"]["testing-name"]
        self.assertEqual(
            partial_template.source,
            "{% partialdef testing-name %}\n"
            "{% verbatim %}{% endpartialdef %}{% endverbatim %}\n"
            "<p>Body</p>\n"
            "{% endpartialdef %}",
        )