mirror of
https://github.com/django/django.git
synced 2025-09-12 05:26:27 +00:00
Isolated template tests from Django settings.
This commit is contained in:
parent
b34b8a12b7
commit
92a2d049a2
85 changed files with 1070 additions and 1081 deletions
|
@ -1,9 +1,7 @@
|
|||
from django.conf import settings
|
||||
from django.template.base import Context, TemplateSyntaxError
|
||||
from django.template.loader import get_template
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from ..utils import render, setup, SilentGetItemClass, SilentAttrClass, SomeClass
|
||||
from ..utils import setup, SilentGetItemClass, SilentAttrClass, SomeClass
|
||||
|
||||
|
||||
basic_templates = {
|
||||
|
@ -20,7 +18,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Plain text should go through the template parser untouched.
|
||||
"""
|
||||
output = render('basic-syntax01')
|
||||
output = self.engine.render_to_string('basic-syntax01')
|
||||
self.assertEqual(output, "something cool")
|
||||
|
||||
@setup(basic_templates)
|
||||
|
@ -29,7 +27,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
Variables should be replaced with their value in the current
|
||||
context
|
||||
"""
|
||||
output = render('basic-syntax02', {'headline': 'Success'})
|
||||
output = self.engine.render_to_string('basic-syntax02', {'headline': 'Success'})
|
||||
self.assertEqual(output, 'Success')
|
||||
|
||||
@setup(basic_templates)
|
||||
|
@ -37,7 +35,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
More than one replacement variable is allowed in a template
|
||||
"""
|
||||
output = render('basic-syntax03', {"first": 1, "second": 2})
|
||||
output = self.engine.render_to_string('basic-syntax03', {"first": 1, "second": 2})
|
||||
self.assertEqual(output, '1 --- 2')
|
||||
|
||||
@setup({'basic-syntax04': 'as{{ missing }}df'})
|
||||
|
@ -45,8 +43,8 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Fail silently when a variable is not found in the current context
|
||||
"""
|
||||
output = render('basic-syntax04')
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('basic-syntax04')
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'asINVALIDdf')
|
||||
else:
|
||||
self.assertEqual(output, 'asdf')
|
||||
|
@ -57,7 +55,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
A variable may not contain more than one word
|
||||
"""
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax06')
|
||||
self.engine.get_template('basic-syntax06')
|
||||
|
||||
@setup({'basic-syntax07': '{{ }}'})
|
||||
def test_basic_syntax07(self):
|
||||
|
@ -65,7 +63,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
Raise TemplateSyntaxError for empty variable tags.
|
||||
"""
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax07')
|
||||
self.engine.get_template('basic-syntax07')
|
||||
|
||||
@setup({'basic-syntax08': '{{ }}'})
|
||||
def test_basic_syntax08(self):
|
||||
|
@ -73,14 +71,14 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
Raise TemplateSyntaxError for empty variable tags.
|
||||
"""
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax08')
|
||||
self.engine.get_template('basic-syntax08')
|
||||
|
||||
@setup({'basic-syntax09': '{{ var.method }}'})
|
||||
def test_basic_syntax09(self):
|
||||
"""
|
||||
Attribute syntax allows a template to call an object's attribute
|
||||
"""
|
||||
output = render('basic-syntax09', {'var': SomeClass()})
|
||||
output = self.engine.render_to_string('basic-syntax09', {'var': SomeClass()})
|
||||
self.assertEqual(output, 'SomeClass.method')
|
||||
|
||||
@setup({'basic-syntax10': '{{ var.otherclass.method }}'})
|
||||
|
@ -88,7 +86,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Multiple levels of attribute access are allowed.
|
||||
"""
|
||||
output = render('basic-syntax10', {'var': SomeClass()})
|
||||
output = self.engine.render_to_string('basic-syntax10', {'var': SomeClass()})
|
||||
self.assertEqual(output, 'OtherClass.method')
|
||||
|
||||
@setup({'basic-syntax11': '{{ var.blech }}'})
|
||||
|
@ -96,9 +94,9 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Fail silently when a variable's attribute isn't found.
|
||||
"""
|
||||
output = render('basic-syntax11', {'var': SomeClass()})
|
||||
output = self.engine.render_to_string('basic-syntax11', {'var': SomeClass()})
|
||||
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '')
|
||||
|
@ -110,34 +108,34 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
beginning with an underscore.
|
||||
"""
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax12')
|
||||
self.engine.get_template('basic-syntax12')
|
||||
|
||||
# Raise TemplateSyntaxError when trying to access a variable
|
||||
# containing an illegal character.
|
||||
@setup({'basic-syntax13': "{{ va>r }}"})
|
||||
def test_basic_syntax13(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax13')
|
||||
self.engine.get_template('basic-syntax13')
|
||||
|
||||
@setup({'basic-syntax14': "{{ (var.r) }}"})
|
||||
def test_basic_syntax14(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax14')
|
||||
self.engine.get_template('basic-syntax14')
|
||||
|
||||
@setup({'basic-syntax15': "{{ sp%am }}"})
|
||||
def test_basic_syntax15(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax15')
|
||||
self.engine.get_template('basic-syntax15')
|
||||
|
||||
@setup({'basic-syntax16': "{{ eggs! }}"})
|
||||
def test_basic_syntax16(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax16')
|
||||
self.engine.get_template('basic-syntax16')
|
||||
|
||||
@setup({'basic-syntax17': "{{ moo? }}"})
|
||||
def test_basic_syntax17(self):
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax17')
|
||||
self.engine.get_template('basic-syntax17')
|
||||
|
||||
@setup({'basic-syntax18': "{{ foo.bar }}"})
|
||||
def test_basic_syntax18(self):
|
||||
|
@ -145,7 +143,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
Attribute syntax allows a template to call a dictionary key's
|
||||
value.
|
||||
"""
|
||||
output = render('basic-syntax18', {"foo": {"bar": "baz"}})
|
||||
output = self.engine.render_to_string('basic-syntax18', {"foo": {"bar": "baz"}})
|
||||
self.assertEqual(output, "baz")
|
||||
|
||||
@setup({'basic-syntax19': "{{ foo.spam }}"})
|
||||
|
@ -153,9 +151,9 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Fail silently when a variable's dictionary key isn't found.
|
||||
"""
|
||||
output = render('basic-syntax19', {"foo": {"bar": "baz"}})
|
||||
output = self.engine.render_to_string('basic-syntax19', {"foo": {"bar": "baz"}})
|
||||
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '')
|
||||
|
@ -165,9 +163,9 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Fail silently when accessing a non-simple method
|
||||
"""
|
||||
output = render('basic-syntax20', {'var': SomeClass()})
|
||||
output = self.engine.render_to_string('basic-syntax20', {'var': SomeClass()})
|
||||
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '')
|
||||
|
@ -177,7 +175,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Don't silence a TypeError if it was raised inside a callable.
|
||||
"""
|
||||
template = get_template('basic-syntax20b')
|
||||
template = self.engine.get_template('basic-syntax20b')
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
template.render(Context({'var': SomeClass()}))
|
||||
|
@ -186,12 +184,12 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
# quite, a template tag.
|
||||
@setup({'basic-syntax21': "a {{ moo %} b"})
|
||||
def test_basic_syntax21(self):
|
||||
output = render('basic-syntax21')
|
||||
output = self.engine.render_to_string('basic-syntax21')
|
||||
self.assertEqual(output, "a {{ moo %} b")
|
||||
|
||||
@setup({'basic-syntax22': "{{ moo #}"})
|
||||
def test_basic_syntax22(self):
|
||||
output = render('basic-syntax22')
|
||||
output = self.engine.render_to_string('basic-syntax22')
|
||||
self.assertEqual(output, "{{ moo #}")
|
||||
|
||||
@setup({'basic-syntax23': "{{ moo #} {{ cow }}"})
|
||||
|
@ -201,47 +199,47 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
around, so this triggers an error.
|
||||
"""
|
||||
with self.assertRaises(TemplateSyntaxError):
|
||||
get_template('basic-syntax23')
|
||||
self.engine.get_template('basic-syntax23')
|
||||
|
||||
@setup({'basic-syntax24': "{{ moo\n }}"})
|
||||
def test_basic_syntax24(self):
|
||||
"""
|
||||
Embedded newlines make it not-a-tag.
|
||||
"""
|
||||
output = render('basic-syntax24')
|
||||
output = self.engine.render_to_string('basic-syntax24')
|
||||
self.assertEqual(output, "{{ moo\n }}")
|
||||
|
||||
# Literal strings are permitted inside variables, mostly for i18n
|
||||
# purposes.
|
||||
@setup({'basic-syntax25': '{{ "fred" }}'})
|
||||
def test_basic_syntax25(self):
|
||||
output = render('basic-syntax25')
|
||||
output = self.engine.render_to_string('basic-syntax25')
|
||||
self.assertEqual(output, "fred")
|
||||
|
||||
@setup({'basic-syntax26': r'{{ "\"fred\"" }}'})
|
||||
def test_basic_syntax26(self):
|
||||
output = render('basic-syntax26')
|
||||
output = self.engine.render_to_string('basic-syntax26')
|
||||
self.assertEqual(output, "\"fred\"")
|
||||
|
||||
@setup({'basic-syntax27': r'{{ _("\"fred\"") }}'})
|
||||
def test_basic_syntax27(self):
|
||||
output = render('basic-syntax27')
|
||||
output = self.engine.render_to_string('basic-syntax27')
|
||||
self.assertEqual(output, "\"fred\"")
|
||||
|
||||
# #12554 -- Make sure a silent_variable_failure Exception is
|
||||
# suppressed on dictionary and attribute lookup.
|
||||
@setup({'basic-syntax28': "{{ a.b }}"})
|
||||
def test_basic_syntax28(self):
|
||||
output = render('basic-syntax28', {'a': SilentGetItemClass()})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('basic-syntax28', {'a': SilentGetItemClass()})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '')
|
||||
|
||||
@setup({'basic-syntax29': "{{ a.b }}"})
|
||||
def test_basic_syntax29(self):
|
||||
output = render('basic-syntax29', {'a': SilentAttrClass()})
|
||||
if settings.TEMPLATE_STRING_IF_INVALID:
|
||||
output = self.engine.render_to_string('basic-syntax29', {'a': SilentAttrClass()})
|
||||
if self.engine.string_if_invalid:
|
||||
self.assertEqual(output, 'INVALID')
|
||||
else:
|
||||
self.assertEqual(output, '')
|
||||
|
@ -250,7 +248,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
# as a lookup.
|
||||
@setup({'basic-syntax30': "{{ 1.2.3 }}"})
|
||||
def test_basic_syntax30(self):
|
||||
output = render(
|
||||
output = self.engine.render_to_string(
|
||||
'basic-syntax30',
|
||||
{"1": {"2": {"3": "d"}}}
|
||||
)
|
||||
|
@ -258,7 +256,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
|
||||
@setup({'basic-syntax31': "{{ 1.2.3 }}"})
|
||||
def test_basic_syntax31(self):
|
||||
output = render(
|
||||
output = self.engine.render_to_string(
|
||||
'basic-syntax31',
|
||||
{"1": {"2": ("a", "b", "c", "d")}},
|
||||
)
|
||||
|
@ -266,7 +264,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
|
||||
@setup({'basic-syntax32': "{{ 1.2.3 }}"})
|
||||
def test_basic_syntax32(self):
|
||||
output = render(
|
||||
output = self.engine.render_to_string(
|
||||
'basic-syntax32',
|
||||
{"1": (("x", "x", "x", "x"), ("y", "y", "y", "y"), ("a", "b", "c", "d"))},
|
||||
)
|
||||
|
@ -274,7 +272,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
|
||||
@setup({'basic-syntax33': "{{ 1.2.3 }}"})
|
||||
def test_basic_syntax33(self):
|
||||
output = render(
|
||||
output = self.engine.render_to_string(
|
||||
'basic-syntax33',
|
||||
{"1": ("xxxx", "yyyy", "abcd")},
|
||||
)
|
||||
|
@ -282,7 +280,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
|
||||
@setup({'basic-syntax34': "{{ 1.2.3 }}"})
|
||||
def test_basic_syntax34(self):
|
||||
output = render(
|
||||
output = self.engine.render_to_string(
|
||||
'basic-syntax34',
|
||||
{"1": ({"x": "x"}, {"y": "y"}, {"z": "z", "3": "d"})}
|
||||
)
|
||||
|
@ -291,12 +289,12 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
# Numbers are numbers even if their digits are in the context.
|
||||
@setup({'basic-syntax35': "{{ 1 }}"})
|
||||
def test_basic_syntax35(self):
|
||||
output = render('basic-syntax35', {"1": "abc"})
|
||||
output = self.engine.render_to_string('basic-syntax35', {"1": "abc"})
|
||||
self.assertEqual(output, '1')
|
||||
|
||||
@setup({'basic-syntax36': "{{ 1.2 }}"})
|
||||
def test_basic_syntax36(self):
|
||||
output = render('basic-syntax36', {"1": "abc"})
|
||||
output = self.engine.render_to_string('basic-syntax36', {"1": "abc"})
|
||||
self.assertEqual(output, '1.2')
|
||||
|
||||
@setup({'basic-syntax37': '{{ callable }}'})
|
||||
|
@ -304,7 +302,7 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Call methods in the top level of the context.
|
||||
"""
|
||||
output = render('basic-syntax37', {"callable": lambda: "foo bar"})
|
||||
output = self.engine.render_to_string('basic-syntax37', {"callable": lambda: "foo bar"})
|
||||
self.assertEqual(output, 'foo bar')
|
||||
|
||||
@setup({'basic-syntax38': '{{ var.callable }}'})
|
||||
|
@ -312,5 +310,5 @@ class BasicSyntaxTests(SimpleTestCase):
|
|||
"""
|
||||
Call methods returned from dictionary lookups.
|
||||
"""
|
||||
output = render('basic-syntax38', {"var": {"callable": lambda: "foo bar"}})
|
||||
output = self.engine.render_to_string('basic-syntax38', {"var": {"callable": lambda: "foo bar"}})
|
||||
self.assertEqual(output, 'foo bar')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue