/
test_template_tags.py
361 lines (244 loc) · 10.2 KB
/
test_template_tags.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""
Almost all test cases covers both tag calling and template using.
"""
from __future__ import absolute_import, print_function, unicode_literals
from django.conf import settings as django_settings
from django.contrib.contenttypes.models import ContentType
from django.http import HttpRequest
from django.utils.six import assertCountEqual
from wiki.conf import settings
from wiki.forms import CreateRootForm
from wiki.models import Article, ArticleForObject, ArticleRevision
from wiki.templatetags.wiki_tags import (article_for_object, login_url,
wiki_form, wiki_render)
from ..base import TemplateTestCase
if not django_settings.configured:
django_settings.configure()
# XXX article_for_object accepts context, but not using it
class ArticleForObjectTemplatetagTest(TemplateTestCase):
template = """
{% load wiki_tags %}
{% article_for_object obj as anything %}
{{ anything }}
"""
def setUp(self):
super(ArticleForObjectTemplatetagTest, self).setUp()
from wiki.templatetags import wiki_tags
wiki_tags._cache = {}
def test_obj_arg_is_not_a_django_model(self):
from wiki.templatetags import wiki_tags
with self.assertRaises(TypeError):
article_for_object({}, '')
with self.assertRaises(TypeError):
article_for_object({'request': 100500}, {})
with self.assertRaises(TypeError):
self.render({'obj': 'tiger!'})
self.assertEqual(len(wiki_tags._cache), 0)
def test_obj_is_not_in__cache_and_articleforobject_is_not_exist(self):
from wiki.templatetags.wiki_tags import _cache as cache
obj = Article.objects.create()
article_for_object({}, obj)
self.assertIn(obj, cache)
self.assertEqual(cache[obj], None)
self.assertEqual(len(cache), 1)
self.render({'obj': obj})
self.assertIn(obj, cache)
self.assertEqual(cache[obj], None)
self.assertEqual(len(cache), 1)
def test_obj_is_not_in__cache_and_articleforobjec_is_exist(self):
from wiki.templatetags.wiki_tags import _cache as cache
a = Article.objects.create()
content_type = ContentType.objects.get_for_model(a)
ArticleForObject.objects.create(
article=a,
content_type=content_type,
object_id=1
)
output = article_for_object({}, a)
self.assertEqual(output, a)
self.assertIn(a, cache)
self.assertEqual(cache[a], a)
self.assertEqual(len(cache), 1)
self.render({'obj': a})
self.assertIn(a, cache)
self.assertEqual(cache[a], a)
self.assertEqual(len(cache), 1)
def test_obj_in__cache_and_articleforobject_is_not_exist(self):
model = Article.objects.create()
from wiki.templatetags import wiki_tags
wiki_tags._cache = {model: 'spam'}
article_for_object({}, model)
self.assertIn(model, wiki_tags._cache)
self.assertEqual(wiki_tags._cache[model], None)
self.assertEqual(len(wiki_tags._cache), 1)
self.render({'obj': model})
self.assertIn(model, wiki_tags._cache)
self.assertEqual(wiki_tags._cache[model], None)
self.assertEqual(len(wiki_tags._cache), 1)
self.assertNotIn('spam', wiki_tags._cache.values())
def test_obj_in__cache_and_articleforobjec_is_exist(self):
article = Article.objects.create()
content_type = ContentType.objects.get_for_model(article)
ArticleForObject.objects.create(
article=article,
content_type=content_type,
object_id=1
)
from wiki.templatetags import wiki_tags
wiki_tags._cache = {article: 'spam'}
output = article_for_object({}, article)
self.assertEqual(output, article)
self.assertIn(article, wiki_tags._cache)
self.assertEqual(wiki_tags._cache[article], article)
output = self.render({'obj': article})
self.assertIn(article, wiki_tags._cache)
self.assertEqual(wiki_tags._cache[article], article)
expected = 'Article without content (1)'
self.assertIn(expected, output)
# TODO manage plugins in template
class WikiRenderTest(TemplateTestCase):
template = """
{% load wiki_tags %}
{% wiki_render article pc %}
"""
def tearDown(self):
from wiki.core.plugins import registry
registry._cache = {}
super(WikiRenderTest, self).tearDown()
keys = ['article',
'content',
'preview',
'plugins',
'STATIC_URL',
'CACHE_TIMEOUT'
]
def test_if_preview_content_is_none(self):
# monkey patch
from wiki.core.plugins import registry
registry._cache = {'ham': 'spam'}
article = Article.objects.create()
output = wiki_render({}, article)
assertCountEqual(self, self.keys, output)
self.assertEqual(output['article'], article)
self.assertEqual(output['content'], None)
self.assertEqual(output['preview'], False)
self.assertEqual(output['plugins'], {'ham': 'spam'})
self.assertEqual(output['STATIC_URL'], django_settings.STATIC_URL)
self.assertEqual(output['CACHE_TIMEOUT'], settings.CACHE_TIMEOUT)
# Additional check
self.render({'article': article, 'pc': None})
def test_called_with_preview_content_and_article_have_current_revision(self):
article = Article.objects.create()
ArticleRevision.objects.create(
article=article,
title="Test title",
content="Some beauty test text"
)
content = (
"""This is a normal paragraph\n"""
"""\n"""
"""Headline\n"""
"""========\n"""
)
expected_markdown = (
"""<p>This is a normal paragraph</p>\n"""
"""<h1 id="wiki-toc-headline">Headline</h1>"""
)
# monkey patch
from wiki.core.plugins import registry
registry._cache = {'spam': 'eggs'}
output = wiki_render({}, article, preview_content=content)
assertCountEqual(self, self.keys, output)
self.assertEqual(output['article'], article)
self.assertMultiLineEqual(output['content'], expected_markdown)
self.assertEqual(output['preview'], True)
self.assertEqual(output['plugins'], {'spam': 'eggs'})
self.assertEqual(output['STATIC_URL'], django_settings.STATIC_URL)
self.assertEqual(output['CACHE_TIMEOUT'], settings.CACHE_TIMEOUT)
output = self.render({'article': article, 'pc': content})
self.assertIn(expected_markdown, output)
def test_called_with_preview_content_and_article_dont_have_current_revision(
self):
article = Article.objects.create()
content = (
"""This is a normal paragraph\n"""
"""\n"""
"""Headline\n"""
"""========\n"""
)
# monkey patch
from wiki.core.plugins import registry
registry._cache = {'spam': 'eggs'}
output = wiki_render({}, article, preview_content=content)
assertCountEqual(self, self.keys, output)
self.assertEqual(output['article'], article)
self.assertMultiLineEqual(output['content'], '')
self.assertEqual(output['preview'], True)
self.assertEqual(output['plugins'], {'spam': 'eggs'})
self.assertEqual(output['STATIC_URL'], django_settings.STATIC_URL)
self.assertEqual(output['CACHE_TIMEOUT'], settings.CACHE_TIMEOUT)
self.render({'article': article, 'pc': content})
class WikiFormTest(TemplateTestCase):
template = """
{% load wiki_tags %}
{% wiki_form form_obj %}
"""
def test_form_obj_is_not_baseform_instance(self):
context = {'test_key': 'test_value'}
form_obj = 'ham'
with self.assertRaises(TypeError):
wiki_form(context, form_obj)
self.assertEqual(context, {'test_key': 'test_value'})
with self.assertRaises(TypeError):
self.render({'test_key': 100500})
self.assertEqual(context, {'test_key': 'test_value'})
def test_form_obj_is_baseform_instance(self):
context = {'test_key': 'test_value'}
# not by any special reasons, just a form
form_obj = CreateRootForm()
wiki_form(context, form_obj)
self.assertEqual(context, {'test_key': 'test_value', 'form': form_obj})
self.render({'form_obj': form_obj})
self.assertEqual(context, {'test_key': 'test_value', 'form': form_obj})
class LoginUrlTest(TemplateTestCase):
template = """
{% load wiki_tags %}
{% login_url as some_url %}
{{ some_url }}
"""
def test_no_request_in_context(self):
with self.assertRaises(KeyError):
login_url({})
with self.assertRaises(KeyError):
self.render({})
def test_login_url_if_no_query_string_in_request(self):
r = HttpRequest()
r.META = {}
r.path = 'best/test/page/ever/'
output = login_url({'request': r})
expected = '/_accounts/login/?next=best/test/page/ever/'
self.assertEqual(output, expected)
output = self.render({'request': r})
self.assertIn(expected, output)
def test_login_url_if_query_string_is_empty(self):
r = HttpRequest()
r.META = {'QUERY_STRING': ''}
r.path = 'best/test/page/ever/'
output = login_url({'request': r})
expected = '/_accounts/login/?next=best/test/page/ever/'
self.assertEqual(output, expected)
output = self.render({'request': r})
self.assertIn(expected, output)
def test_login_url_if_query_string_is_not_empty(self):
r = HttpRequest()
r.META = {'QUERY_STRING': 'title=Main_page&action=raw'}
r.path = 'best/test/page/ever/'
context = {'request': r}
output = login_url(context)
expected = (
'/_accounts/login/'
'?next=best/test/page/ever/%3Ftitle%3DMain_page%26action%3Draw'
)
self.assertEqual(output, expected)
output = self.render({'request': r})
self.assertIn(expected, output)