/
test_templatetags.py
205 lines (153 loc) · 8.05 KB
/
test_templatetags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from __future__ import absolute_import
from django.contrib.comments.forms import CommentForm
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType
from django.template import Template, Context, Library, libraries
from ..models import Article, Author
from . import CommentTestCase
register = Library()
@register.filter
def noop(variable, param=None):
return variable
libraries['comment_testtags'] = register
class CommentTemplateTagTests(CommentTestCase):
def render(self, t, **c):
ctx = Context(c)
out = Template(t).render(ctx)
return ctx, out
def testCommentFormTarget(self):
ctx, out = self.render("{% load comments %}{% comment_form_target %}")
self.assertEqual(out, "/post/")
def testGetCommentForm(self, tag=None):
t = "{% load comments %}" + (tag or "{% get_comment_form for comment_tests.article a.id as form %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
self.assertEqual(out, "")
self.assertIsInstance(ctx["form"], CommentForm)
def testGetCommentFormFromLiteral(self):
self.testGetCommentForm("{% get_comment_form for comment_tests.article 1 as form %}")
def testGetCommentFormFromObject(self):
self.testGetCommentForm("{% get_comment_form for a as form %}")
def testWhitespaceInGetCommentFormTag(self):
self.testGetCommentForm("{% load comment_testtags %}{% get_comment_form for a|noop:'x y' as form %}")
def testRenderCommentForm(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
self.assertTrue(out.strip().startswith("<form action="))
self.assertTrue(out.strip().endswith("</form>"))
def testRenderCommentFormFromLiteral(self):
self.testRenderCommentForm("{% render_comment_form for comment_tests.article 1 %}")
def testRenderCommentFormFromObject(self):
self.testRenderCommentForm("{% render_comment_form for a %}")
def testWhitespaceInRenderCommentFormTag(self):
self.testRenderCommentForm("{% load comment_testtags %}{% render_comment_form for a|noop:'x y' %}")
def testRenderCommentFormFromObjectWithQueryCount(self):
with self.assertNumQueries(1):
self.testRenderCommentFormFromObject()
def verifyGetCommentCount(self, tag=None):
t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
ctx, out = self.render(t, a=Article.objects.get(pk=1))
self.assertEqual(out, "2")
def testGetCommentCount(self):
self.createSomeComments()
self.verifyGetCommentCount("{% get_comment_count for comment_tests.article a.id as cc %}")
def testGetCommentCountFromLiteral(self):
self.createSomeComments()
self.verifyGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
def testGetCommentCountFromObject(self):
self.createSomeComments()
self.verifyGetCommentCount("{% get_comment_count for a as cc %}")
def testWhitespaceInGetCommentCountTag(self):
self.createSomeComments()
self.verifyGetCommentCount("{% load comment_testtags %}{% get_comment_count for a|noop:'x y' as cc %}")
def verifyGetCommentList(self, tag=None):
c1, c2, c3, c4 = Comment.objects.all()[:4]
t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
ctx, out = self.render(t, a=Author.objects.get(pk=1))
self.assertEqual(out, "")
self.assertEqual(list(ctx["cl"]), [c2])
def testGetCommentList(self):
self.createSomeComments()
self.verifyGetCommentList("{% get_comment_list for comment_tests.author a.id as cl %}")
def testGetCommentListFromLiteral(self):
self.createSomeComments()
self.verifyGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
def testGetCommentListFromObject(self):
self.createSomeComments()
self.verifyGetCommentList("{% get_comment_list for a as cl %}")
def testWhitespaceInGetCommentListTag(self):
self.createSomeComments()
self.verifyGetCommentList("{% load comment_testtags %}{% get_comment_list for a|noop:'x y' as cl %}")
def testGetCommentPermalink(self):
c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
t += "{% get_comment_permalink cl.0 %}"
ct = ContentType.objects.get_for_model(Author)
author = Author.objects.get(pk=1)
ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
def testGetCommentPermalinkFormatted(self):
c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}"
ct = ContentType.objects.get_for_model(Author)
author = Author.objects.get(pk=1)
ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id))
def testWhitespaceInGetCommentPermalinkTag(self):
c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments comment_testtags %}{% get_comment_list for comment_tests.author author.id as cl %}"
t += "{% get_comment_permalink cl.0|noop:'x y' %}"
ct = ContentType.objects.get_for_model(Author)
author = Author.objects.get(pk=1)
ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
def testRenderCommentList(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
self.assertTrue(out.strip().startswith("<dl id=\"comments\">"))
self.assertTrue(out.strip().endswith("</dl>"))
def testRenderCommentListFromLiteral(self):
self.testRenderCommentList("{% render_comment_list for comment_tests.article 1 %}")
def testRenderCommentListFromObject(self):
self.testRenderCommentList("{% render_comment_list for a %}")
def testWhitespaceInRenderCommentListTag(self):
self.testRenderCommentList("{% load comment_testtags %}{% render_comment_list for a|noop:'x y' %}")
def testNumberQueries(self):
"""
Ensure that the template tags use cached content types to reduce the
number of DB queries.
Refs #16042.
"""
self.createSomeComments()
# {% render_comment_list %} -----------------
# Clear CT cache
ContentType.objects.clear_cache()
with self.assertNumQueries(4):
self.testRenderCommentListFromObject()
# CT's should be cached
with self.assertNumQueries(3):
self.testRenderCommentListFromObject()
# {% get_comment_list %} --------------------
ContentType.objects.clear_cache()
with self.assertNumQueries(4):
self.verifyGetCommentList()
with self.assertNumQueries(3):
self.verifyGetCommentList()
# {% render_comment_form %} -----------------
ContentType.objects.clear_cache()
with self.assertNumQueries(3):
self.testRenderCommentForm()
with self.assertNumQueries(2):
self.testRenderCommentForm()
# {% get_comment_form %} --------------------
ContentType.objects.clear_cache()
with self.assertNumQueries(3):
self.testGetCommentForm()
with self.assertNumQueries(2):
self.testGetCommentForm()
# {% get_comment_count %} -------------------
ContentType.objects.clear_cache()
with self.assertNumQueries(3):
self.verifyGetCommentCount()
with self.assertNumQueries(2):
self.verifyGetCommentCount()