4 from django import template
5 from django.conf import settings
6 from django.contrib.contenttypes.models import ContentType
7 from django.db import connection, models
8 from django.template import loader
9 from django.template.loaders import cached
10 from django.test import TestCase
11 from django.test.utils import setup_test_template_loader, restore_template_loaders
12 from django.utils.datastructures import SortedDict
14 from philo.exceptions import AncestorDoesNotExist
15 from philo.models import Node, Page, Template, Tag
18 class TemplateTestCase(TestCase):
19 fixtures = ['test_fixtures.json']
21 def test_templates(self):
22 "Tests to make sure that embed behaves with complex includes and extends"
23 template_tests = self.get_template_tests()
25 # Register our custom template loader. Shamelessly cribbed from django/tests/regressiontests/templates/tests.py:384.
26 cache_loader = setup_test_template_loader(
27 dict([(name, t[0]) for name, t in template_tests.iteritems()]),
28 use_cached_loader=True,
32 tests = template_tests.items()
35 # Turn TEMPLATE_DEBUG off, because tests assume that.
36 old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False
38 # Set TEMPLATE_STRING_IF_INVALID to a known string.
39 old_invalid = settings.TEMPLATE_STRING_IF_INVALID
40 expected_invalid_str = 'INVALID'
43 for name, vals in tests:
44 xx, context, result = vals
46 test_template = loader.get_template(name)
47 output = test_template.render(template.Context(context))
49 exc_type, exc_value, exc_tb = sys.exc_info()
50 if exc_type != result:
51 tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb))
52 failures.append("Template test %s -- FAILED. Got %s, exception: %s\n%s" % (name, exc_type, exc_value, tb))
55 failures.append("Template test %s -- FAILED. Expected %r, got %r" % (name, result, output))
58 settings.TEMPLATE_DEBUG = old_td
59 settings.TEMPLATE_STRING_IF_INVALID = old_invalid
60 restore_template_loaders()
62 self.assertEqual(failures, [], "Tests failed:\n%s\n%s" % ('-'*70, ("\n%s\n" % ('-'*70)).join(failures)))
65 def get_template_tests(self):
67 # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
68 embedded = Tag.objects.get(pk=1)
70 # EMBED INCLUSION HANDLING
72 'embed01': ('{{ embedded.name|safe }}', {'embedded': embedded}, embedded.name),
73 'embed02': ('{{ embedded.name|safe }}{{ var1 }}{{ var2 }}', {'embedded': embedded}, embedded.name),
74 'embed03': ('{{ embedded.name|safe }} is a lie!', {'embedded': embedded}, '%s is a lie!' % embedded.name),
76 # Simple template structure with embed
77 'simple01': ('{% embed philo.tag with "embed01" %}{% embed philo.tag 1 %}Simple{% block one %}{% endblock %}', {'embedded': embedded}, '%sSimple' % embedded.name),
78 'simple02': ('{% extends "simple01" %}', {}, '%sSimple' % embedded.name),
79 'simple03': ('{% embed philo.tag with "embed000" %}', {}, settings.TEMPLATE_STRING_IF_INVALID),
80 'simple04': ('{% embed philo.tag 1 %}', {}, settings.TEMPLATE_STRING_IF_INVALID),
81 'simple05': ('{% embed philo.tag with "embed01" %}{% embed embedded %}', {'embedded': embedded}, embedded.name),
84 'kwargs01': ('{% embed philo.tag with "embed02" %}{% embed philo.tag 1 var1="hi" var2=lo %}', {'lo': 'lo'}, '%shilo' % embedded.name),
87 'filters01': ('{% embed philo.tag with "embed02" %}{% embed philo.tag 1 var1=hi|first var2=lo|slice:"3" %}', {'hi': ["These", "words"], 'lo': 'lower'}, '%sTheselow' % embedded.name),
88 'filters02': ('{% embed philo.tag with "embed01" %}{% embed philo.tag entry %}', {'entry': 1}, embedded.name),
91 'block01': ('{% block one %}Hello{% endblock %}', {}, 'Hello'),
92 'block02': ('{% extends "simple01" %}{% block one %}{% embed philo.tag 1 %}{% endblock %}', {}, "%sSimple%s" % (embedded.name, embedded.name)),
93 'block03': ('{% extends "simple01" %}{% embed philo.tag with "embed03" %}{% block one %}{% embed philo.tag 1 %}{% endblock %}', {}, "%sSimple%s is a lie!" % (embedded.name, embedded.name)),
96 'block-include01': ('{% extends "simple01" %}{% embed philo.tag with "embed03" %}{% block one %}{% include "simple01" %}{% embed philo.tag 1 %}{% endblock %}', {}, "%sSimple%sSimple%s is a lie!" % (embedded.name, embedded.name, embedded.name)),
97 'block-include02': ('{% extends "simple01" %}{% block one %}{% include "simple04" %}{% embed philo.tag with "embed03" %}{% include "simple04" %}{% embed philo.tag 1 %}{% endblock %}', {}, "%sSimple%s%s is a lie!%s is a lie!" % (embedded.name, embedded.name, embedded.name, embedded.name)),
99 # Tests for more complex situations...
100 'complex01': ('{% block one %}{% endblock %}complex{% block two %}{% endblock %}', {}, 'complex'),
101 'complex02': ('{% extends "complex01" %}', {}, 'complex'),
102 'complex03': ('{% extends "complex02" %}{% embed philo.tag with "embed01" %}', {}, 'complex'),
103 'complex04': ('{% extends "complex03" %}{% block one %}{% embed philo.tag 1 %}{% endblock %}', {}, '%scomplex' % embedded.name),
104 'complex05': ('{% extends "complex03" %}{% block one %}{% include "simple04" %}{% endblock %}', {}, '%scomplex' % embedded.name),
108 class NodeURLTestCase(TestCase):
109 """Tests the features of the node_url template tag."""
111 fixtures = ['test_fixtures.json']
115 ("{% node_url %}", "/root/second"),
116 ("{% node_url for node2 %}", "/root/second2"),
117 ("{% node_url as hello %}<p>{{ hello|slice:'1:' }}</p>", "<p>root/second</p>"),
118 ("{% node_url for nodes|first %}", "/root"),
121 nodes = Node.objects.all()
123 self.context = template.Context({
124 'node': nodes.get(slug='second'),
125 'node2': nodes.get(slug='second2'),
129 def test_nodeurl(self):
130 for string, result in self.templates:
131 self.assertEqual(template.Template(string).render(self.context), result)
133 class TreePathTestCase(TestCase):
135 fixtures = ['test_fixtures.json']
137 def assertQueryLimit(self, max, expected_result, *args, **kwargs):
138 # As a rough measure of efficiency, limit the number of queries required for a given operation.
139 settings.DEBUG = True
140 call = kwargs.pop('callable', Node.objects.get_with_path)
142 queries = len(connection.queries)
143 if isinstance(expected_result, type) and issubclass(expected_result, Exception):
144 self.assertRaises(expected_result, call, *args, **kwargs)
146 self.assertEqual(call(*args, **kwargs), expected_result)
147 queries = len(connection.queries) - queries
149 raise AssertionError('"%d" unexpectedly not less than or equal to "%s"' % (queries, max))
151 settings.DEBUG = False
153 def test_get_with_path(self):
154 root = Node.objects.get(slug='root')
155 third = Node.objects.get(slug='third')
156 second2 = Node.objects.get(slug='second2')
157 fifth = Node.objects.get(slug='fifth')
158 e = Node.DoesNotExist
161 self.assertQueryLimit(0, root, '', root=root)
162 self.assertQueryLimit(0, e, '')
163 self.assertQueryLimit(0, (root, None), '', root=root, absolute_result=False)
166 self.assertQueryLimit(1, third, 'root/second/third')
167 self.assertQueryLimit(1, third, 'second/third', root=root)
168 self.assertQueryLimit(1, third, 'root//////second/third///')
170 self.assertQueryLimit(1, e, 'root/secont/third')
171 self.assertQueryLimit(1, e, 'second/third')
173 # Non-absolute result (binary search)
174 self.assertQueryLimit(2, (second2, 'sub/path/tail'), 'root/second2/sub/path/tail', absolute_result=False)
175 self.assertQueryLimit(3, (second2, 'sub'), 'root/second2/sub/', absolute_result=False)
176 self.assertQueryLimit(2, e, 'invalid/path/1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/7/8/9/0', absolute_result=False)
177 self.assertQueryLimit(1, (root, None), 'root', absolute_result=False)
178 self.assertQueryLimit(2, (second2, None), 'root/second2', absolute_result=False)
179 self.assertQueryLimit(3, (third, None), 'root/second/third', absolute_result=False)
182 self.assertQueryLimit(1, (second2, None), 'second2', root=root, absolute_result=False)
183 self.assertQueryLimit(2, (third, None), 'second/third', root=root, absolute_result=False)
185 # Eliminate trailing slash
186 self.assertQueryLimit(2, (second2, 'sub/path/tail'), 'root/second2/sub/path/tail/', absolute_result=False)
188 # Speed increase for leaf nodes - should this be tested?
189 self.assertQueryLimit(1, (fifth, 'sub/path/tail/len/five'), 'root/second/third/fourth/fifth/sub/path/tail/len/five', absolute_result=False)
191 def test_get_path(self):
192 root = Node.objects.get(slug='root')
193 root2 = Node.objects.get(slug='root')
194 third = Node.objects.get(slug='third')
195 second2 = Node.objects.get(slug='second2')
196 fifth = Node.objects.get(slug='fifth')
197 e = AncestorDoesNotExist
199 self.assertQueryLimit(0, 'root', callable=root.get_path)
200 self.assertQueryLimit(0, '', root2, callable=root.get_path)
201 self.assertQueryLimit(1, 'root/second/third', callable=third.get_path)
202 self.assertQueryLimit(1, 'second/third', root, callable=third.get_path)
203 self.assertQueryLimit(1, e, third, callable=second2.get_path)
204 self.assertQueryLimit(1, '? - ?', root, ' - ', 'title', callable=third.get_path)
207 class ContainerTestCase(TestCase):
208 def test_simple_containers(self):
209 t = Template(code="{% container one %}{% container two %}{% container three %}{% container two %}")
210 contentlet_specs, contentreference_specs = t.containers
211 self.assertEqual(len(contentreference_specs.keyOrder), 0)
212 self.assertEqual(contentlet_specs, ['one', 'two', 'three'])
214 ct = ContentType.objects.get_for_model(Tag)
215 t = Template(code="{% container one references philo.tag as tag1 %}{% container two references philo.tag as tag2 %}{% container one references philo.tag as tag1 %}")
216 contentlet_specs, contentreference_specs = t.containers
217 self.assertEqual(len(contentlet_specs), 0)
218 self.assertEqual(contentreference_specs, SortedDict([('one', ct), ('two', ct)]))