Merge branch 'master' of git://github.com/melinath/philo
[philo.git] / tests.py
1 from django.test import TestCase
2 from django import template
3 from django.conf import settings
4 from django.template import loader
5 from django.template.loaders import cached
6 from philo.exceptions import AncestorDoesNotExist
7 from philo.models import Node, Page, Template
8 from philo.contrib.penfield.models import Blog, BlogView, BlogEntry
9 import sys, traceback
10
11
12 class TemplateTestCase(TestCase):
13         fixtures = ['test_fixtures.json']
14         
15         def test_templates(self):
16                 "Tests to make sure that embed behaves with complex includes and extends"
17                 template_tests = self.get_template_tests()
18                 
19                 # Register our custom template loader. Shamelessly cribbed from django core regressiontests.
20                 def test_template_loader(template_name, template_dirs=None):
21                         "A custom template loader that loads the unit-test templates."
22                         try:
23                                 return (template_tests[template_name][0] , "test:%s" % template_name)
24                         except KeyError:
25                                 raise template.TemplateDoesNotExist, template_name
26                 
27                 cache_loader = cached.Loader(('test_template_loader',))
28                 cache_loader._cached_loaders = (test_template_loader,)
29                 
30                 old_template_loaders = loader.template_source_loaders
31                 loader.template_source_loaders = [cache_loader]
32                 
33                 # Turn TEMPLATE_DEBUG off, because tests assume that.
34                 old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False
35                 
36                 # Set TEMPLATE_STRING_IF_INVALID to a known string.
37                 old_invalid = settings.TEMPLATE_STRING_IF_INVALID
38                 expected_invalid_str = 'INVALID'
39                 
40                 failures = []
41                 
42                 # Run tests
43                 for name, vals in template_tests.items():
44                         xx, context, result = vals
45                         try:
46                                 test_template = loader.get_template(name)
47                                 output = test_template.render(template.Context(context))
48                         except Exception:
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))
53                                 continue
54                         if output != result:
55                                 failures.append("Template test %s -- FAILED. Expected %r, got %r" % (name, result, output))
56                 
57                 # Cleanup
58                 settings.TEMPLATE_DEBUG = old_td
59                 settings.TEMPLATE_STRING_IF_INVALID = old_invalid
60                 loader.template_source_loaders = old_template_loaders
61                 
62                 self.assertEqual(failures, [], "Tests failed:\n%s\n%s" % ('-'*70, ("\n%s\n" % ('-'*70)).join(failures)))
63         
64         
65         def get_template_tests(self):
66                 # SYNTAX --
67                 # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
68                 blog = Blog.objects.all()[0]
69                 return {
70                         # EMBED INCLUSION HANDLING
71                         
72                         'embed01': ('{{ embedded.title|safe }}', {'embedded': blog}, blog.title),
73                         'embed02': ('{{ embedded.title|safe }}{{ var1 }}{{ var2 }}', {'embedded': blog}, blog.title),
74                         'embed03': ('{{ embedded.title|safe }} is a lie!', {'embedded': blog}, '%s is a lie!' % blog.title),
75                         
76                         # Simple template structure with embed
77                         'simple01': ('{% embed penfield.blog with "embed01" %}{% embed penfield.blog 1 %}Simple{% block one %}{% endblock %}', {'blog': blog}, '%sSimple' % blog.title),
78                         'simple02': ('{% extends "simple01" %}', {}, '%sSimple' % blog.title),
79                         'simple03': ('{% embed penfield.blog with "embed000" %}', {}, settings.TEMPLATE_STRING_IF_INVALID),
80                         'simple04': ('{% embed penfield.blog 1 %}', {}, settings.TEMPLATE_STRING_IF_INVALID),
81                         
82                         # Kwargs
83                         'kwargs01': ('{% embed penfield.blog with "embed02" %}{% embed penfield.blog 1 var1="hi" var2=lo %}', {'lo': 'lo'}, '%shilo' % blog.title),
84                         
85                         # Filters/variables
86                         'filters01': ('{% embed penfield.blog with "embed02" %}{% embed penfield.blog 1 var1=hi|first var2=lo|slice:"3" %}', {'hi': ["These", "words"], 'lo': 'lower'}, '%sTheselow' % blog.title),
87                         'filters02': ('{% embed penfield.blog with "embed01" %}{% embed penfield.blog entry %}', {'entry': 1}, blog.title),
88                         
89                         # Blocky structure
90                         'block01': ('{% block one %}Hello{% endblock %}', {}, 'Hello'),
91                         'block02': ('{% extends "simple01" %}{% block one %}{% embed penfield.blog 1 %}{% endblock %}', {}, "%sSimple%s" % (blog.title, blog.title)),
92                         'block03': ('{% extends "simple01" %}{% embed penfield.blog with "embed03" %}{% block one %}{% embed penfield.blog 1 %}{% endblock %}', {}, "%sSimple%s is a lie!" % (blog.title, blog.title)),
93                         
94                         # Blocks and includes
95                         'block-include01': ('{% extends "simple01" %}{% embed penfield.blog with "embed03" %}{% block one %}{% include "simple01" %}{% embed penfield.blog 1 %}{% endblock %}', {}, "%sSimple%sSimple%s is a lie!" % (blog.title, blog.title, blog.title)),
96                         'block-include02': ('{% extends "simple01" %}{% block one %}{% include "simple04" %}{% embed penfield.blog with "embed03" %}{% include "simple04" %}{% embed penfield.blog 1 %}{% endblock %}', {}, "%sSimple%s%s is a lie!%s is a lie!" % (blog.title, blog.title, blog.title, blog.title)),
97                 }
98
99
100 class NodeURLTestCase(TestCase):
101         """Tests the features of the node_url template tag."""
102         urls = 'philo.urls'
103         fixtures = ['test_fixtures.json']
104         
105         def setUp(self):
106                 if 'south' in settings.INSTALLED_APPS:
107                         from south.management.commands.migrate import Command
108                         command = Command()
109                         command.handle(all_apps=True)
110                 
111                 self.templates = [
112                                 ("{% node_url %}", "/root/never/"),
113                                 ("{% node_url for node2 %}", "/root/blog/"),
114                                 ("{% node_url as hello %}<p>{{ hello|slice:'1:' }}</p>", "<p>root/never/</p>"),
115                                 ("{% node_url for nodes|first %}", "/root/never/"),
116                                 ("{% node_url with entry %}", settings.TEMPLATE_STRING_IF_INVALID),
117                                 ("{% node_url with entry for node2 %}", "/root/blog/2010/10/20/first-entry"),
118                                 ("{% node_url with tag for node2 %}", "/root/blog/tags/test-tag/"),
119                                 ("{% node_url with date for node2 %}", "/root/blog/2010/10/20"),
120                                 ("{% 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>"),
121                                 ("{% node_url entries_by_month year=date|date:'Y' month=date|date:'m' for node2 %}", "/root/blog/2010/10"),
122                                 ("{% node_url entries_by_year year=date|date:'Y' for node2 %}", "/root/blog/2010/"),
123                 ]
124                 
125                 nodes = Node.objects.all()
126                 blog = Blog.objects.all()[0]
127                 
128                 self.context = template.Context({
129                         'node': nodes[0],
130                         'node2': nodes[1],
131                         'nodes': nodes,
132                         'entry': BlogEntry.objects.all()[0],
133                         'tag': blog.entry_tags.all()[0],
134                         'date': blog.entry_dates['day'][0]
135                 })
136         
137         def test_nodeurl(self):
138                 for string, result in self.templates:
139                         self.assertEqual(template.Template(string).render(self.context), result)
140
141 class TreePathTestCase(TestCase):
142         urls = 'philo.urls'
143         fixtures = ['test_fixtures.json']
144         
145         def setUp(self):
146                 if 'south' in settings.INSTALLED_APPS:
147                         from south.management.commands.migrate import Command
148                         command = Command()
149                         command.handle(all_apps=True)
150         
151         def test_has_ancestor(self):
152                 root = Node.objects.get(slug='root')
153                 third = Node.objects.get(slug='third')
154                 r1 = Node.objects.get(slug='recursive1')
155                 r2 = Node.objects.get(slug='recursive2')
156                 pr1 = Node.objects.get(slug='postrecursive1')
157                 
158                 # Simple case: straight path
159                 self.assertEqual(third.has_ancestor(root), True)
160                 self.assertEqual(root.has_ancestor(root), False)
161                 self.assertEqual(root.has_ancestor(None), True)
162                 self.assertEqual(third.has_ancestor(None), True)
163                 self.assertEqual(root.has_ancestor(root, inclusive=True), True)
164                 
165                 # Recursive case
166                 self.assertEqual(r1.has_ancestor(r1), True)
167                 self.assertEqual(r1.has_ancestor(r2), True)
168                 self.assertEqual(r2.has_ancestor(r1), True)
169                 self.assertEqual(r2.has_ancestor(None), False)
170                 
171                 # Post-recursive case
172                 self.assertEqual(pr1.has_ancestor(r1), True)
173                 self.assertEqual(pr1.has_ancestor(pr1), False)
174                 self.assertEqual(pr1.has_ancestor(pr1, inclusive=True), True)
175                 self.assertEqual(pr1.has_ancestor(None), False)
176                 self.assertEqual(pr1.has_ancestor(root), False)
177         
178         def test_get_path(self):
179                 root = Node.objects.get(slug='root')
180                 third = Node.objects.get(slug='third')
181                 r1 = Node.objects.get(slug='recursive1')
182                 r2 = Node.objects.get(slug='recursive2')
183                 pr1 = Node.objects.get(slug='postrecursive1')
184                 
185                 # Simple case: straight path to None
186                 self.assertEqual(root.get_path(), 'root')
187                 self.assertEqual(third.get_path(), 'root/never/more/second/third')
188                 
189                 # Recursive case: Looped path to root None
190                 self.assertEqual(r1.get_path(), u'\u2026/recursive1/recursive2/recursive3/recursive1')
191                 self.assertEqual(pr1.get_path(), u'\u2026/recursive3/recursive1/recursive2/recursive3/postrecursive1')
192                 
193                 # Simple error case: straight invalid path
194                 self.assertRaises(AncestorDoesNotExist, root.get_path, root=third)
195                 self.assertRaises(AncestorDoesNotExist, third.get_path, root=pr1)
196                 
197                 # Recursive error case
198                 self.assertRaises(AncestorDoesNotExist, r1.get_path, root=root)
199                 self.assertRaises(AncestorDoesNotExist, pr1.get_path, root=third)