1 from django.test import TestCase
2 from django import template
3 from django.conf import settings
4 from philo.exceptions import AncestorDoesNotExist
5 from philo.models import Node, Page, Template
6 from philo.contrib.penfield.models import Blog, BlogView, BlogEntry
9 class NodeURLTestCase(TestCase):
10 """Tests the features of the node_url template tag."""
12 fixtures = ['test_fixtures.json']
15 if 'south' in settings.INSTALLED_APPS:
16 from south.management.commands.migrate import Command
18 command.handle(all_apps=True)
21 ("{% node_url %}", "/root/never/"),
22 ("{% node_url for node2 %}", "/root/blog/"),
23 ("{% node_url as hello %}<p>{{ hello|slice:'1:' }}</p>", "<p>root/never/</p>"),
24 ("{% node_url for nodes|first %}", "/root/never/"),
25 ("{% node_url with entry %}", settings.TEMPLATE_STRING_IF_INVALID),
26 ("{% node_url with entry for node2 %}", "/root/blog/2010/10/20/first-entry"),
27 ("{% node_url with tag for node2 %}", "/root/blog/tags/test-tag/"),
28 ("{% node_url with date for node2 %}", "/root/blog/2010/10/20"),
29 ("{% 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/BLOG/2010/10/20</em>"),
30 ("{% node_url entries_by_month year=date|date:'Y' month=date|date:'m' for node2 %}", "/root/blog/2010/10"),
31 ("{% node_url entries_by_year year=date|date:'Y' for node2 %}", "/root/blog/2010/"),
34 nodes = Node.objects.all()
35 blog = Blog.objects.all()[0]
37 self.context = template.Context({
41 'entry': BlogEntry.objects.all()[0],
42 'tag': blog.entry_tags.all()[0],
43 'date': blog.entry_dates['day'][0]
46 def test_nodeurl(self):
47 for string, result in self.templates:
48 self.assertEqual(template.Template(string).render(self.context), result)
50 class TreePathTestCase(TestCase):
52 fixtures = ['test_fixtures.json']
55 if 'south' in settings.INSTALLED_APPS:
56 from south.management.commands.migrate import Command
58 command.handle(all_apps=True)
60 def test_has_ancestor(self):
61 root = Node.objects.get(slug='root')
62 third = Node.objects.get(slug='third')
63 r1 = Node.objects.get(slug='recursive1')
64 r2 = Node.objects.get(slug='recursive2')
65 pr1 = Node.objects.get(slug='postrecursive1')
67 # Simple case: straight path
68 self.assertEqual(third.has_ancestor(root), True)
69 self.assertEqual(root.has_ancestor(root), False)
70 self.assertEqual(root.has_ancestor(None), True)
71 self.assertEqual(third.has_ancestor(None), True)
72 self.assertEqual(root.has_ancestor(root, inclusive=True), True)
75 self.assertEqual(r1.has_ancestor(r1), True)
76 self.assertEqual(r1.has_ancestor(r2), True)
77 self.assertEqual(r2.has_ancestor(r1), True)
78 self.assertEqual(r2.has_ancestor(None), False)
81 self.assertEqual(pr1.has_ancestor(r1), True)
82 self.assertEqual(pr1.has_ancestor(pr1), False)
83 self.assertEqual(pr1.has_ancestor(pr1, inclusive=True), True)
84 self.assertEqual(pr1.has_ancestor(None), False)
85 self.assertEqual(pr1.has_ancestor(root), False)
87 def test_get_path(self):
88 root = Node.objects.get(slug='root')
89 third = Node.objects.get(slug='third')
90 r1 = Node.objects.get(slug='recursive1')
91 r2 = Node.objects.get(slug='recursive2')
92 pr1 = Node.objects.get(slug='postrecursive1')
94 # Simple case: straight path to None
95 self.assertEqual(root.get_path(), 'root')
96 self.assertEqual(third.get_path(), 'root/never/more/second/third')
98 # Recursive case: Looped path to root None
99 self.assertEqual(r1.get_path(), u'\u2026/recursive1/recursive2/recursive3/recursive1')
100 self.assertEqual(pr1.get_path(), u'\u2026/recursive3/recursive1/recursive2/recursive3/postrecursive1')
102 # Simple error case: straight invalid path
103 self.assertRaises(AncestorDoesNotExist, root.get_path, root=third)
104 self.assertRaises(AncestorDoesNotExist, third.get_path, root=pr1)
106 # Recursive error case
107 self.assertRaises(AncestorDoesNotExist, r1.get_path, root=root)
108 self.assertRaises(AncestorDoesNotExist, pr1.get_path, root=third)