Added MPTTModelAdmin integration. Committed docstring changes for get_with_path.
[philo.git] / tests.py
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
7
8
9 class NodeURLTestCase(TestCase):
10         """Tests the features of the node_url template tag."""
11         urls = 'philo.urls'
12         fixtures = ['test_fixtures.json']
13         
14         def setUp(self):
15                 if 'south' in settings.INSTALLED_APPS:
16                         from south.management.commands.migrate import Command
17                         command = Command()
18                         command.handle(all_apps=True)
19                 
20                 self.templates = [
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/"),
32                 ]
33                 
34                 nodes = Node.objects.all()
35                 blog = Blog.objects.all()[0]
36                 
37                 self.context = template.Context({
38                         'node': nodes[0],
39                         'node2': nodes[1],
40                         'nodes': nodes,
41                         'entry': BlogEntry.objects.all()[0],
42                         'tag': blog.entry_tags.all()[0],
43                         'date': blog.entry_dates['day'][0]
44                 })
45         
46         def test_nodeurl(self):
47                 for string, result in self.templates:
48                         self.assertEqual(template.Template(string).render(self.context), result)
49
50 class TreePathTestCase(TestCase):
51         urls = 'philo.urls'
52         fixtures = ['test_fixtures.json']
53         
54         def setUp(self):
55                 if 'south' in settings.INSTALLED_APPS:
56                         from south.management.commands.migrate import Command
57                         command = Command()
58                         command.handle(all_apps=True)
59         
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')
66                 
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)
73                 
74                 # Recursive case
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)
79                 
80                 # Post-recursive case
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)
86         
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')
93                 
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')
97                 
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')
101                 
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)
105                 
106                 # Recursive error case
107                 self.assertRaises(AncestorDoesNotExist, r1.get_path, root=root)
108                 self.assertRaises(AncestorDoesNotExist, pr1.get_path, root=third)