Debian Codegraph @

tests/template_loader/tests.py

raw
1 from django.template import TemplateDoesNotExist
2 from django.template.loader import get_template, render_to_string, select_template
3 from django.test import SimpleTestCase, override_settings
4 from django.test.client import RequestFactory
5
6
7 @override_settings(
8 TEMPLATES=[
9 {
10 "BACKEND": "django.template.backends.dummy.TemplateStrings",
11 "APP_DIRS": True,
12 },
13 {
14 "BACKEND": "django.template.backends.django.DjangoTemplates",
15 "OPTIONS": {
16 "context_processors": [
17 "django.template.context_processors.request",
18 ],
19 "loaders": [
20 "django.template.loaders.filesystem.Loader",
21 "django.template.loaders.app_directories.Loader",
22 ],
23 },
24 },
25 ]
26 )
27 class TemplateLoaderTests(SimpleTestCase):
28 def test_get_template_first_engine(self):
29 template = get_template("template_loader/hello.html")
30 self.assertEqual(template.render(), "Hello! (template strings)\n")
31
32 def test_get_template_second_engine(self):
33 template = get_template("template_loader/goodbye.html")
34 self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
35
36 def test_get_template_using_engine(self):
37 template = get_template("template_loader/hello.html", using="django")
38 self.assertEqual(template.render(), "Hello! (Django templates)\n")
39
40 def test_get_template_not_found(self):
41 with self.assertRaises(TemplateDoesNotExist) as e:
42 get_template("template_loader/unknown.html")
43 self.assertEqual(
44 e.exception.chain[-1].tried[0][0].template_name,
45 "template_loader/unknown.html",
46 )
47 self.assertEqual(e.exception.chain[-1].backend.name, "django")
48
49 def test_select_template_first_engine(self):
50 template = select_template(
51 ["template_loader/unknown.html", "template_loader/hello.html"]
52 )
53 self.assertEqual(template.render(), "Hello! (template strings)\n")
54
55 def test_select_template_second_engine(self):
56 template = select_template(
57 ["template_loader/unknown.html", "template_loader/goodbye.html"]
58 )
59 self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
60
61 def test_select_template_using_engine(self):
62 template = select_template(
63 ["template_loader/unknown.html", "template_loader/hello.html"],
64 using="django",
65 )
66 self.assertEqual(template.render(), "Hello! (Django templates)\n")
67
68 def test_select_template_empty(self):
69 with self.assertRaises(TemplateDoesNotExist):
70 select_template([])
71
72 def test_select_template_string(self):
73 with self.assertRaisesMessage(
74 TypeError,
75 "select_template() takes an iterable of template names but got a "
76 "string: 'template_loader/hello.html'. Use get_template() if you "
77 "want to load a single template by name.",
78 ):
79 select_template("template_loader/hello.html")
80
81 def test_select_template_not_found(self):
82 with self.assertRaises(TemplateDoesNotExist) as e:
83 select_template(
84 ["template_loader/unknown.html", "template_loader/missing.html"]
85 )
86 self.assertEqual(
87 e.exception.chain[0].tried[0][0].template_name,
88 "template_loader/unknown.html",
89 )
90 self.assertEqual(e.exception.chain[0].backend.name, "dummy")
91 self.assertEqual(
92 e.exception.chain[-1].tried[0][0].template_name,
93 "template_loader/missing.html",
94 )
95 self.assertEqual(e.exception.chain[-1].backend.name, "django")
96
97 def test_select_template_tries_all_engines_before_names(self):
98 template = select_template(
99 ["template_loader/goodbye.html", "template_loader/hello.html"]
100 )
101 self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
102
103 def test_render_to_string_first_engine(self):
104 content = render_to_string("template_loader/hello.html")
105 self.assertEqual(content, "Hello! (template strings)\n")
106
107 def test_render_to_string_second_engine(self):
108 content = render_to_string("template_loader/goodbye.html")
109 self.assertEqual(content, "Goodbye! (Django templates)\n")
110
111 def test_render_to_string_with_request(self):
112 request = RequestFactory().get("/foobar/")
113 content = render_to_string("template_loader/request.html", request=request)
114 self.assertEqual(content, "/foobar/\n")
115
116 def test_render_to_string_using_engine(self):
117 content = render_to_string("template_loader/hello.html", using="django")
118 self.assertEqual(content, "Hello! (Django templates)\n")
119
120 def test_render_to_string_not_found(self):
121 with self.assertRaises(TemplateDoesNotExist) as e:
122 render_to_string("template_loader/unknown.html")
123 self.assertEqual(
124 e.exception.chain[-1].tried[0][0].template_name,
125 "template_loader/unknown.html",
126 )
127 self.assertEqual(e.exception.chain[-1].backend.name, "django")
128
129 def test_render_to_string_with_list_first_engine(self):
130 content = render_to_string(
131 ["template_loader/unknown.html", "template_loader/hello.html"]
132 )
133 self.assertEqual(content, "Hello! (template strings)\n")
134
135 def test_render_to_string_with_list_second_engine(self):
136 content = render_to_string(
137 ["template_loader/unknown.html", "template_loader/goodbye.html"]
138 )
139 self.assertEqual(content, "Goodbye! (Django templates)\n")
140
141 def test_render_to_string_with_list_using_engine(self):
142 content = render_to_string(
143 ["template_loader/unknown.html", "template_loader/hello.html"],
144 using="django",
145 )
146 self.assertEqual(content, "Hello! (Django templates)\n")
147
148 def test_render_to_string_with_list_empty(self):
149 with self.assertRaises(TemplateDoesNotExist):
150 render_to_string([])
151
152 def test_render_to_string_with_list_not_found(self):
153 with self.assertRaises(TemplateDoesNotExist) as e:
154 render_to_string(
155 ["template_loader/unknown.html", "template_loader/missing.html"]
156 )
157 self.assertEqual(
158 e.exception.chain[0].tried[0][0].template_name,
159 "template_loader/unknown.html",
160 )
161 self.assertEqual(e.exception.chain[0].backend.name, "dummy")
162 self.assertEqual(
163 e.exception.chain[1].tried[0][0].template_name,
164 "template_loader/unknown.html",
165 )
166 self.assertEqual(e.exception.chain[1].backend.name, "django")
167 self.assertEqual(
168 e.exception.chain[2].tried[0][0].template_name,
169 "template_loader/missing.html",
170 )
171 self.assertEqual(e.exception.chain[2].backend.name, "dummy")
172 self.assertEqual(
173 e.exception.chain[3].tried[0][0].template_name,
174 "template_loader/missing.html",
175 )
176 self.assertEqual(e.exception.chain[3].backend.name, "django")
177
178 def test_render_to_string_with_list_tries_all_engines_before_names(self):
179 content = render_to_string(
180 ["template_loader/goodbye.html", "template_loader/hello.html"]
181 )
182 self.assertEqual(content, "Goodbye! (Django templates)\n")