1
from django.contrib.comments.forms import CommentForm
2
from django.contrib.comments.models import Comment
3
from django.template import Template, Context
4
from regressiontests.comment_tests.models import Article, Author
5
from regressiontests.comment_tests.tests import CommentTestCase
7
class CommentTemplateTagTests(CommentTestCase):
9
def render(self, t, **c):
11
out = Template(t).render(ctx)
14
def testCommentFormTarget(self):
15
ctx, out = self.render("{% load comments %}{% comment_form_target %}")
16
self.assertEqual(out, "/post/")
18
def testGetCommentForm(self, tag=None):
19
t = "{% load comments %}" + (tag or "{% get_comment_form for comment_tests.article a.id as form %}")
20
ctx, out = self.render(t, a=Article.objects.get(pk=1))
21
self.assertEqual(out, "")
22
self.assert_(isinstance(ctx["form"], CommentForm))
24
def testGetCommentFormFromLiteral(self):
25
self.testGetCommentForm("{% get_comment_form for comment_tests.article 1 as form %}")
27
def testGetCommentFormFromObject(self):
28
self.testGetCommentForm("{% get_comment_form for a as form %}")
30
def testRenderCommentForm(self, tag=None):
31
t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
32
ctx, out = self.render(t, a=Article.objects.get(pk=1))
33
self.assert_(out.strip().startswith("<form action="))
34
self.assert_(out.strip().endswith("</form>"))
36
def testRenderCommentFormFromLiteral(self):
37
self.testRenderCommentForm("{% render_comment_form for comment_tests.article 1 %}")
39
def testRenderCommentFormFromObject(self):
40
self.testRenderCommentForm("{% render_comment_form for a %}")
42
def testGetCommentCount(self, tag=None):
43
self.createSomeComments()
44
t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
45
ctx, out = self.render(t, a=Article.objects.get(pk=1))
46
self.assertEqual(out, "2")
48
def testGetCommentCountFromLiteral(self):
49
self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
51
def testGetCommentCountFromObject(self):
52
self.testGetCommentCount("{% get_comment_count for a as cc %}")
54
def testGetCommentList(self, tag=None):
55
c1, c2, c3, c4 = self.createSomeComments()
56
t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
57
ctx, out = self.render(t, a=Author.objects.get(pk=1))
58
self.assertEqual(out, "")
59
self.assertEqual(list(ctx["cl"]), [c2])
61
def testGetCommentListFromLiteral(self):
62
self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
64
def testGetCommentListFromObject(self):
65
self.testGetCommentList("{% get_comment_list for a as cl %}")