1
from django import template
2
from django.test import TestCase
3
from django.contrib.auth.models import User
6
class TagTestCase(TestCase):
7
"""Helper class with some tag helper functions"""
9
def installTagLibrary(self, library):
10
template.libraries[library] = __import__(library)
12
def renderTemplate(self, tstr, **context):
13
tmpl = template.Template(tstr)
14
cntxt = template.Context(context)
15
return tmpl.render(cntxt)
17
class OutputTagTest(TagTestCase):
20
self.installTagLibrary('django_render.templatetags.render')
21
self.user = User.objects.create(username='test')
22
self.test_templates = os.path.join(os.path.dirname(
23
os.path.realpath(__file__)),
26
def testDefaultTemplate(self):
27
tmpl = "{% load render %}"\
29
o = self.renderTemplate(tmpl, user=self.user)
30
self.assertEqual(o.strip(), '<a href="/users/test/">test</a>')
32
def testModelTemplate(self):
33
original_templdirs = settings.TEMPLATE_DIRS
34
settings.TEMPLATE_DIRS += self.test_templates
35
tmpl = "{% load render %}"\
37
o = self.renderTemplate(tmpl, user=self.user)
38
self.assertEqual(o.strip(), 'this is user test')
39
settings.TEMPLATE_DIRS = original_templatedirs
43
tmpl = "{% load geotagging %}"\
44
"{% get_objects_nearby obj.point as nearby_objs %}"\
45
"{{ nearby_objs|length }}"
46
o = self.renderTemplate(tmpl, obj=self.denver)
47
self.assertEqual(o.strip(), "1")
49
def testShortDistance(self):
50
# DIA is about 18 miles from downtown Denver
51
short_tmpl = "{% load geotagging %}"\
52
"{% get_objects_nearby obj.point as nearby_objs within 17 %}"\
53
"{{ nearby_objs|length }}"
54
o = self.renderTemplate(short_tmpl, obj=self.denver)
55
self.assertEqual(o.strip(), "1")
56
long_tmpl = short_tmpl.replace("17", "19")
57
o = self.renderTemplate(long_tmpl, obj=self.denver)
58
self.assertEqual(o.strip(), "2")
60
def testLongDistance(self):
61
# Ann Arbor is about 1122 miles from Denver
62
short_tmpl = "{% load geotagging %}"\
63
"{% get_objects_nearby obj.point within 1115 as nearby_objs %}"\
64
"{{ nearby_objs|length }}"
65
o = self.renderTemplate(short_tmpl, obj=self.denver)
66
self.assertEqual(o.strip(), "2")
67
long_tmpl = short_tmpl.replace("1115", "1125")
68
o = self.renderTemplate(long_tmpl, obj=self.denver)
69
self.assertEqual(o.strip(), "3")
72
class SyntaxTagTest(TestCase):
74
def getNode(self, strng):
75
from django_render.templatetags.render import render
76
return render(None, template.Token(template.TOKEN_BLOCK,
79
def assertNodeException(self, strng):
80
self.assertRaises(template.TemplateSyntaxError,
83
def testInvalidSyntax(self):
84
self.assertNodeException("render")
85
self.assertNodeException("render obj bad_arg")