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
|
from django.template import TemplateDoesNotExist
from django.template.loader import get_template, render_to_string, select_template
from django.test import SimpleTestCase, override_settings
from django.test.client import RequestFactory
@override_settings(
TEMPLATES=[
{
"BACKEND": "django.template.backends.dummy.TemplateStrings",
"APP_DIRS": True,
},
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
],
"loaders": [
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
},
},
]
)
class TemplateLoaderTests(SimpleTestCase):
def test_get_template_first_engine(self):
template = get_template("template_loader/hello.html")
self.assertEqual(template.render(), "Hello! (template strings)\n")
def test_get_template_second_engine(self):
template = get_template("template_loader/goodbye.html")
self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
def test_get_template_using_engine(self):
template = get_template("template_loader/hello.html", using="django")
self.assertEqual(template.render(), "Hello! (Django templates)\n")
def test_get_template_not_found(self):
with self.assertRaises(TemplateDoesNotExist) as e:
get_template("template_loader/unknown.html")
self.assertEqual(
e.exception.chain[-1].tried[0][0].template_name,
"template_loader/unknown.html",
)
self.assertEqual(e.exception.chain[-1].backend.name, "django")
def test_select_template_first_engine(self):
template = select_template(
["template_loader/unknown.html", "template_loader/hello.html"]
)
self.assertEqual(template.render(), "Hello! (template strings)\n")
def test_select_template_second_engine(self):
template = select_template(
["template_loader/unknown.html", "template_loader/goodbye.html"]
)
self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
def test_select_template_using_engine(self):
template = select_template(
["template_loader/unknown.html", "template_loader/hello.html"],
using="django",
)
self.assertEqual(template.render(), "Hello! (Django templates)\n")
def test_select_template_empty(self):
with self.assertRaises(TemplateDoesNotExist):
select_template([])
def test_select_template_string(self):
with self.assertRaisesMessage(
TypeError,
"select_template() takes an iterable of template names but got a "
"string: 'template_loader/hello.html'. Use get_template() if you "
"want to load a single template by name.",
):
select_template("template_loader/hello.html")
def test_select_template_not_found(self):
with self.assertRaises(TemplateDoesNotExist) as e:
select_template(
["template_loader/unknown.html", "template_loader/missing.html"]
)
self.assertEqual(
e.exception.chain[0].tried[0][0].template_name,
"template_loader/unknown.html",
)
self.assertEqual(e.exception.chain[0].backend.name, "dummy")
self.assertEqual(
e.exception.chain[-1].tried[0][0].template_name,
"template_loader/missing.html",
)
self.assertEqual(e.exception.chain[-1].backend.name, "django")
def test_select_template_tries_all_engines_before_names(self):
template = select_template(
["template_loader/goodbye.html", "template_loader/hello.html"]
)
self.assertEqual(template.render(), "Goodbye! (Django templates)\n")
def test_render_to_string_first_engine(self):
content = render_to_string("template_loader/hello.html")
self.assertEqual(content, "Hello! (template strings)\n")
def test_render_to_string_second_engine(self):
content = render_to_string("template_loader/goodbye.html")
self.assertEqual(content, "Goodbye! (Django templates)\n")
def test_render_to_string_with_request(self):
request = RequestFactory().get("/foobar/")
content = render_to_string("template_loader/request.html", request=request)
self.assertEqual(content, "/foobar/\n")
def test_render_to_string_using_engine(self):
content = render_to_string("template_loader/hello.html", using="django")
self.assertEqual(content, "Hello! (Django templates)\n")
def test_render_to_string_not_found(self):
with self.assertRaises(TemplateDoesNotExist) as e:
render_to_string("template_loader/unknown.html")
self.assertEqual(
e.exception.chain[-1].tried[0][0].template_name,
"template_loader/unknown.html",
)
self.assertEqual(e.exception.chain[-1].backend.name, "django")
def test_render_to_string_with_list_first_engine(self):
content = render_to_string(
["template_loader/unknown.html", "template_loader/hello.html"]
)
self.assertEqual(content, "Hello! (template strings)\n")
def test_render_to_string_with_list_second_engine(self):
content = render_to_string(
["template_loader/unknown.html", "template_loader/goodbye.html"]
)
self.assertEqual(content, "Goodbye! (Django templates)\n")
def test_render_to_string_with_list_using_engine(self):
content = render_to_string(
["template_loader/unknown.html", "template_loader/hello.html"],
using="django",
)
self.assertEqual(content, "Hello! (Django templates)\n")
def test_render_to_string_with_list_empty(self):
with self.assertRaises(TemplateDoesNotExist):
render_to_string([])
def test_render_to_string_with_list_not_found(self):
with self.assertRaises(TemplateDoesNotExist) as e:
render_to_string(
["template_loader/unknown.html", "template_loader/missing.html"]
)
self.assertEqual(
e.exception.chain[0].tried[0][0].template_name,
"template_loader/unknown.html",
)
self.assertEqual(e.exception.chain[0].backend.name, "dummy")
self.assertEqual(
e.exception.chain[1].tried[0][0].template_name,
"template_loader/unknown.html",
)
self.assertEqual(e.exception.chain[1].backend.name, "django")
self.assertEqual(
e.exception.chain[2].tried[0][0].template_name,
"template_loader/missing.html",
)
self.assertEqual(e.exception.chain[2].backend.name, "dummy")
self.assertEqual(
e.exception.chain[3].tried[0][0].template_name,
"template_loader/missing.html",
)
self.assertEqual(e.exception.chain[3].backend.name, "django")
def test_render_to_string_with_list_tries_all_engines_before_names(self):
content = render_to_string(
["template_loader/goodbye.html", "template_loader/hello.html"]
)
self.assertEqual(content, "Goodbye! (Django templates)\n")
|