File: test_engine.py

package info (click to toggle)
python-django 1.8.18-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 41,628 kB
  • sloc: python: 189,488; xml: 695; makefile: 194; sh: 169; sql: 11
file content (125 lines) | stat: -rw-r--r-- 4,364 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
import os

from django.template import Context
from django.template.engine import Engine
from django.test import SimpleTestCase, ignore_warnings
from django.utils.deprecation import RemovedInDjango110Warning

from .utils import ROOT, TEMPLATE_DIR

OTHER_DIR = os.path.join(ROOT, 'other_templates')


@ignore_warnings(category=RemovedInDjango110Warning)
class DeprecatedRenderToStringTest(SimpleTestCase):

    def setUp(self):
        self.engine = Engine(dirs=[TEMPLATE_DIR])

    def test_basic_context(self):
        self.assertEqual(
            self.engine.render_to_string('test_context.html', {'obj': 'test'}),
            'obj:test\n',
        )

    def test_existing_context_kept_clean(self):
        context = Context({'obj': 'before'})
        output = self.engine.render_to_string(
            'test_context.html', {'obj': 'after'}, context_instance=context,
        )
        self.assertEqual(output, 'obj:after\n')
        self.assertEqual(context['obj'], 'before')

    def test_no_empty_dict_pushed_to_stack(self):
        """
        #21741 -- An empty dict should not be pushed to the context stack when
        render_to_string is called without a context argument.
        """

        # The stack should have a length of 1, corresponding to the builtins
        self.assertEqual(
            '1',
            self.engine.render_to_string('test_context_stack.html').strip(),
        )
        self.assertEqual(
            '1',
            self.engine.render_to_string(
                'test_context_stack.html',
                context_instance=Context()
            ).strip(),
        )


class LoaderTests(SimpleTestCase):

    def test_debug_nodelist_name(self):
        engine = Engine(dirs=[TEMPLATE_DIR], debug=True)
        template_name = 'index.html'
        template = engine.get_template(template_name)
        name = template.nodelist[0].source[0].name
        self.assertTrue(name.endswith(template_name))

    def test_origin(self):
        engine = Engine(dirs=[TEMPLATE_DIR], debug=True)
        template = engine.get_template('index.html')
        self.assertEqual(template.origin.loadname, 'index.html')

    def test_origin_debug_false(self):
        engine = Engine(dirs=[TEMPLATE_DIR], debug=False)
        template = engine.get_template('index.html')
        self.assertEqual(template.origin, None)

    def test_loader_priority(self):
        """
        #21460 -- Check that the order of template loader works.
        """
        loaders = [
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',
        ]
        engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
        template = engine.get_template('priority/foo.html')
        self.assertEqual(template.render(Context()), 'priority\n')

    def test_cached_loader_priority(self):
        """
        Check that the order of template loader works. Refs #21460.
        """
        loaders = [
            ('django.template.loaders.cached.Loader', [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ]),
        ]
        engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)

        template = engine.get_template('priority/foo.html')
        self.assertEqual(template.render(Context()), 'priority\n')

        template = engine.get_template('priority/foo.html')
        self.assertEqual(template.render(Context()), 'priority\n')


@ignore_warnings(category=RemovedInDjango110Warning)
class TemplateDirsOverrideTests(SimpleTestCase):
    DIRS = ((OTHER_DIR, ), [OTHER_DIR])

    def setUp(self):
        self.engine = Engine()

    def test_render_to_string(self):
        for dirs in self.DIRS:
            self.assertEqual(
                self.engine.render_to_string('test_dirs.html', dirs=dirs),
                'spam eggs\n',
            )

    def test_get_template(self):
        for dirs in self.DIRS:
            template = self.engine.get_template('test_dirs.html', dirs=dirs)
            self.assertEqual(template.render(Context()), 'spam eggs\n')

    def test_select_template(self):
        for dirs in self.DIRS:
            template = self.engine.select_template(['test_dirs.html'], dirs=dirs)
            self.assertEqual(template.render(Context()), 'spam eggs\n')