From 969e082858339b8da79af2c036d434ce91c27ee0 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 27 Dec 2014 23:39:43 +0100 Subject: [PATCH] Added tests for django.template.loader. Deprecated features aren't tested. --- tests/template_loader/__init__.py | 0 .../template_loader/hello.html | 1 + .../templates/template_loader/goodbye.html | 1 + .../templates/template_loader/hello.html | 1 + tests/template_loader/tests.py | 104 ++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 tests/template_loader/__init__.py create mode 100644 tests/template_loader/template_strings/template_loader/hello.html create mode 100644 tests/template_loader/templates/template_loader/goodbye.html create mode 100644 tests/template_loader/templates/template_loader/hello.html create mode 100644 tests/template_loader/tests.py diff --git a/tests/template_loader/__init__.py b/tests/template_loader/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/template_loader/template_strings/template_loader/hello.html b/tests/template_loader/template_strings/template_loader/hello.html new file mode 100644 index 00000000000..70175fd9548 --- /dev/null +++ b/tests/template_loader/template_strings/template_loader/hello.html @@ -0,0 +1 @@ +Hello! (template strings) diff --git a/tests/template_loader/templates/template_loader/goodbye.html b/tests/template_loader/templates/template_loader/goodbye.html new file mode 100644 index 00000000000..b8fb727ddd8 --- /dev/null +++ b/tests/template_loader/templates/template_loader/goodbye.html @@ -0,0 +1 @@ +Goodbye! (Django templates) diff --git a/tests/template_loader/templates/template_loader/hello.html b/tests/template_loader/templates/template_loader/hello.html new file mode 100644 index 00000000000..7f54a62edb0 --- /dev/null +++ b/tests/template_loader/templates/template_loader/hello.html @@ -0,0 +1 @@ +Hello! (Django templates) diff --git a/tests/template_loader/tests.py b/tests/template_loader/tests.py new file mode 100644 index 00000000000..3ec785a6a45 --- /dev/null +++ b/tests/template_loader/tests.py @@ -0,0 +1,104 @@ +from django.test import override_settings, SimpleTestCase +from django.template import TemplateDoesNotExist +from django.template.loader import ( + get_template, select_template, render_to_string) + + +@override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.dummy.TemplateStrings', + 'APP_DIRS': True, +}, { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, +}]) +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): + get_template("template_loader/unknown.html") + + 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_not_found(self): + with self.assertRaises(TemplateDoesNotExist): + select_template(["template_loader/unknown.html", + "template_loader/missing.html"]) + + 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_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): + render_to_string("template_loader/unknown.html") + + 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): + render_to_string(["template_loader/unknown.html", + "template_loader/missing.html"]) + + 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")