PageRenderTime 389ms CodeModel.GetById 211ms app.highlight 14ms RepoModel.GetById 161ms app.codeStats 0ms

/tests/regressiontests/comment_tests/tests/templatetag_tests.py

https://code.google.com/p/mango-py/
Python | 97 lines | 88 code | 9 blank | 0 comment | 14 complexity | c442e38877340d0f8ee78345d9a42e58 MD5 | raw file
 1from django.contrib.comments.forms import CommentForm
 2from django.contrib.comments.models import Comment
 3from django.contrib.contenttypes.models import ContentType
 4from django.template import Template, Context
 5from regressiontests.comment_tests.models import Article, Author
 6from regressiontests.comment_tests.tests import CommentTestCase
 7
 8class CommentTemplateTagTests(CommentTestCase):
 9
10    def render(self, t, **c):
11        ctx = Context(c)
12        out = Template(t).render(ctx)
13        return ctx, out
14
15    def testCommentFormTarget(self):
16        ctx, out = self.render("{% load comments %}{% comment_form_target %}")
17        self.assertEqual(out, "/post/")
18
19    def testGetCommentForm(self, tag=None):
20        t = "{% load comments %}" + (tag or "{% get_comment_form for comment_tests.article a.id as form %}")
21        ctx, out = self.render(t, a=Article.objects.get(pk=1))
22        self.assertEqual(out, "")
23        self.assertTrue(isinstance(ctx["form"], CommentForm))
24
25    def testGetCommentFormFromLiteral(self):
26        self.testGetCommentForm("{% get_comment_form for comment_tests.article 1 as form %}")
27
28    def testGetCommentFormFromObject(self):
29        self.testGetCommentForm("{% get_comment_form for a as form %}")
30
31    def testRenderCommentForm(self, tag=None):
32        t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
33        ctx, out = self.render(t, a=Article.objects.get(pk=1))
34        self.assertTrue(out.strip().startswith("<form action="))
35        self.assertTrue(out.strip().endswith("</form>"))
36
37    def testRenderCommentFormFromLiteral(self):
38        self.testRenderCommentForm("{% render_comment_form for comment_tests.article 1 %}")
39
40    def testRenderCommentFormFromObject(self):
41        self.testRenderCommentForm("{% render_comment_form for a %}")
42
43    def testGetCommentCount(self, tag=None):
44        self.createSomeComments()
45        t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
46        ctx, out = self.render(t, a=Article.objects.get(pk=1))
47        self.assertEqual(out, "2")
48
49    def testGetCommentCountFromLiteral(self):
50        self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
51
52    def testGetCommentCountFromObject(self):
53        self.testGetCommentCount("{% get_comment_count for a as cc %}")
54
55    def testGetCommentList(self, tag=None):
56        c1, c2, c3, c4 = self.createSomeComments()
57        t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
58        ctx, out = self.render(t, a=Author.objects.get(pk=1))
59        self.assertEqual(out, "")
60        self.assertEqual(list(ctx["cl"]), [c2])
61
62    def testGetCommentListFromLiteral(self):
63        self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
64
65    def testGetCommentListFromObject(self):
66        self.testGetCommentList("{% get_comment_list for a as cl %}")
67
68    def testGetCommentPermalink(self):
69        c1, c2, c3, c4 = self.createSomeComments()
70        t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
71        t += "{% get_comment_permalink cl.0 %}"
72        ct = ContentType.objects.get_for_model(Author)
73        author = Author.objects.get(pk=1)
74        ctx, out = self.render(t, author=author)
75        self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
76
77    def testGetCommentPermalinkFormatted(self):
78        c1, c2, c3, c4 = self.createSomeComments()
79        t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
80        t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}"
81        ct = ContentType.objects.get_for_model(Author)
82        author = Author.objects.get(pk=1)
83        ctx, out = self.render(t, author=author)
84        self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id))
85
86    def testRenderCommentList(self, tag=None):
87        t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
88        ctx, out = self.render(t, a=Article.objects.get(pk=1))
89        self.assertTrue(out.strip().startswith("<dl id=\"comments\">"))
90        self.assertTrue(out.strip().endswith("</dl>"))
91
92    def testRenderCommentListFromLiteral(self):
93        self.testRenderCommentList("{% render_comment_list for comment_tests.article 1 %}")
94
95    def testRenderCommentListFromObject(self):
96        self.testRenderCommentList("{% render_comment_list for a %}")
97