Added framework for writing non-model-based tests, and added tests for cache and templates
from the old django/tests location (which has been removed). git-svn-id: http://code.djangoproject.com/svn/django/trunk@367 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
8483f0aa04
commit
440a2a97e9
|
@ -1,119 +0,0 @@
|
||||||
"""
|
|
||||||
Unit tests for django.core.cache
|
|
||||||
|
|
||||||
If you don't have memcached running on localhost port 11211, the memcached tests
|
|
||||||
will fail.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from django.core import cache
|
|
||||||
import unittest
|
|
||||||
import time
|
|
||||||
|
|
||||||
# functions/classes for complex data type tests
|
|
||||||
def f():
|
|
||||||
return 42
|
|
||||||
class C:
|
|
||||||
def m(n):
|
|
||||||
return 24
|
|
||||||
|
|
||||||
class CacheBackendsTest(unittest.TestCase):
|
|
||||||
|
|
||||||
def testBackends(self):
|
|
||||||
sc = cache.get_cache('simple://')
|
|
||||||
mc = cache.get_cache('memcached://127.0.0.1:11211/')
|
|
||||||
self.failUnless(isinstance(sc, cache._SimpleCache))
|
|
||||||
self.failUnless(isinstance(mc, cache._MemcachedCache))
|
|
||||||
|
|
||||||
def testInvalidBackends(self):
|
|
||||||
self.assertRaises(cache.InvalidCacheBackendError, cache.get_cache, 'nothing://foo/')
|
|
||||||
self.assertRaises(cache.InvalidCacheBackendError, cache.get_cache, 'not a uri')
|
|
||||||
|
|
||||||
def testDefaultTimeouts(self):
|
|
||||||
sc = cache.get_cache('simple:///?timeout=15')
|
|
||||||
mc = cache.get_cache('memcached://127.0.0.1:11211/?timeout=15')
|
|
||||||
self.assertEquals(sc.default_timeout, 15)
|
|
||||||
self.assertEquals(sc.default_timeout, 15)
|
|
||||||
|
|
||||||
class SimpleCacheTest(unittest.TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.cache = cache.get_cache('simple://')
|
|
||||||
|
|
||||||
def testGetSet(self):
|
|
||||||
self.cache.set('key', 'value')
|
|
||||||
self.assertEqual(self.cache.get('key'), 'value')
|
|
||||||
|
|
||||||
def testNonExistantKeys(self):
|
|
||||||
self.assertEqual(self.cache.get('does not exist'), None)
|
|
||||||
self.assertEqual(self.cache.get('does not exist', 'bang!'), 'bang!')
|
|
||||||
|
|
||||||
def testGetMany(self):
|
|
||||||
self.cache.set('a', 'a')
|
|
||||||
self.cache.set('b', 'b')
|
|
||||||
self.cache.set('c', 'c')
|
|
||||||
self.cache.set('d', 'd')
|
|
||||||
self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
|
|
||||||
self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})
|
|
||||||
|
|
||||||
def testDelete(self):
|
|
||||||
self.cache.set('key1', 'spam')
|
|
||||||
self.cache.set('key2', 'eggs')
|
|
||||||
self.assertEqual(self.cache.get('key1'), 'spam')
|
|
||||||
self.cache.delete('key1')
|
|
||||||
self.assertEqual(self.cache.get('key1'), None)
|
|
||||||
self.assertEqual(self.cache.get('key2'), 'eggs')
|
|
||||||
|
|
||||||
def testHasKey(self):
|
|
||||||
self.cache.set('hello', 'goodbye')
|
|
||||||
self.assertEqual(self.cache.has_key('hello'), True)
|
|
||||||
self.assertEqual(self.cache.has_key('goodbye'), False)
|
|
||||||
|
|
||||||
def testDataTypes(self):
|
|
||||||
items = {
|
|
||||||
'string' : 'this is a string',
|
|
||||||
'int' : 42,
|
|
||||||
'list' : [1, 2, 3, 4],
|
|
||||||
'tuple' : (1, 2, 3, 4),
|
|
||||||
'dict' : {'A': 1, 'B' : 2},
|
|
||||||
'function' : f,
|
|
||||||
'class' : C,
|
|
||||||
}
|
|
||||||
for (key, value) in items.items():
|
|
||||||
self.cache.set(key, value)
|
|
||||||
self.assertEqual(self.cache.get(key), value)
|
|
||||||
|
|
||||||
def testExpiration(self):
|
|
||||||
self.cache.set('expire', 'very quickly', 1)
|
|
||||||
time.sleep(2)
|
|
||||||
self.assertEqual(self.cache.get('expire'), None)
|
|
||||||
|
|
||||||
def testCull(self):
|
|
||||||
c = cache.get_cache('simple://?max_entries=9&cull_frequency=3')
|
|
||||||
for i in range(10):
|
|
||||||
c.set('culltest%i' % i, i)
|
|
||||||
n = 0
|
|
||||||
for i in range(10):
|
|
||||||
if c.get('culltest%i' % i):
|
|
||||||
n += 1
|
|
||||||
self.assertEqual(n, 6)
|
|
||||||
|
|
||||||
def testCullAll(self):
|
|
||||||
c = cache.get_cache('simple://?max_entries=9&cull_frequency=0')
|
|
||||||
for i in range(10):
|
|
||||||
c.set('cullalltest%i' % i, i)
|
|
||||||
for i in range(10):
|
|
||||||
self.assertEqual(self.cache.get('cullalltest%i' % i), None)
|
|
||||||
|
|
||||||
class MemcachedCacheTest(SimpleCacheTest):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.cache = cache.get_cache('memcached://127.0.0.1:11211/')
|
|
||||||
|
|
||||||
testCull = testCullAll = lambda s: None
|
|
||||||
|
|
||||||
def tests():
|
|
||||||
s = unittest.TestLoader().loadTestsFromName(__name__)
|
|
||||||
unittest.TextTestRunner(verbosity=0).run(s)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
tests()
|
|
|
@ -1,102 +0,0 @@
|
||||||
from django.core import template, template_loader
|
|
||||||
|
|
||||||
# SYNTAX --
|
|
||||||
# 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
|
|
||||||
TEMPLATE_TESTS = {
|
|
||||||
# Standard template with no inheritance
|
|
||||||
'test01': ("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", {}, '1_3_'),
|
|
||||||
|
|
||||||
# Standard two-level inheritance
|
|
||||||
'test02': ("{% extends 'test01' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
|
|
||||||
|
|
||||||
# Three-level with no redefinitions on third level
|
|
||||||
'test03': ("{% extends 'test02' %}", {}, '1234'),
|
|
||||||
|
|
||||||
# Two-level with no redefinitions on second level
|
|
||||||
'test04': ("{% extends 'test01' %}", {}, '1_3_'),
|
|
||||||
|
|
||||||
# Two-level with double quotes instead of single quotes
|
|
||||||
'test05': ('{% extends "test02" %}', {}, '1234'),
|
|
||||||
|
|
||||||
# Three-level with variable parent-template name
|
|
||||||
'test06': ("{% extends foo %}", {'foo': 'test02'}, '1234'),
|
|
||||||
|
|
||||||
# Two-level with one block defined, one block not defined
|
|
||||||
'test07': ("{% extends 'test01' %}{% block second %}5{% endblock %}", {}, '1_35'),
|
|
||||||
|
|
||||||
# Three-level with one block defined on this level, two blocks defined next level
|
|
||||||
'test08': ("{% extends 'test02' %}{% block second %}5{% endblock %}", {}, '1235'),
|
|
||||||
|
|
||||||
# Three-level with second and third levels blank
|
|
||||||
'test09': ("{% extends 'test04' %}", {}, '1_3_'),
|
|
||||||
|
|
||||||
# Three-level with space NOT in a block -- should be ignored
|
|
||||||
'test10': ("{% extends 'test04' %} ", {}, '1_3_'),
|
|
||||||
|
|
||||||
# Three-level with both blocks defined on this level, but none on second level
|
|
||||||
'test11': ("{% extends 'test04' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
|
|
||||||
|
|
||||||
# Three-level with this level providing one and second level providing the other
|
|
||||||
'test12': ("{% extends 'test07' %}{% block first %}2{% endblock %}", {}, '1235'),
|
|
||||||
|
|
||||||
# Three-level with this level overriding second level
|
|
||||||
'test13': ("{% extends 'test02' %}{% block first %}a{% endblock %}{% block second %}b{% endblock %}", {}, '1a3b'),
|
|
||||||
|
|
||||||
# A block defined only in a child template shouldn't be displayed
|
|
||||||
'test14': ("{% extends 'test01' %}{% block newblock %}NO DISPLAY{% endblock %}", {}, '1_3_'),
|
|
||||||
|
|
||||||
# A block within another block
|
|
||||||
'test15': ("{% extends 'test01' %}{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}", {}, '12inner3_'),
|
|
||||||
|
|
||||||
# A block within another block (level 2)
|
|
||||||
'test16': ("{% extends 'test15' %}{% block inner %}out{% endblock %}", {}, '12out3_'),
|
|
||||||
|
|
||||||
# {% load %} tag (parent -- setup for test-exception04)
|
|
||||||
'test17': ("{% load polls.polls %}{% block first %}1234{% endblock %}", {}, '1234'),
|
|
||||||
|
|
||||||
# {% load %} tag (standard usage, without inheritance)
|
|
||||||
'test18': ("{% load polls.polls %}{% voteratio choice poll 400 %}5678", {}, '05678'),
|
|
||||||
|
|
||||||
# {% load %} tag (within a child template)
|
|
||||||
'test19': ("{% extends 'test01' %}{% block first %}{% load polls.polls %}{% voteratio choice poll 400 %}5678{% endblock %}", {}, '1056783_'),
|
|
||||||
|
|
||||||
# Raise exception for invalid template name
|
|
||||||
'test-exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
|
|
||||||
|
|
||||||
# Raise exception for invalid template name (in variable)
|
|
||||||
'test-exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
|
|
||||||
|
|
||||||
# Raise exception for extra {% extends %} tags
|
|
||||||
'test-exception03': ("{% extends 'test01' %}{% block first %}2{% endblock %}{% extends 'test16' %}", {}, template.TemplateSyntaxError),
|
|
||||||
|
|
||||||
# Raise exception for custom tags used in child with {% load %} tag in parent, not in child
|
|
||||||
'test-exception04': ("{% extends 'test17' %}{% block first %}{% votegraph choice poll 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
|
|
||||||
}
|
|
||||||
|
|
||||||
# This replaces the standard template_loader.
|
|
||||||
def test_template_loader(template_name):
|
|
||||||
try:
|
|
||||||
return TEMPLATE_TESTS[template_name][0]
|
|
||||||
except KeyError:
|
|
||||||
raise template.TemplateDoesNotExist, template_name
|
|
||||||
template_loader.load_template_source = test_template_loader
|
|
||||||
|
|
||||||
def run_tests():
|
|
||||||
tests = TEMPLATE_TESTS.items()
|
|
||||||
tests.sort()
|
|
||||||
for name, vals in tests:
|
|
||||||
try:
|
|
||||||
output = template_loader.get_template(name).render(template.Context(vals[1]))
|
|
||||||
except Exception, e:
|
|
||||||
if e.__class__ == vals[2]:
|
|
||||||
print "%s -- Passed" % name
|
|
||||||
else:
|
|
||||||
print "%s -- FAILED. Got %s, exception: %s" % (name, e.__class__, e)
|
|
||||||
continue
|
|
||||||
if output == vals[2]:
|
|
||||||
print "%s -- Passed" % name
|
|
||||||
else:
|
|
||||||
print "%s -- FAILED. Expected %r, got %r" % (name, vals[2], output)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
run_tests()
|
|
|
@ -1,707 +0,0 @@
|
||||||
"""
|
|
||||||
Unit tests for template.py
|
|
||||||
|
|
||||||
These tests assume the following template syntax:
|
|
||||||
|
|
||||||
FILTER_SEPARATOR = '|'
|
|
||||||
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
|
|
||||||
BLOCK_TAG_START = '{%'
|
|
||||||
BLOCK_TAG_END = '%}'
|
|
||||||
VARIABLE_TAG_START = '{{'
|
|
||||||
VARIABLE_TAG_END = '}}'
|
|
||||||
"""
|
|
||||||
|
|
||||||
from django.core import template
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
class RandomSyntaxErrorsCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testTagsOnOneLine(self):
|
|
||||||
"Tags straddling more than one line are not interpreted"
|
|
||||||
c = template.Context({'key':'value'})
|
|
||||||
t = template.Template('<h1>{{key\n}}</h1>')
|
|
||||||
expected = '<h1>{{key\n}}</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class PlainTextCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testPlainText(self):
|
|
||||||
"Plain text should go through the template parser untouched"
|
|
||||||
c = template.Context()
|
|
||||||
t = template.Template('<h1>Success</h1>')
|
|
||||||
expected = '<h1>Success</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class VariableSubstitutionCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testSingleTag(self):
|
|
||||||
"Variables should be replaced with their value in the current context"
|
|
||||||
c = template.Context({'headline':'Success'})
|
|
||||||
t = template.Template('<h1>{{headline}}</h1>')
|
|
||||||
expected = '<h1>Success</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testDoubleTag(self):
|
|
||||||
"More than one replacement variable is allowed in a template"
|
|
||||||
c = template.Context({'firsttag':'it', 'secondtag':'worked'})
|
|
||||||
t = template.Template('<h1>{{firsttag}} {{secondtag}}</h1>')
|
|
||||||
expected = '<h1>it worked</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNonexistentVariable(self):
|
|
||||||
"Fail silently when a variable is not found in the current context"
|
|
||||||
c = template.Context({})
|
|
||||||
t = template.Template('<h1>{{unknownvar}}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariablesWithSpaces(self):
|
|
||||||
"A replacement-variable tag may not contain more than one word"
|
|
||||||
t = '<h1>{{multi word tag}}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testEmptyTag(self):
|
|
||||||
"Raise TemplateSyntaxError for empty variable tags"
|
|
||||||
t = '{{ }}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
t = '{{ }}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testIntegerContextValue(self):
|
|
||||||
"Accept integers as variable values"
|
|
||||||
c = template.Context({'var':55})
|
|
||||||
t = template.Template('<h1>{{var}}</h1>')
|
|
||||||
expected = '<h1>55</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def textIntegerContextKey(self):
|
|
||||||
"Accept integers as variable keys"
|
|
||||||
c = template.Context({55:'var'})
|
|
||||||
t = template.Template('<h1>{{55}}</h1>')
|
|
||||||
expected = '<h1>var</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableAttributeAccess1(self):
|
|
||||||
"Attribute syntax allows a template to call an object's attribute"
|
|
||||||
class AClass: pass
|
|
||||||
obj = AClass()
|
|
||||||
obj.att = 'attvalue'
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.att }}</h1>')
|
|
||||||
expected = '<h1>attvalue</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableAttributeAccess2(self):
|
|
||||||
"Attribute syntax allows a template to call an object's attribute (with getattr defined)"
|
|
||||||
class AClass:
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
return "attvalue"
|
|
||||||
obj = AClass()
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.att }}</h1>')
|
|
||||||
expected = '<h1>attvalue</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableAttributeAccessMultiple(self):
|
|
||||||
"Multiple levels of attribute access are allowed"
|
|
||||||
class AClass: pass
|
|
||||||
obj = AClass()
|
|
||||||
obj.article = AClass()
|
|
||||||
obj.article.section = AClass()
|
|
||||||
obj.article.section.title = 'Headline'
|
|
||||||
c = template.Context({'obj':obj})
|
|
||||||
t = template.Template('<h1>{{ obj.article.section.title }}</h1>')
|
|
||||||
expected = '<h1>Headline</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNonexistentVariableAttributeObject(self):
|
|
||||||
"Fail silently when a variable's attribute isn't found"
|
|
||||||
class AClass: pass
|
|
||||||
obj = AClass()
|
|
||||||
obj.att = 'attvalue'
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.nonexistentatt }}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testIllegalUnderscoreInVariableName(self):
|
|
||||||
"Raise TemplateSyntaxError when trying to access a variable beginning with an underscore"
|
|
||||||
t = '<h1>{{ var._att }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
t = '<h1>{{ _att }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testIllegalCharacterInVariableName(self):
|
|
||||||
"Raise TemplateSyntaxError when trying to access a variable containing an illegal character"
|
|
||||||
t = '<h1>{{ (blah }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
t = '<h1>{{ (blah.test) }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
t = '<h1>{{ bl(ah.test) }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testVariableAttributeDictionary(self):
|
|
||||||
"Attribute syntax allows a template to call a dictionary key's value"
|
|
||||||
obj = {'att':'attvalue'}
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.att }}</h1>')
|
|
||||||
expected = '<h1>attvalue</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNonexistentVariableAttributeDictionary(self):
|
|
||||||
"Fail silently when a variable's dictionary key isn't found"
|
|
||||||
obj = {'att':'attvalue'}
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.nonexistentatt }}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableAttributeCallable(self):
|
|
||||||
"Attribute syntax allows a template to call a simple method"
|
|
||||||
class AClass:
|
|
||||||
def hello(self): return 'hi'
|
|
||||||
obj = AClass()
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.hello }}</h1>')
|
|
||||||
expected = '<h1>hi</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableAttributeCallableWrongArguments(self):
|
|
||||||
"Fail silently when accessing a non-simple method"
|
|
||||||
class AClass:
|
|
||||||
def hello(self, name): return 'hi, %s' % name
|
|
||||||
obj = AClass()
|
|
||||||
c = template.Context({'var':obj})
|
|
||||||
t = template.Template('<h1>{{ var.hello }}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class VariableFiltersCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.c = template.Context({'var':'Hello There Programmer'})
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.c = None
|
|
||||||
|
|
||||||
def testUpper(self):
|
|
||||||
"The 'upper' filter converts a string into all uppercase"
|
|
||||||
t = template.Template('<h1>{{ var|upper }}</h1>')
|
|
||||||
expected = '<h1>HELLO THERE PROGRAMMER</h1>'
|
|
||||||
self.assertEqual(expected, t.render(self.c))
|
|
||||||
|
|
||||||
def testLower(self):
|
|
||||||
"The 'lower' filter converts a string into all lowercase"
|
|
||||||
t = template.Template('<h1>{{ var|lower }}</h1>')
|
|
||||||
expected = '<h1>hello there programmer</h1>'
|
|
||||||
self.assertEqual(expected, t.render(self.c))
|
|
||||||
|
|
||||||
def testUpperThenLower(self):
|
|
||||||
"Filters may be applied in succession (upper|lower)"
|
|
||||||
t = template.Template('<h1>{{ var|upper|lower }}</h1>')
|
|
||||||
expected = '<h1>hello there programmer</h1>'
|
|
||||||
self.assertEqual(expected, t.render(self.c))
|
|
||||||
|
|
||||||
def testLowerThenUpper(self):
|
|
||||||
"Filters may be applied in succession (lower|upper)"
|
|
||||||
t = template.Template('<h1>{{ var|lower|upper }}</h1>')
|
|
||||||
expected = '<h1>HELLO THERE PROGRAMMER</h1>'
|
|
||||||
self.assertEqual(expected, t.render(self.c))
|
|
||||||
|
|
||||||
def testSpaceBetweenVariableAndFilterPipe(self):
|
|
||||||
"Raise TemplateSyntaxError for space between a variable and filter pipe"
|
|
||||||
t = '<h1>{{ var |lower }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testSpaceBetweenFilterPipeAndFilterName1(self):
|
|
||||||
"Raise TemplateSyntaxError for space after a filter pipe"
|
|
||||||
t = '<h1>{{ var| lower }}</h1>'
|
|
||||||
expected = '<h1>Hello There Programmer</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testNonexistentFilter(self):
|
|
||||||
"Raise TemplateSyntaxError for a nonexistent filter"
|
|
||||||
t = '<h1>{{ var|nonexistentfilter }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testDefaultFilter1(self):
|
|
||||||
"Ignore the default argument when a variable passed through the 'default' filter already exists"
|
|
||||||
c = template.Context({'var':'Variable'})
|
|
||||||
t = template.Template('<h1>{{ var|default:"Default" }}</h1>')
|
|
||||||
expected = '<h1>Variable</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testDefaultFilter2(self):
|
|
||||||
"Use the default argument when a variable passed through the 'default' filter doesn't exist"
|
|
||||||
c = template.Context({'var':'Variable'})
|
|
||||||
t = template.Template('<h1>{{ nonvar|default:"Default" }}</h1>')
|
|
||||||
expected = '<h1>Default</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testDefaultFilter3(self):
|
|
||||||
"Use the default argument when a variable passed through the 'default' filter doesn't exist (spaces)"
|
|
||||||
c = template.Context({'var':'Variable'})
|
|
||||||
t = template.Template('<h1>{{ nonvar|default:"Default value" }}</h1>')
|
|
||||||
expected = '<h1>Default value</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testDefaultFilter4(self):
|
|
||||||
"Use the default argument when a variable passed through the 'default' filter doesn't exist (quoted)"
|
|
||||||
c = template.Context({'var':'Variable'})
|
|
||||||
t = template.Template('<h1>{{ nonvar|default:"Default \"quoted\" value" }}</h1>')
|
|
||||||
expected = '<h1>Default "quoted" value</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testDefaultFilter4(self):
|
|
||||||
"Use the default argument when a variable passed through the 'default' filter doesn't exist (escaped backslash)"
|
|
||||||
c = template.Context({'var':'Variable'})
|
|
||||||
t = template.Template('<h1>{{ nonvar|default:"Default \\\\ slash" }}</h1>')
|
|
||||||
expected = '<h1>Default \\ slash</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testDefaultFilter4(self):
|
|
||||||
"Use the default argument when a variable passed through the 'default' filter doesn't exist (single backslash)"
|
|
||||||
t = '<h1>{{ nonvar|default:"Default \\ slash" }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testIllegalCharacterInFilterName(self):
|
|
||||||
"Raise TemplateSyntaxError when trying to access a filter containing an illegal character"
|
|
||||||
t = '<h1>{{ blah|(lower) }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
t = '<h1>{{ blah|low(er) }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
class BlockTagCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testNonexistentTag(self):
|
|
||||||
"Raise TemplateSyntaxError for invalid block tags"
|
|
||||||
t = '<h1>{% not-a-tag %}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testEmptyTag(self):
|
|
||||||
"Raise TemplateSyntaxError for empty block tags"
|
|
||||||
t = '{% %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
class FirstOfCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testFirstOfDisplaysFirstIfSet(self):
|
|
||||||
"A firstof tag should display the first item if it evaluates to true somehow"
|
|
||||||
c = template.Context({'first': 'one', 'second': 'two'})
|
|
||||||
t = template.Template('<h1>{% firstof first second %}</h1>')
|
|
||||||
expected = '<h1>one</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testFirstOfDisplaysSecondIfFirstIsFalse(self):
|
|
||||||
"A firstof tag should display the second item if it evaluates to true and the first is false"
|
|
||||||
c = template.Context({'first': '', 'second': 'two'})
|
|
||||||
t = template.Template('<h1>{% firstof first second %}</h1>')
|
|
||||||
expected = '<h1>two</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testFirstOfRaisesErrorIfEmpty(self):
|
|
||||||
"A firstof tag should raise a syntax error if it doesn't have any arguments"
|
|
||||||
t = '{% firstof %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testFirstOfDoesNothingIfAllAreFalse(self):
|
|
||||||
"A firstof tag should display nothing if no arguments evaluate to true"
|
|
||||||
c = template.Context({'first': '', 'second': False})
|
|
||||||
t = template.Template('<h1>{% firstof first second third %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testFirstOfWorksWithInts(self):
|
|
||||||
"Can a firstof tag display an integer?"
|
|
||||||
c = template.Context({'first': 1, 'second': False})
|
|
||||||
t = template.Template('<h1>{% firstof first second %}</h1>')
|
|
||||||
expected = '<h1>1</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class IfStatementCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testSingleIfStatementTrue(self):
|
|
||||||
"An if statement should display its contents if the test evaluates true"
|
|
||||||
c = template.Context({'test':True})
|
|
||||||
t = template.Template('<h1>{% if test %}Yes{% endif %}</h1>')
|
|
||||||
expected = '<h1>Yes</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testSingleIfStatementFalse(self):
|
|
||||||
"An if statement should not display its contents if the test is false"
|
|
||||||
c = template.Context({'test':False})
|
|
||||||
t = template.Template('<h1>{% if test %}Should not see this{% endif %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNestedIfStatementTrueThenTrue(self):
|
|
||||||
"Nested if statements should work properly (case 1)"
|
|
||||||
c = template.Context({'test1':True, 'test2':True})
|
|
||||||
t = template.Template('<h1>{% if test1 %} First {% if test2 %} Second {% endif %} First again {% endif %}</h1>')
|
|
||||||
expected = '<h1> First Second First again </h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNestedIfStatementTrueThenFalse(self):
|
|
||||||
"Nested if statements should work properly (case 2)"
|
|
||||||
c = template.Context({'test1':True, 'test2':False})
|
|
||||||
t = template.Template('<h1>{% if test1 %} First {% if test2 %} Second {% endif %} First again {% endif %}</h1>')
|
|
||||||
expected = '<h1> First First again </h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNestedIfStatementFalseThenTrue(self):
|
|
||||||
"Nested if statements should work properly (case 3)"
|
|
||||||
c = template.Context({'test1':False, 'test2':True})
|
|
||||||
t = template.Template('<h1>{% if test1 %} First {% if test2 %} Second {% endif %} First again {% endif %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNestedIfStatementFalseThenFalse(self):
|
|
||||||
"Nested if statements should work properly (case 4)"
|
|
||||||
c = template.Context({'test1':False, 'test2':False})
|
|
||||||
t = template.Template('<h1>{% if test1 %} First {% if test2 %} Second {% endif %} First again {% endif %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testElseIfTrue(self):
|
|
||||||
"An else statement should not execute if the test evaluates to true"
|
|
||||||
c = template.Context({'test':True})
|
|
||||||
t = template.Template('<h1>{% if test %}Correct{% else %}Incorrect{% endif %}</h1>')
|
|
||||||
expected = '<h1>Correct</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testElseIfFalse(self):
|
|
||||||
"An else statement should execute if the test evaluates to false"
|
|
||||||
c = template.Context({'test':False})
|
|
||||||
t = template.Template('<h1>{% if test %}Incorrect{% else %}Correct{% endif %}</h1>')
|
|
||||||
expected = '<h1>Correct</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNonClosedIfTag(self):
|
|
||||||
"Raise TemplateSyntaxError for non-closed 'if' tags"
|
|
||||||
c = template.Context({'test':True})
|
|
||||||
t = '<h1>{% if test %}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testNonexistentTest(self):
|
|
||||||
"Fail silently when an if statement accesses a nonexistent test"
|
|
||||||
c = template.Context({'var':'value'})
|
|
||||||
t = template.Template('<h1>{% if nonexistent %}Hello{% endif %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testIfTagNoArgs(self):
|
|
||||||
"If statements must have one argument (case 1)"
|
|
||||||
t = '<h1>{% if %}Hello{% endif %}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testIfTagManyArgs(self):
|
|
||||||
"If statements must have one argument (case 2)"
|
|
||||||
t = '<h1>{% if multiple tests %}Hello{% endif %}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testAttributeAccessInIfNode(self):
|
|
||||||
"An if node should resolve a variable's attributes before checking it as a test"
|
|
||||||
class AClass: pass
|
|
||||||
obj = AClass()
|
|
||||||
obj.article = AClass()
|
|
||||||
obj.article.section = AClass()
|
|
||||||
obj.article.section.title = 'Headline'
|
|
||||||
c = template.Context({'obj':obj})
|
|
||||||
t = template.Template('<h1>{% if obj.article.section.title %}Hello{% endif %}</h1>')
|
|
||||||
expected = '<h1>Hello</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
t = template.Template('<h1>{% if obj.article.section.not_here %}Hello{% endif %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testIfNot(self):
|
|
||||||
"If statements supports 'not' as an optional argument"
|
|
||||||
t = template.Template('{% if not a %}Not a{% endif %}')
|
|
||||||
c = template.Context({'a': False})
|
|
||||||
expected = 'Not a'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
c['a'] = True
|
|
||||||
expected = ''
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testIfOr(self):
|
|
||||||
"If statements support 'or'"
|
|
||||||
t = template.Template('{% if a or b %}Hello{% endif %}')
|
|
||||||
c = template.Context({'a': False, 'b': True})
|
|
||||||
expected = 'Hello'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
c['b'] = False
|
|
||||||
expected = ''
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testIfOrNot(self):
|
|
||||||
"If statements support 'or' clauses with optional 'not's"
|
|
||||||
t = template.Template('{% if a or not b or c%}Hello{% endif %}')
|
|
||||||
c = template.Context({'a': False, 'b': False, 'c': False})
|
|
||||||
expected = 'Hello'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
c['b'] = True
|
|
||||||
expected = ''
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class ForLoopCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testNormalForLoop(self):
|
|
||||||
"A for loop should work as expected, given one or more values"
|
|
||||||
c = template.Context({'pieces': ('1', '2', '3')})
|
|
||||||
t = template.Template('<h1>{% for piece in pieces %}{{ piece }}{% endfor %}</h1>')
|
|
||||||
expected = '<h1>123</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testBlankForLoop(self):
|
|
||||||
"A for loop should work as expected, given an empty list"
|
|
||||||
c = template.Context({'pieces': []})
|
|
||||||
t = template.Template('<h1>{% for piece in pieces %}{{ piece }}{% endfor %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testInvalidForTagFourWords(self):
|
|
||||||
"Raise TemplateSyntaxError if a 'for' statement is not exactly 4 words"
|
|
||||||
t = '<h1>{% for article %}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testInvalidForTagThirdWord(self):
|
|
||||||
"Raise TemplateSyntaxError if 3rd word in a 'for' statement isn't 'in'"
|
|
||||||
t = '<h1>{% for article NOTIN blah %}{% endfor %}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testNonClosedForTag(self):
|
|
||||||
"Raise TemplateSyntaxError for non-closed 'for' tags"
|
|
||||||
t = '<h1>{% for i in numbers %}{{ i }}</h1>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testNonexistentVariable1(self):
|
|
||||||
"Fail silently in loops with nonexistent variables in defn"
|
|
||||||
c = template.Context({'var':'value'})
|
|
||||||
t = template.Template('<h1>{% for i in nonexistent %}<p>{{ var }}</p>{% endfor %}</h1>')
|
|
||||||
expected = '<h1></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNonexistentVariable2(self):
|
|
||||||
"Raise TemplateSyntaxError in loops with nonexistent variables in loop"
|
|
||||||
c = template.Context({'set':('val1', 'val2')})
|
|
||||||
t = template.Template('<h1>{% for i in set %}<p>{{ nonexistent }}</p>{% endfor %}</h1>')
|
|
||||||
expected = '<h1><p></p><p></p></h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testAttributeAccessInForNode(self):
|
|
||||||
"A for node should resolve a variable's attributes before looping through it"
|
|
||||||
c = template.Context({'article': {'authors':('Simon', 'Adrian')}})
|
|
||||||
t = template.Template('<p>{% for i in article.authors %}{{ i }}{% endfor %}</p>')
|
|
||||||
self.assertEqual('<p>SimonAdrian</p>', t.render(c))
|
|
||||||
t = template.Template('<p>{% for i in article.nonexistent %}{{ i }}{% endfor %}</p>')
|
|
||||||
self.assertEqual('<p></p>', t.render(c))
|
|
||||||
|
|
||||||
def testForLoopFirst(self):
|
|
||||||
"A for loop's 'first' variable should work as expected"
|
|
||||||
c = template.Context({'pieces': ('1', '2', '3')})
|
|
||||||
t = template.Template('<h1>{% for piece in pieces %}{% if forloop.first %}<h2>First</h2>{% endif %}{{ piece }}{% endfor %}</h1>')
|
|
||||||
expected = '<h1><h2>First</h2>123</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testForLoopLast(self):
|
|
||||||
"A for loop's 'last' variable should work as expected"
|
|
||||||
c = template.Context({'pieces': ('1', '2', '3')})
|
|
||||||
t = template.Template('<h1>{% for piece in pieces %}{% if forloop.last %}<h2>Last</h2>{% endif %}{{ piece }}{% endfor %}</h1>')
|
|
||||||
expected = '<h1>12<h2>Last</h2>3</h1>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class CycleNodeCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testNormalUsage(self):
|
|
||||||
"A cycle tag should work as expected"
|
|
||||||
c = template.Context({'set':range(10)})
|
|
||||||
t = template.Template('{% for i in set %}{% cycle red, green %}-{{ i }} {% endfor %}')
|
|
||||||
expected = 'red-0 green-1 red-2 green-3 red-4 green-5 red-6 green-7 red-8 green-9 '
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNoArguments(self):
|
|
||||||
"Raise TemplateSyntaxError in cycle tags with no arguments"
|
|
||||||
t = '{% cycle %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testOneArgument(self):
|
|
||||||
"Raise TemplateSyntaxError in cycle tags with only one argument"
|
|
||||||
t = '{% cycle hello %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testExtraInitialSpaces(self):
|
|
||||||
"Extra spaces around cycle tags and their arguments should be ignored"
|
|
||||||
c = template.Context({'set':range(5)})
|
|
||||||
t = template.Template('{% for i in set %}{% cycle red, green %}{% endfor %}')
|
|
||||||
expected = 'redgreenredgreenred'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
class TemplateTagNodeCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testNormalUsage(self):
|
|
||||||
"A templatetag tag should work as expected"
|
|
||||||
c = template.Context()
|
|
||||||
t = template.Template('{% templatetag openblock %}{% templatetag closeblock %}{% templatetag openvariable %}{% templatetag closevariable %}')
|
|
||||||
expected = '{%%}{{}}'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testNoArguments(self):
|
|
||||||
"Raise TemplateSyntaxError in templatetag tags with no arguments"
|
|
||||||
t = '{% templatetag %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testTwoArguments(self):
|
|
||||||
"Raise TemplateSyntaxError in templatetag tags with more than one argument"
|
|
||||||
t = '{% templatetag hello goodbye %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
t = '{% templatetag hello goodbye helloagain %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
def testBadArgument(self):
|
|
||||||
"Raise TemplateSyntaxError in templatetag tags with invalid arguments"
|
|
||||||
t = '{% templatetag hello %}'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
class PluginFilterCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def custom_filter(self, value, arg):
|
|
||||||
"Temporary filter used to verify the filter plugin system is working"
|
|
||||||
return "_%s_%s_" % (value, arg)
|
|
||||||
|
|
||||||
def testPluginFilter(self):
|
|
||||||
"Plugin support allows for custom filters"
|
|
||||||
template.register_filter('unittest', self.custom_filter, True)
|
|
||||||
c = template.Context({'var':'value'})
|
|
||||||
t = template.Template('<body>{{ var|unittest:"hello" }}</body>')
|
|
||||||
expected = '<body>_value_hello_</body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
template.unregister_filter('unittest')
|
|
||||||
|
|
||||||
def testUnregisterPluginFilter(self):
|
|
||||||
"Plugin support allows custom filters to be unregistered"
|
|
||||||
template.register_filter('unittest', self.custom_filter, True)
|
|
||||||
c = template.Context({'var':'value'})
|
|
||||||
t = template.Template('<body>{{ var|unittest:"hello" }}</body>')
|
|
||||||
rendered = t.render(c) # should run with no exception
|
|
||||||
template.unregister_filter('unittest')
|
|
||||||
|
|
||||||
class PluginTagCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
class CustomNode(template.Node):
|
|
||||||
"Prints argument"
|
|
||||||
def __init__(self, arg):
|
|
||||||
self.arg = arg
|
|
||||||
|
|
||||||
def render(self, context):
|
|
||||||
return '_%s_' % self.arg
|
|
||||||
|
|
||||||
def do_custom_node(self, parser, token):
|
|
||||||
"Handle the 'unittest' custom tag"
|
|
||||||
bits = token.contents.split()
|
|
||||||
return self.CustomNode(bits[1])
|
|
||||||
|
|
||||||
def testPluginTag(self):
|
|
||||||
"Plugin support allows for custom tags"
|
|
||||||
template.register_tag('unittest', self.do_custom_node)
|
|
||||||
c = template.Context({})
|
|
||||||
t = template.Template('<body>{% unittest hello %}</body>')
|
|
||||||
expected = '<body>_hello_</body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
template.unregister_tag('unittest')
|
|
||||||
|
|
||||||
def testUnregisterPluginTag(self):
|
|
||||||
"Plugin support allows custom tags to be unregistered"
|
|
||||||
template.register_tag('unittest', self.do_custom_node)
|
|
||||||
c = template.Context({})
|
|
||||||
t = template.Template('<body>{% unittest hello %}</body>')
|
|
||||||
rendered = t.render(c) # should run with no exception
|
|
||||||
del(t)
|
|
||||||
template.unregister_tag('unittest')
|
|
||||||
t = '<body>{% unittest hello %}</body>'
|
|
||||||
self.assertRaises(template.TemplateSyntaxError, template.Template, t)
|
|
||||||
|
|
||||||
class ContextUsageCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testVariableContext2(self):
|
|
||||||
"Variables should fall through additional block-level contexts"
|
|
||||||
c = template.Context({'global':'out', 'set': ('1', '2', '3')})
|
|
||||||
t = template.Template('<body><h1>{{ global }}</h1>{% for i in set %}<p>{{ i }} {{ global }}</p>{% endfor %}</body>')
|
|
||||||
expected = '<body><h1>out</h1><p>1 out</p><p>2 out</p><p>3 out</p></body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableContext2(self):
|
|
||||||
"Variables set within a block statement override like-named variables within their scope"
|
|
||||||
c = template.Context({'i':'out', 'set': ('1', '2', '3')})
|
|
||||||
t = template.Template('<body><h1>{{ i }}</h1>{% for i in set %}<p>{{ i }}</p>{% endfor %}{{ i }}</body>')
|
|
||||||
expected = '<body><h1>out</h1><p>1</p><p>2</p><p>3</p>out</body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testVariableContextDelete(self):
|
|
||||||
"Variables can be deleted from the current context"
|
|
||||||
c = template.Context({'a':'first', 'b':'second'})
|
|
||||||
del c['a']
|
|
||||||
self.assertEqual(c.__repr__(), template.Context({'b':'second'}).__repr__())
|
|
||||||
|
|
||||||
def testInvalidVariableContextDelete(self):
|
|
||||||
"Raise KeyError if code tries to delete a variable that doesn't exist in the current context"
|
|
||||||
c = template.Context({'a':'first'})
|
|
||||||
self.assertRaises(KeyError, c.__delitem__, 'b')
|
|
||||||
|
|
||||||
class AdvancedUsageCheck(unittest.TestCase):
|
|
||||||
|
|
||||||
def testIfInsideFor(self):
|
|
||||||
"An if statement should be executed repeatedly inside a for statement"
|
|
||||||
c = template.Context({'set':(True, False, True, True, False)})
|
|
||||||
t = template.Template('<ul>{% for i in set %}{% if i %}<li>1</li>{% endif %}{% endfor %}</ul>')
|
|
||||||
expected = '<ul><li>1</li><li>1</li><li>1</li></ul>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testIfElseInsideFor(self):
|
|
||||||
"An if/else statement should be executed repeatedly inside a for statement"
|
|
||||||
c = template.Context({'set':(True, False, True, True, False)})
|
|
||||||
t = template.Template('<ul>{% for i in set %}<li>{% if i %}1{% else %}0{% endif %}</li>{% endfor %}</ul>')
|
|
||||||
expected = '<ul><li>1</li><li>0</li><li>1</li><li>1</li><li>0</li></ul>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testForInsideIf_True(self):
|
|
||||||
"A for loop inside an if statement should be executed if the test=true"
|
|
||||||
c = template.Context({'test':True, 'set':('1', '2', '3')})
|
|
||||||
t = template.Template('<body>{% if test %}<ul>{% for i in set %}<li>{{ i }}</li>{% endfor %}</ul>{% endif %}</body>')
|
|
||||||
expected = '<body><ul><li>1</li><li>2</li><li>3</li></ul></body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testForInsideIf_False(self):
|
|
||||||
"A for loop inside an if statement shouldn't be executed if the test=false"
|
|
||||||
c = template.Context({'test':False, 'set':('1', '2', '3')})
|
|
||||||
t = template.Template('<body>{% if test %}<ul>{% for i in set %}<li>{{ i }}</li>{% endfor %}</ul>{% endif %}</body>')
|
|
||||||
expected = '<body></body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testForInsideIfInsideFor(self):
|
|
||||||
"A for loop inside an if statement inside a for loop should work properly"
|
|
||||||
c = template.Context({'set1': (True, False, False, False, True), 'set2': ('1', '2', '3')})
|
|
||||||
t = template.Template('<body>{% for i in set1 %}{% if i %}{% for j in set2 %}{{ j }}{% endfor %}{% endif %}{% endfor %}</body>')
|
|
||||||
expected = '<body>123123</body>'
|
|
||||||
self.assertEqual(expected, t.render(c))
|
|
||||||
|
|
||||||
def testMultipleRendersWhenCompiled(self):
|
|
||||||
"A template can render multiple contexts without having to be recompiled"
|
|
||||||
t = template.Template('<body>{% for i in set1 %}{% if i %}{% for j in set2 %}{{ j }}{% endfor %}{% endif %}{% endfor %}</body>')
|
|
||||||
c = template.Context({'set1': (True, False, False, False, False), 'set2': ('1', '2', '3')})
|
|
||||||
self.assertEqual('<body>123</body>', t.render(c))
|
|
||||||
c = template.Context({'set1': (True, True, False, False, False), 'set2': ('1', '2', '3')})
|
|
||||||
self.assertEqual('<body>123123</body>', t.render(c))
|
|
||||||
c = template.Context({'set1': (True, True, True, False, False), 'set2': ('1', '2', '3')})
|
|
||||||
self.assertEqual('<body>123123123</body>', t.render(c))
|
|
||||||
c = template.Context({'set1': (True, True, True, True, False), 'set2': ('1', '2', '3')})
|
|
||||||
self.assertEqual('<body>123123123123</body>', t.render(c))
|
|
||||||
c = template.Context({'set1': (True, True, True, True, True), 'set2': ('1', '2', '3')})
|
|
||||||
self.assertEqual('<body>123123123123123</body>', t.render(c))
|
|
||||||
|
|
||||||
def tests():
|
|
||||||
s = unittest.TestLoader().loadTestsFromName(__name__)
|
|
||||||
unittest.TextTestRunner(verbosity=0).run(s)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
tests()
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
# Unit tests for cache framework
|
||||||
|
# Uses whatever cache backend is set in the test settings file.
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
|
import time
|
||||||
|
|
||||||
|
# functions/classes for complex data type tests
|
||||||
|
def f():
|
||||||
|
return 42
|
||||||
|
class C:
|
||||||
|
def m(n):
|
||||||
|
return 24
|
||||||
|
|
||||||
|
# simple set/get
|
||||||
|
cache.set("key", "value")
|
||||||
|
assert cache.get("key") == "value"
|
||||||
|
|
||||||
|
# get with non-existant keys
|
||||||
|
assert cache.get("does not exist") is None
|
||||||
|
assert cache.get("does not exist", "bang!") == "bang!"
|
||||||
|
|
||||||
|
# get_many
|
||||||
|
cache.set('a', 'a')
|
||||||
|
cache.set('b', 'b')
|
||||||
|
cache.set('c', 'c')
|
||||||
|
cache.set('d', 'd')
|
||||||
|
assert cache.get_many(['a', 'c', 'd']) == {'a' : 'a', 'c' : 'c', 'd' : 'd'}
|
||||||
|
assert cache.get_many(['a', 'b', 'e']) == {'a' : 'a', 'b' : 'b'}
|
||||||
|
|
||||||
|
# delete
|
||||||
|
cache.set("key1", "spam")
|
||||||
|
cache.set("key2", "eggs")
|
||||||
|
assert cache.get("key1") == "spam"
|
||||||
|
cache.delete("key1")
|
||||||
|
assert cache.get("key1") is None
|
||||||
|
assert cache.get("key2") == "eggs"
|
||||||
|
|
||||||
|
# has_key
|
||||||
|
cache.set("hello", "goodbye")
|
||||||
|
assert cache.has_key("hello") == True
|
||||||
|
assert cache.has_key("goodbye") == False
|
||||||
|
|
||||||
|
# test data types
|
||||||
|
stuff = {
|
||||||
|
'string' : 'this is a string',
|
||||||
|
'int' : 42,
|
||||||
|
'list' : [1, 2, 3, 4],
|
||||||
|
'tuple' : (1, 2, 3, 4),
|
||||||
|
'dict' : {'A': 1, 'B' : 2},
|
||||||
|
'function' : f,
|
||||||
|
'class' : C,
|
||||||
|
}
|
||||||
|
for (key, value) in stuff.items():
|
||||||
|
cache.set(key, value)
|
||||||
|
assert cache.get(key) == value
|
||||||
|
|
||||||
|
# expiration
|
||||||
|
cache.set('expire', 'very quickly', 1)
|
||||||
|
time.sleep(2)
|
||||||
|
assert cache.get("expire") == None
|
|
@ -0,0 +1,209 @@
|
||||||
|
from django.core import template, template_loader
|
||||||
|
|
||||||
|
# Helper objects for template tests
|
||||||
|
class SomeClass:
|
||||||
|
def __init__(self):
|
||||||
|
self.otherclass = OtherClass()
|
||||||
|
|
||||||
|
def method(self):
|
||||||
|
return "SomeClass.method"
|
||||||
|
|
||||||
|
def method2(self, o):
|
||||||
|
return o
|
||||||
|
|
||||||
|
class OtherClass:
|
||||||
|
def method(self):
|
||||||
|
return "OtherClass.method"
|
||||||
|
|
||||||
|
# SYNTAX --
|
||||||
|
# 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
|
||||||
|
TEMPLATE_TESTS = {
|
||||||
|
|
||||||
|
### BASIC SYNTAX ##########################################################
|
||||||
|
|
||||||
|
# Plain text should go through the template parser untouched
|
||||||
|
'basic-syntax01': ("something cool", {}, "something cool"),
|
||||||
|
|
||||||
|
# Variables should be replaced with their value in the current context
|
||||||
|
'basic-syntax02': ("{{ headline }}", {'headline':'Success'}, "Success"),
|
||||||
|
|
||||||
|
# More than one replacement variable is allowed in a template
|
||||||
|
'basic-syntax03': ("{{ first }} --- {{ second }}", {"first" : 1, "second" : 2}, "1 --- 2"),
|
||||||
|
|
||||||
|
# Fail silently when a variable is not found in the current context
|
||||||
|
'basic-syntax04': ("as{{ missing }}df", {}, "asdf"),
|
||||||
|
|
||||||
|
# A variable may not contain more than one word
|
||||||
|
'basic-syntax06': ("{{ multi word variable }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError for empty variable tags
|
||||||
|
'basic-syntax07': ("{{ }}", {}, template.TemplateSyntaxError),
|
||||||
|
'basic-syntax08': ("{{ }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Attribute syntax allows a template to call an object's attribute
|
||||||
|
'basic-syntax09': ("{{ var.method }}", {"var": SomeClass()}, "SomeClass.method"),
|
||||||
|
|
||||||
|
# Multiple levels of attribute access are allowed
|
||||||
|
'basic-syntax10': ("{{ var.otherclass.method }}", {"var": SomeClass()}, "OtherClass.method"),
|
||||||
|
|
||||||
|
# Fail silently when a variable's attribute isn't found
|
||||||
|
'basic-syntax11': ("{{ var.blech }}", {"var": SomeClass()}, ""),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError when trying to access a variable beginning with an underscore
|
||||||
|
'basic-syntax12': ("{{ var.__dict__ }}", {"var": SomeClass()}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError when trying to access a variable containing an illegal character
|
||||||
|
'basic-syntax13': ("{{ va>r }}", {}, template.TemplateSyntaxError),
|
||||||
|
'basic-syntax14': ("{{ (var.r) }}", {}, template.TemplateSyntaxError),
|
||||||
|
'basic-syntax15': ("{{ sp%am }}", {}, template.TemplateSyntaxError),
|
||||||
|
'basic-syntax16': ("{{ eggs! }}", {}, template.TemplateSyntaxError),
|
||||||
|
'basic-syntax17': ("{{ moo? }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Attribute syntax allows a template to call a dictionary key's value
|
||||||
|
'basic-syntax18': ("{{ foo.bar }}", {"foo" : {"bar" : "baz"}}, "baz"),
|
||||||
|
|
||||||
|
# Fail silently when a variable's dictionary key isn't found
|
||||||
|
'basic-syntax19': ("{{ foo.spam }}", {"foo" : {"bar" : "baz"}}, ""),
|
||||||
|
|
||||||
|
# Fail silently when accessing a non-simple method
|
||||||
|
'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ""),
|
||||||
|
|
||||||
|
# Basic filter usage
|
||||||
|
'basic-syntax21': ("{{ var|upper }}", {"var": "Django is the greatest!"}, "DJANGO IS THE GREATEST!"),
|
||||||
|
|
||||||
|
# Chained filters
|
||||||
|
'basic-syntax22': ("{{ var|upper|lower }}", {"var": "Django is the greatest!"}, "django is the greatest!"),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError for space between a variable and filter pipe
|
||||||
|
'basic-syntax23': ("{{ var |upper }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError for space after a filter pipe
|
||||||
|
'basic-syntax24': ("{{ var| upper }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError for a nonexistent filter
|
||||||
|
'basic-syntax25': ("{{ var|does_not_exist }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError when trying to access a filter containing an illegal character
|
||||||
|
'basic-syntax26': ("{{ var|fil(ter) }}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError for invalid block tags
|
||||||
|
'basic-syntax27': ("{% nothing_to_see_here %}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise TemplateSyntaxError for empty block tags
|
||||||
|
'basic-syntax28': ("{% %}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
### INHERITANCE TESTS #####################################################
|
||||||
|
|
||||||
|
# Standard template with no inheritance
|
||||||
|
'inheritance01': ("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", {}, '1_3_'),
|
||||||
|
|
||||||
|
# Standard two-level inheritance
|
||||||
|
'inheritance02': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
|
||||||
|
|
||||||
|
# Three-level with no redefinitions on third level
|
||||||
|
'inheritance03': ("{% extends 'inheritance02' %}", {}, '1234'),
|
||||||
|
|
||||||
|
# Two-level with no redefinitions on second level
|
||||||
|
'inheritance04': ("{% extends 'inheritance01' %}", {}, '1_3_'),
|
||||||
|
|
||||||
|
# Two-level with double quotes instead of single quotes
|
||||||
|
'inheritance05': ('{% extends "inheritance02" %}', {}, '1234'),
|
||||||
|
|
||||||
|
# Three-level with variable parent-template name
|
||||||
|
'inheritance06': ("{% extends foo %}", {'foo': 'inheritance02'}, '1234'),
|
||||||
|
|
||||||
|
# Two-level with one block defined, one block not defined
|
||||||
|
'inheritance07': ("{% extends 'inheritance01' %}{% block second %}5{% endblock %}", {}, '1_35'),
|
||||||
|
|
||||||
|
# Three-level with one block defined on this level, two blocks defined next level
|
||||||
|
'inheritance08': ("{% extends 'inheritance02' %}{% block second %}5{% endblock %}", {}, '1235'),
|
||||||
|
|
||||||
|
# Three-level with second and third levels blank
|
||||||
|
'inheritance09': ("{% extends 'inheritance04' %}", {}, '1_3_'),
|
||||||
|
|
||||||
|
# Three-level with space NOT in a block -- should be ignored
|
||||||
|
'inheritance10': ("{% extends 'inheritance04' %} ", {}, '1_3_'),
|
||||||
|
|
||||||
|
# Three-level with both blocks defined on this level, but none on second level
|
||||||
|
'inheritance11': ("{% extends 'inheritance04' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
|
||||||
|
|
||||||
|
# Three-level with this level providing one and second level providing the other
|
||||||
|
'inheritance12': ("{% extends 'inheritance07' %}{% block first %}2{% endblock %}", {}, '1235'),
|
||||||
|
|
||||||
|
# Three-level with this level overriding second level
|
||||||
|
'inheritance13': ("{% extends 'inheritance02' %}{% block first %}a{% endblock %}{% block second %}b{% endblock %}", {}, '1a3b'),
|
||||||
|
|
||||||
|
# A block defined only in a child template shouldn't be displayed
|
||||||
|
'inheritance14': ("{% extends 'inheritance01' %}{% block newblock %}NO DISPLAY{% endblock %}", {}, '1_3_'),
|
||||||
|
|
||||||
|
# A block within another block
|
||||||
|
'inheritance15': ("{% extends 'inheritance01' %}{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}", {}, '12inner3_'),
|
||||||
|
|
||||||
|
# A block within another block (level 2)
|
||||||
|
'inheritance16': ("{% extends 'inheritance15' %}{% block inner %}out{% endblock %}", {}, '12out3_'),
|
||||||
|
|
||||||
|
# {% load %} tag (parent -- setup for exception04)
|
||||||
|
'inheritance17': ("{% load testtags %}{% block first %}1234{% endblock %}", {}, '1234'),
|
||||||
|
|
||||||
|
# {% load %} tag (standard usage, without inheritance)
|
||||||
|
'inheritance18': ("{% load testtags %}{% echo this that theother %}5678", {}, 'this that theother5678'),
|
||||||
|
|
||||||
|
# {% load %} tag (within a child template)
|
||||||
|
'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'),
|
||||||
|
|
||||||
|
### EXCEPTION TESTS #######################################################
|
||||||
|
|
||||||
|
# Raise exception for invalid template name
|
||||||
|
'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise exception for invalid template name (in variable)
|
||||||
|
'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise exception for extra {% extends %} tags
|
||||||
|
'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
|
||||||
|
|
||||||
|
# Raise exception for custom tags used in child with {% load %} tag in parent, not in child
|
||||||
|
'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
|
||||||
|
}
|
||||||
|
|
||||||
|
# This replaces the standard template_loader.
|
||||||
|
def test_template_loader(template_name, template_dirs=None):
|
||||||
|
try:
|
||||||
|
return TEMPLATE_TESTS[template_name][0]
|
||||||
|
except KeyError:
|
||||||
|
raise template.TemplateDoesNotExist, template_name
|
||||||
|
|
||||||
|
def run_tests(verbosity=0, standalone=False):
|
||||||
|
template_loader.load_template_source, old_template_loader = test_template_loader, template_loader.load_template_source
|
||||||
|
failed_tests = []
|
||||||
|
tests = TEMPLATE_TESTS.items()
|
||||||
|
tests.sort()
|
||||||
|
for name, vals in tests:
|
||||||
|
try:
|
||||||
|
output = template_loader.get_template(name).render(template.Context(vals[1]))
|
||||||
|
except Exception, e:
|
||||||
|
if e.__class__ == vals[2]:
|
||||||
|
if verbosity:
|
||||||
|
print "Template test: %s -- Passed" % name
|
||||||
|
else:
|
||||||
|
if verbosity:
|
||||||
|
print "Template test: %s -- FAILED. Got %s, exception: %s" % (name, e.__class__, e)
|
||||||
|
failed_tests.append(name)
|
||||||
|
continue
|
||||||
|
if output == vals[2]:
|
||||||
|
if verbosity:
|
||||||
|
print "Template test: %s -- Passed" % name
|
||||||
|
else:
|
||||||
|
if verbosity:
|
||||||
|
print "Template test: %s -- FAILED. Expected %r, got %r" % (name, vals[2], output)
|
||||||
|
failed_tests.append(name)
|
||||||
|
template_loader.load_template_source = old_template_loader
|
||||||
|
|
||||||
|
if failed_tests and not standalone:
|
||||||
|
msg = "Template tests %s failed." % failed_tests
|
||||||
|
if not verbosity:
|
||||||
|
msg += " Re-run tests with -v1 to see actual failures"
|
||||||
|
raise Exception, msg
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_tests(1, True)
|
|
@ -8,12 +8,13 @@ import os, sys, time, traceback
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
APP_NAME = 'testapp'
|
APP_NAME = 'testapp'
|
||||||
|
OTHER_TESTS_DIR = "othertests"
|
||||||
TEST_DATABASE_NAME = 'django_test_db'
|
TEST_DATABASE_NAME = 'django_test_db'
|
||||||
|
|
||||||
error_list = []
|
error_list = []
|
||||||
def log_error(model_name, title, description):
|
def log_error(model_name, title, description):
|
||||||
error_list.append({
|
error_list.append({
|
||||||
'title': "%r model: %s" % (model_name, title),
|
'title': "%r module: %s" % (model_name, title),
|
||||||
'description': description,
|
'description': description,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -91,6 +92,7 @@ class TestRunner:
|
||||||
management.init()
|
management.init()
|
||||||
|
|
||||||
# Run the tests for each test model.
|
# Run the tests for each test model.
|
||||||
|
self.output(1, "Running app tests")
|
||||||
for model_name in get_test_models():
|
for model_name in get_test_models():
|
||||||
self.output(1, "%s model: Importing" % model_name)
|
self.output(1, "%s model: Importing" % model_name)
|
||||||
try:
|
try:
|
||||||
|
@ -110,7 +112,32 @@ class TestRunner:
|
||||||
runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False)
|
runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False)
|
||||||
self.output(1, "%s model: Running tests" % model_name)
|
self.output(1, "%s model: Running tests" % model_name)
|
||||||
runner.run(dtest, clear_globs=True, out=sys.stdout.write)
|
runner.run(dtest, clear_globs=True, out=sys.stdout.write)
|
||||||
|
|
||||||
|
# Run the non-model tests in the other tests dir
|
||||||
|
self.output(1, "Running other tests")
|
||||||
|
other_tests_dir = os.path.join(os.path.dirname(__file__), OTHER_TESTS_DIR)
|
||||||
|
test_modules = [f[:-3] for f in os.listdir(other_tests_dir) if f.endswith('.py') and not f.startswith('__init__')]
|
||||||
|
for module in test_modules:
|
||||||
|
self.output(1, "%s module: Importing" % module)
|
||||||
|
try:
|
||||||
|
mod = __import__("othertests." + module, '', '', [''])
|
||||||
|
except Exception, e:
|
||||||
|
log_error(module, "Error while importing", ''.join(traceback.format_exception(*sys.exc_info())[1:]))
|
||||||
|
continue
|
||||||
|
if mod.__doc__:
|
||||||
|
p = doctest.DocTestParser()
|
||||||
|
dtest = p.get_doctest(mod.__doc__, mod.__dict__, module, None, None)
|
||||||
|
runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False)
|
||||||
|
self.output(1, "%s module: runing tests" % module)
|
||||||
|
runner.run(dtest, clear_globs=True, out=sys.stdout.write)
|
||||||
|
if hasattr(mod, "run_tests") and callable(mod.run_tests):
|
||||||
|
self.output(1, "%s module: runing tests" % module)
|
||||||
|
try:
|
||||||
|
mod.run_tests(verbosity_level)
|
||||||
|
except Exception, e:
|
||||||
|
log_error(module, "Exception running tests", ''.join(traceback.format_exception(*sys.exc_info())[1:]))
|
||||||
|
continue
|
||||||
|
|
||||||
# Unless we're using SQLite, remove the test database to clean up after
|
# Unless we're using SQLite, remove the test database to clean up after
|
||||||
# ourselves. Connect to the previous database (not the test database)
|
# ourselves. Connect to the previous database (not the test database)
|
||||||
# to do so, because it's not allowed to delete a database while being
|
# to do so, because it's not allowed to delete a database while being
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
# Custom tag library used in conjunction with template tests
|
||||||
|
|
||||||
|
from django.core import template
|
||||||
|
|
||||||
|
class EchoNode(template.Node):
|
||||||
|
def __init__(self, contents):
|
||||||
|
self.contents = contents
|
||||||
|
|
||||||
|
def render(self, context):
|
||||||
|
return " ".join(self.contents)
|
||||||
|
|
||||||
|
def do_echo(parser, token):
|
||||||
|
return EchoNode(token.contents.split()[1:])
|
||||||
|
|
||||||
|
template.register_tag("echo", do_echo)
|
Loading…
Reference in New Issue