1 from django.test import TestCase
2 from django import template
3 from django.conf import settings
4 from django.db import connection
5 from philo.exceptions import AncestorDoesNotExist
6 from philo.models import Node, Page, Template
7 from philo.contrib.penfield.models import Blog, BlogView, BlogEntry
10 class NodeURLTestCase(TestCase):
11 """Tests the features of the node_url template tag."""
13 fixtures = ['test_fixtures.json']
16 if 'south' in settings.INSTALLED_APPS:
17 from south.management.commands.migrate import Command
19 command.handle(all_apps=True)
22 ("{% node_url %}", "/root/second/"),
23 ("{% node_url for node2 %}", "/root/second2/"),
24 ("{% node_url as hello %}<p>{{ hello|slice:'1:' }}</p>", "<p>root/second/</p>"),
25 ("{% node_url for nodes|first %}", "/root/"),
26 ("{% node_url with entry %}", settings.TEMPLATE_STRING_IF_INVALID),
27 ("{% node_url with entry for node2 %}", "/root/second2/2010/10/20/first-entry"),
28 ("{% node_url with tag for node2 %}", "/root/second2/tags/test-tag/"),
29 ("{% node_url with date for node2 %}", "/root/second2/2010/10/20"),
30 ("{% node_url entries_by_day year=date|date:'Y' month=date|date:'m' day=date|date:'d' for node2 as goodbye %}<em>{{ goodbye|upper }}</em>", "<em>/ROOT/SECOND2/2010/10/20</em>"),
31 ("{% node_url entries_by_month year=date|date:'Y' month=date|date:'m' for node2 %}", "/root/second2/2010/10"),
32 ("{% node_url entries_by_year year=date|date:'Y' for node2 %}", "/root/second2/2010/"),
35 nodes = Node.objects.all()
36 blog = Blog.objects.all()[0]
38 self.context = template.Context({
39 'node': nodes.get(slug='second'),
40 'node2': nodes.get(slug='second2'),
42 'entry': BlogEntry.objects.all()[0],
43 'tag': blog.entry_tags.all()[0],
44 'date': blog.entry_dates['day'][0]
47 def test_nodeurl(self):
48 for string, result in self.templates:
49 self.assertEqual(template.Template(string).render(self.context), result)
51 class TreePathTestCase(TestCase):
53 fixtures = ['test_fixtures.json']
56 if 'south' in settings.INSTALLED_APPS:
57 from south.management.commands.migrate import Command
59 command.handle(all_apps=True)
61 def assertQueryLimit(self, max, expected_result, *args, **kwargs):
62 # As a rough measure of efficiency, limit the number of queries required for a given operation.
65 queries = len(connection.queries)
66 if isinstance(expected_result, type) and issubclass(expected_result, Exception):
67 self.assertRaises(expected_result, Node.objects.get_with_path, *args, **kwargs)
69 self.assertEqual(Node.objects.get_with_path(*args, **kwargs), expected_result)
70 queries = len(connection.queries) - queries
72 raise AssertionError('"%d" unexpectedly not less than or equal to "%s"' % (queries, max))
74 settings.DEBUG = False
76 def test_get_with_path(self):
77 root = Node.objects.get(slug='root')
78 third = Node.objects.get(slug='third')
79 second2 = Node.objects.get(slug='second2')
80 fifth = Node.objects.get(slug='fifth')
84 self.assertQueryLimit(0, root, '', root=root)
85 self.assertQueryLimit(0, e, '')
86 self.assertQueryLimit(0, (root, None), '', root=root, absolute_result=False)
89 self.assertQueryLimit(1, third, 'root/second/third')
90 self.assertQueryLimit(1, third, 'second/third', root=root)
91 self.assertQueryLimit(1, third, 'root//////second/third///')
93 self.assertQueryLimit(1, e, 'root/secont/third')
94 self.assertQueryLimit(1, e, 'second/third')
96 # Non-absolute result (binary search)
97 self.assertQueryLimit(2, (second2, 'sub/path/tail'), 'root/second2/sub/path/tail', absolute_result=False)
98 self.assertQueryLimit(3, (second2, 'sub/'), 'root/second2/sub/', absolute_result=False)
99 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)
100 self.assertQueryLimit(1, (root, None), 'root', absolute_result=False)
101 self.assertQueryLimit(2, (second2, None), 'root/second2', absolute_result=False)
102 self.assertQueryLimit(3, (third, None), 'root/second/third', absolute_result=False)
105 self.assertQueryLimit(1, (second2, None), 'second2', root=root, absolute_result=False)
106 self.assertQueryLimit(2, (third, None), 'second/third', root=root, absolute_result=False)
108 # Preserve trailing slash
109 self.assertQueryLimit(2, (second2, 'sub/path/tail/'), 'root/second2/sub/path/tail/', absolute_result=False)
111 # Speed increase for leaf nodes - should this be tested?
112 self.assertQueryLimit(1, (fifth, 'sub/path/tail/len/five'), 'root/second/third/fourth/fifth/sub/path/tail/len/five', absolute_result=False)