| 1 |
import os |
| 2 |
|
| 3 |
from django.core.exceptions import ImproperlyConfigured |
| 4 |
from django.template import Context |
| 5 |
from django.template.engine import Engine |
| 6 |
from django.test import SimpleTestCase, override_settings |
| 7 |
|
| 8 |
from .utils import ROOT, TEMPLATE_DIR |
| 9 |
|
| 10 |
OTHER_DIR = os.path.join(ROOT, "other_templates") |
| 11 |
|
| 12 |
|
| 13 |
class EngineTest(SimpleTestCase): |
| 14 |
def test_repr_empty(self): |
| 15 |
engine = Engine() |
| 16 |
self.assertEqual( |
| 17 |
repr(engine), |
| 18 |
"<Engine: app_dirs=False debug=False loaders=[(" |
| 19 |
"'django.template.loaders.cached.Loader', " |
| 20 |
"['django.template.loaders.filesystem.Loader'])] " |
| 21 |
"string_if_invalid='' file_charset='utf-8' builtins=[" |
| 22 |
"'django.template.defaulttags', 'django.template.defaultfilters', " |
| 23 |
"'django.template.loader_tags'] autoescape=True>", |
| 24 |
) |
| 25 |
|
| 26 |
def test_repr(self): |
| 27 |
engine = Engine( |
| 28 |
dirs=[TEMPLATE_DIR], |
| 29 |
context_processors=["django.template.context_processors.debug"], |
| 30 |
debug=True, |
| 31 |
loaders=["django.template.loaders.filesystem.Loader"], |
| 32 |
string_if_invalid="x", |
| 33 |
file_charset="utf-16", |
| 34 |
libraries={"custom": "template_tests.templatetags.custom"}, |
| 35 |
autoescape=False, |
| 36 |
) |
| 37 |
self.assertEqual( |
| 38 |
repr(engine), |
| 39 |
f"<Engine: dirs=[{TEMPLATE_DIR!r}] app_dirs=False " |
| 40 |
"context_processors=['django.template.context_processors.debug'] " |
| 41 |
"debug=True loaders=['django.template.loaders.filesystem.Loader'] " |
| 42 |
"string_if_invalid='x' file_charset='utf-16' " |
| 43 |
"libraries={'custom': 'template_tests.templatetags.custom'} " |
| 44 |
"builtins=['django.template.defaulttags', " |
| 45 |
"'django.template.defaultfilters', 'django.template.loader_tags'] " |
| 46 |
"autoescape=False>", |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
class RenderToStringTest(SimpleTestCase): |
| 51 |
def setUp(self): |
| 52 |
self.engine = Engine(dirs=[TEMPLATE_DIR]) |
| 53 |
|
| 54 |
def test_basic_context(self): |
| 55 |
self.assertEqual( |
| 56 |
self.engine.render_to_string("test_context.html", {"obj": "test"}), |
| 57 |
"obj:test\n", |
| 58 |
) |
| 59 |
|
| 60 |
def test_autoescape_off(self): |
| 61 |
engine = Engine(dirs=[TEMPLATE_DIR], autoescape=False) |
| 62 |
self.assertEqual( |
| 63 |
engine.render_to_string("test_context.html", {"obj": "<script>"}), |
| 64 |
"obj:<script>\n", |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
class GetDefaultTests(SimpleTestCase): |
| 69 |
@override_settings(TEMPLATES=[]) |
| 70 |
def test_no_engines_configured(self): |
| 71 |
msg = "No DjangoTemplates backend is configured." |
| 72 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 73 |
Engine.get_default() |
| 74 |
|
| 75 |
@override_settings( |
| 76 |
TEMPLATES=[ |
| 77 |
{ |
| 78 |
"NAME": "default", |
| 79 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 80 |
"OPTIONS": {"file_charset": "abc"}, |
| 81 |
} |
| 82 |
] |
| 83 |
) |
| 84 |
def test_single_engine_configured(self): |
| 85 |
self.assertEqual(Engine.get_default().file_charset, "abc") |
| 86 |
|
| 87 |
@override_settings( |
| 88 |
TEMPLATES=[ |
| 89 |
{ |
| 90 |
"NAME": "default", |
| 91 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 92 |
"OPTIONS": {"file_charset": "abc"}, |
| 93 |
}, |
| 94 |
{ |
| 95 |
"NAME": "other", |
| 96 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 97 |
"OPTIONS": {"file_charset": "def"}, |
| 98 |
}, |
| 99 |
] |
| 100 |
) |
| 101 |
def test_multiple_engines_configured(self): |
| 102 |
self.assertEqual(Engine.get_default().file_charset, "abc") |
| 103 |
|
| 104 |
|
| 105 |
class LoaderTests(SimpleTestCase): |
| 106 |
def test_origin(self): |
| 107 |
engine = Engine(dirs=[TEMPLATE_DIR], debug=True) |
| 108 |
template = engine.get_template("index.html") |
| 109 |
self.assertEqual(template.origin.template_name, "index.html") |
| 110 |
|
| 111 |
def test_loader_priority(self): |
| 112 |
""" |
| 113 |
#21460 -- The order of template loader works. |
| 114 |
""" |
| 115 |
loaders = [ |
| 116 |
"django.template.loaders.filesystem.Loader", |
| 117 |
"django.template.loaders.app_directories.Loader", |
| 118 |
] |
| 119 |
engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders) |
| 120 |
template = engine.get_template("priority/foo.html") |
| 121 |
self.assertEqual(template.render(Context()), "priority\n") |
| 122 |
|
| 123 |
def test_cached_loader_priority(self): |
| 124 |
""" |
| 125 |
The order of template loader works. Refs #21460. |
| 126 |
""" |
| 127 |
loaders = [ |
| 128 |
( |
| 129 |
"django.template.loaders.cached.Loader", |
| 130 |
[ |
| 131 |
"django.template.loaders.filesystem.Loader", |
| 132 |
"django.template.loaders.app_directories.Loader", |
| 133 |
], |
| 134 |
), |
| 135 |
] |
| 136 |
engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders) |
| 137 |
|
| 138 |
template = engine.get_template("priority/foo.html") |
| 139 |
self.assertEqual(template.render(Context()), "priority\n") |
| 140 |
|
| 141 |
template = engine.get_template("priority/foo.html") |
| 142 |
self.assertEqual(template.render(Context()), "priority\n") |