mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Isolated template tests from Django settings.
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
from django.conf import settings
|
||||
from django.template.base import Context, TemplateDoesNotExist, TemplateSyntaxError
|
||||
from django.template.loader import get_template
|
||||
from django.template import Context, TemplateDoesNotExist, TemplateSyntaxError
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from .test_basic import basic_templates
|
||||
from ..utils import render, setup
|
||||
from ..utils import setup
|
||||
|
||||
|
||||
include_fail_templates = {
|
||||
@@ -17,17 +15,17 @@ class IncludeTagTests(SimpleTestCase):
|
||||
|
||||
@setup({'include01': '{% include "basic-syntax01" %}'}, basic_templates)
|
||||
def test_include01(self):
|
||||
output = render('include01')
|
||||
output = self.engine.render_to_string('include01')
|
||||
self.assertEqual(output, 'something cool')
|
||||
|
||||
@setup({'include02': '{% include "basic-syntax02" %}'}, basic_templates)
|
||||
def test_include02(self):
|
||||
output = render('include02', {'headline': 'Included'})
|
||||
output = self.engine.render_to_string('include02', {'headline': 'Included'})
|
||||
self.assertEqual(output, 'Included')
|
||||
|
||||
@setup({'include03': '{% include template_name %}'}, basic_templates)
|
||||
def test_include03(self):
|
||||
output = render(
|
||||
output = self.engine.render_to_string(
|
||||
'include03',
|
||||
{'template_name': 'basic-syntax02', 'headline': 'Included'},
|
||||
)
|
||||
@@ -35,9 +33,9 @@ class IncludeTagTests(SimpleTestCase):
|
||||
|
||||
@setup({'include04': 'a{% include "nonexistent" %}b'})
|
||||
def test_include04(self):
|
||||
template = get_template('include04')
|
||||
template = self.engine.get_template('include04')
|
||||
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
if self.engine.debug:
|
||||
with self.assertRaises(TemplateDoesNotExist):
|
||||
template.render(Context({}))
|
||||
else:
|
||||
@@ -49,17 +47,17 @@ class IncludeTagTests(SimpleTestCase):
|
||||
'include06': '{% include "include 05"%}',
|
||||
})
|
||||
def test_include06(self):
|
||||
output = render('include06')
|
||||
output = self.engine.render_to_string('include06')
|
||||
self.assertEqual(output, "template with a space")
|
||||
|
||||
@setup({'include07': '{% include "basic-syntax02" with headline="Inline" %}'}, basic_templates)
|
||||
def test_include07(self):
|
||||
output = render('include07', {'headline': 'Included'})
|
||||
output = self.engine.render_to_string('include07', {'headline': 'Included'})
|
||||
self.assertEqual(output, 'Inline')
|
||||
|
||||
@setup({'include08': '{% include headline with headline="Dynamic" %}'}, basic_templates)
|
||||
def test_include08(self):
|
||||
output = render('include08', {'headline': 'basic-syntax02'})
|
||||
output = self.engine.render_to_string('include08', {'headline': 'basic-syntax02'})
|
||||
self.assertEqual(output, 'Dynamic')
|
||||
|
||||
@setup(
|
||||
@@ -69,29 +67,29 @@ class IncludeTagTests(SimpleTestCase):
|
||||
basic_templates,
|
||||
)
|
||||
def test_include09(self):
|
||||
output = render('include09', {'first': 'Ul', 'second': 'lU'})
|
||||
output = self.engine.render_to_string('include09', {'first': 'Ul', 'second': 'lU'})
|
||||
self.assertEqual(output, 'Ul--LU --- UL--lU')
|
||||
|
||||
@setup({'include10': '{% include "basic-syntax03" only %}'}, basic_templates)
|
||||
def test_include10(self):
|
||||
output = render('include10', {'first': '1'})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('include10', {'first': '1'})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID --- INVALID')
|
||||
else:
|
||||
self.assertEqual(output, ' --- ')
|
||||
|
||||
@setup({'include11': '{% include "basic-syntax03" only with second=2 %}'}, basic_templates)
|
||||
def test_include11(self):
|
||||
output = render('include11', {'first': '1'})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('include11', {'first': '1'})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID --- 2')
|
||||
else:
|
||||
self.assertEqual(output, ' --- 2')
|
||||
|
||||
@setup({'include12': '{% include "basic-syntax03" with first=1 only %}'}, basic_templates)
|
||||
def test_include12(self):
|
||||
output = render('include12', {'second': '2'})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('include12', {'second': '2'})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, '1 --- INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '1 --- ')
|
||||
@@ -101,8 +99,8 @@ class IncludeTagTests(SimpleTestCase):
|
||||
basic_templates,
|
||||
)
|
||||
def test_include13(self):
|
||||
output = render('include13', {'first': '&'})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('include13', {'first': '&'})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, '& --- INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '& --- ')
|
||||
@@ -114,8 +112,8 @@ class IncludeTagTests(SimpleTestCase):
|
||||
basic_templates,
|
||||
)
|
||||
def test_include14(self):
|
||||
output = render('include14', {'var1': '&'})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('include14', {'var1': '&'})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, '& --- INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '& --- ')
|
||||
@@ -124,81 +122,81 @@ class IncludeTagTests(SimpleTestCase):
|
||||
@setup({'include-error01': '{% include "basic-syntax01" with %}'})
|
||||
def test_include_error01(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-error01')
|
||||
self.engine.get_template('include-error01')
|
||||
|
||||
@setup({'include-error02': '{% include "basic-syntax01" with "no key" %}'})
|
||||
def test_include_error02(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-error02')
|
||||
self.engine.get_template('include-error02')
|
||||
|
||||
@setup({'include-error03': '{% include "basic-syntax01" with dotted.arg="error" %}'})
|
||||
def test_include_error03(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-error03')
|
||||
self.engine.get_template('include-error03')
|
||||
|
||||
@setup({'include-error04': '{% include "basic-syntax01" something_random %}'})
|
||||
def test_include_error04(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-error04')
|
||||
self.engine.get_template('include-error04')
|
||||
|
||||
@setup({'include-error05': '{% include "basic-syntax01" foo="duplicate" foo="key" %}'})
|
||||
def test_include_error05(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-error05')
|
||||
self.engine.get_template('include-error05')
|
||||
|
||||
@setup({'include-error06': '{% include "basic-syntax01" only only %}'})
|
||||
def test_include_error06(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-error06')
|
||||
self.engine.get_template('include-error06')
|
||||
|
||||
@setup(include_fail_templates)
|
||||
def test_include_fail1(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
get_template('include-fail1')
|
||||
self.engine.get_template('include-fail1')
|
||||
|
||||
@setup(include_fail_templates)
|
||||
def test_include_fail2(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('include-fail2')
|
||||
self.engine.get_template('include-fail2')
|
||||
|
||||
@setup({'include-error07': '{% include "include-fail1" %}'}, include_fail_templates)
|
||||
def test_include_error07(self):
|
||||
t = get_template('include-error07')
|
||||
template = self.engine.get_template('include-error07')
|
||||
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
if self.engine.debug:
|
||||
with self.assertRaises(RuntimeError):
|
||||
t.render(Context())
|
||||
template.render(Context())
|
||||
else:
|
||||
self.assertEqual(t.render(Context()), '')
|
||||
self.assertEqual(template.render(Context()), '')
|
||||
|
||||
@setup({'include-error08': '{% include "include-fail2" %}'}, include_fail_templates)
|
||||
def test_include_error08(self):
|
||||
t = get_template('include-error08')
|
||||
template = self.engine.get_template('include-error08')
|
||||
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
if self.engine.debug:
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
t.render(Context())
|
||||
template.render(Context())
|
||||
else:
|
||||
self.assertEqual(t.render(Context()), '')
|
||||
self.assertEqual(template.render(Context()), '')
|
||||
|
||||
@setup({'include-error09': '{% include failed_include %}'}, include_fail_templates)
|
||||
def test_include_error09(self):
|
||||
c = Context({'failed_include': 'include-fail1'})
|
||||
t = get_template('include-error09')
|
||||
context = Context({'failed_include': 'include-fail1'})
|
||||
template = self.engine.get_template('include-error09')
|
||||
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
if self.engine.debug:
|
||||
with self.assertRaises(RuntimeError):
|
||||
t.render(c)
|
||||
template.render(context)
|
||||
else:
|
||||
self.assertEqual(t.render(c), '')
|
||||
self.assertEqual(template.render(context), '')
|
||||
|
||||
@setup({'include-error10': '{% include failed_include %}'}, include_fail_templates)
|
||||
def test_include_error10(self):
|
||||
c = Context({'failed_include': 'include-fail2'})
|
||||
t = get_template('include-error10')
|
||||
context = Context({'failed_include': 'include-fail2'})
|
||||
template = self.engine.get_template('include-error10')
|
||||
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
if self.engine.debug:
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
t.render(c)
|
||||
template.render(context)
|
||||
else:
|
||||
self.assertEqual(t.render(c), '')
|
||||
self.assertEqual(template.render(context), '')
|
||||
|
||||
Reference in New Issue
Block a user