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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
# Copyright (c) 2011, 2012 Free Software Foundation
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This project incorporates work covered by the following copyright and permission notice:
# Copyright (c) 2009, Julien Fache
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of the author nor the names of other
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# Copyright (c) 2011, 2012 Free Software Foundation
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test cases for Objectapp's templatetags"""
from datetime import datetime
from django.test import TestCase
from django.template import Context
from django.template import Template
from django.template import TemplateSyntaxError
from django.contrib import comments
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.contrib.comments.models import CommentFlag
from tagging.models import Tag
from objectapp.models import Gbobject
from objectapp.models import Author
from objectapp.models import Objecttype
from objectapp.managers import DRAFT
from objectapp.managers import PUBLISHED
from objectapp.templatetags.objectapp_tags import get_authors
from objectapp.templatetags.objectapp_tags import get_gravatar
from objectapp.templatetags.objectapp_tags import get_tag_cloud
from objectapp.templatetags.objectapp_tags import get_objecttypes
from objectapp.templatetags.objectapp_tags import objectapp_pagination
from objectapp.templatetags.objectapp_tags import get_recent_gbobjects
from objectapp.templatetags.objectapp_tags import get_random_gbobjects
from objectapp.templatetags.objectapp_tags import objectapp_breadcrumbs
from objectapp.templatetags.objectapp_tags import get_popular_gbobjects
from objectapp.templatetags.objectapp_tags import get_similar_gbobjects
from objectapp.templatetags.objectapp_tags import get_recent_comments
from objectapp.templatetags.objectapp_tags import get_recent_linkbacks
from objectapp.templatetags.objectapp_tags import get_calendar_gbobjects
from objectapp.templatetags.objectapp_tags import get_archives_gbobjects
from objectapp.templatetags.objectapp_tags import get_featured_gbobjects
from objectapp.templatetags.objectapp_tags import get_archives_gbobjects_tree
class TemplateTagsTestCase(TestCase):
"""Test cases for Template tags"""
def setUp(self):
params = {'title': 'My gbobject',
'content': 'My content',
'tags': 'objectapp, test',
'creation_date': datetime(2010, 1, 1),
'slug': 'my-gbobject'}
self.gbobject = Gbobject.objects.create(**params)
def publish_gbobject(self):
self.gbobject.status = PUBLISHED
self.gbobject.featured = True
self.gbobject.sites.add(Site.objects.get_current())
self.gbobject.save()
def test_get_objecttypes(self):
context = get_objecttypes()
self.assertEquals(len(context['objecttypes']), 0)
self.assertEquals(context['template'], 'objectapp/tags/objecttypes.html')
Objecttype.objects.create(title='Objecttype 1', slug='Objecttype-1')
context = get_objecttypes('custom_template.html')
self.assertEquals(len(context['objecttypes']), 1)
self.assertEquals(context['template'], 'custom_template.html')
def test_get_authors(self):
context = get_authors()
self.assertEquals(len(context['authors']), 0)
self.assertEquals(context['template'], 'objectapp/tags/authors.html')
user = User.objects.create_user(username='webmaster',
email='webmaster@example.com')
self.gbobject.authors.add(user)
self.publish_gbobject()
context = get_authors('custom_template.html')
self.assertEquals(len(context['authors']), 1)
self.assertEquals(context['template'], 'custom_template.html')
def test_get_recent_gbobjects(self):
context = get_recent_gbobjects()
self.assertEquals(len(context['gbobjects']), 0)
self.assertEquals(context['template'],
'objectapp/tags/recent_gbobjects.html')
self.publish_gbobject()
context = get_recent_gbobjects(3, 'custom_template.html')
self.assertEquals(len(context['gbobjects']), 1)
self.assertEquals(context['template'], 'custom_template.html')
context = get_recent_gbobjects(0)
self.assertEquals(len(context['gbobjects']), 0)
def test_get_featured_gbobjects(self):
context = get_featured_gbobjects()
self.assertEquals(len(context['gbobjects']), 0)
self.assertEquals(context['template'],
'objectapp/tags/featured_gbobjects.html')
self.publish_gbobject()
context = get_featured_gbobjects(3, 'custom_template.html')
self.assertEquals(len(context['gbobjects']), 1)
self.assertEquals(context['template'], 'custom_template.html')
context = get_featured_gbobjects(0)
self.assertEquals(len(context['gbobjects']), 0)
def test_get_random_gbobjects(self):
context = get_random_gbobjects()
self.assertEquals(len(context['gbobjects']), 0)
self.assertEquals(context['template'],
'objectapp/tags/random_gbobjects.html')
self.publish_gbobject()
context = get_random_gbobjects(3, 'custom_template.html')
self.assertEquals(len(context['gbobjects']), 1)
self.assertEquals(context['template'], 'custom_template.html')
context = get_random_gbobjects(0)
self.assertEquals(len(context['gbobjects']), 0)
def test_get_popular_gbobjects(self):
context = get_popular_gbobjects()
self.assertEquals(len(context['gbobjects']), 0)
self.assertEquals(context['template'],
'objectapp/tags/popular_gbobjects.html')
self.publish_gbobject()
context = get_popular_gbobjects(3, 'custom_template.html')
self.assertEquals(len(context['gbobjects']), 0)
self.assertEquals(context['template'], 'custom_template.html')
params = {'title': 'My second gbobject',
'content': 'My second content',
'tags': 'objectapp, test',
'status': PUBLISHED,
'slug': 'my-second-gbobject'}
site = Site.objects.get_current()
second_gbobject = Gbobject.objects.create(**params)
second_gbobject.sites.add(site)
comments.get_model().objects.create(comment='My Comment 1', site=site,
content_object=self.gbobject)
comments.get_model().objects.create(comment='My Comment 2', site=site,
content_object=self.gbobject)
comments.get_model().objects.create(comment='My Comment 3', site=site,
content_object=second_gbobject)
context = get_popular_gbobjects(3)
self.assertEquals(context['gbobjects'], [self.gbobject, second_gbobject])
self.gbobject.status = DRAFT
self.gbobject.save()
context = get_popular_gbobjects(3)
self.assertEquals(context['gbobjects'], [second_gbobject])
def test_get_similar_gbobjects(self):
self.publish_gbobject()
source_context = Context({'object': self.gbobject})
context = get_similar_gbobjects(source_context)
self.assertEquals(len(context['gbobjects']), 0)
self.assertEquals(context['template'],
'objectapp/tags/similar_gbobjects.html')
params = {'title': 'My second gbobject',
'content': 'This is the second gbobject of my tests.',
'tags': 'objectapp, test',
'status': PUBLISHED,
'slug': 'my-second-gbobject'}
site = Site.objects.get_current()
second_gbobject = Gbobject.objects.create(**params)
second_gbobject.sites.add(site)
source_context = Context({'object': second_gbobject})
context = get_similar_gbobjects(source_context, 3,
'custom_template.html',
flush=True)
self.assertEquals(len(context['gbobjects']), 1)
self.assertEquals(context['template'], 'custom_template.html')
def test_get_archives_gbobjects(self):
context = get_archives_gbobjects()
self.assertEquals(len(context['archives']), 0)
self.assertEquals(context['template'],
'objectapp/tags/archives_gbobjects.html')
self.publish_gbobject()
params = {'title': 'My second gbobject',
'content': 'My second content',
'tags': 'objectapp, test',
'status': PUBLISHED,
'creation_date': datetime(2009, 1, 1),
'slug': 'my-second-gbobject'}
site = Site.objects.get_current()
second_gbobject = Gbobject.objects.create(**params)
second_gbobject.sites.add(site)
context = get_archives_gbobjects('custom_template.html')
self.assertEquals(len(context['archives']), 2)
self.assertEquals(context['archives'][0], datetime(2010, 1, 1))
self.assertEquals(context['archives'][1], datetime(2009, 1, 1))
self.assertEquals(context['template'], 'custom_template.html')
def test_get_archives_tree(self):
context = get_archives_gbobjects_tree()
self.assertEquals(len(context['archives']), 0)
self.assertEquals(context['template'],
'objectapp/tags/archives_gbobjects_tree.html')
self.publish_gbobject()
params = {'title': 'My second gbobject',
'content': 'My second content',
'tags': 'objectapp, test',
'status': PUBLISHED,
'creation_date': datetime(2009, 1, 10),
'slug': 'my-second-gbobject'}
site = Site.objects.get_current()
second_gbobject = Gbobject.objects.create(**params)
second_gbobject.sites.add(site)
context = get_archives_gbobjects_tree('custom_template.html')
self.assertEquals(len(context['archives']), 2)
self.assertEquals(context['archives'][0], datetime(2009, 1, 10))
self.assertEquals(context['archives'][1], datetime(2010, 1, 1))
self.assertEquals(context['template'], 'custom_template.html')
def test_get_calendar_gbobjects(self):
source_context = Context()
context = get_calendar_gbobjects(source_context)
self.assertEquals(context['previous_month'], None)
self.assertEquals(context['next_month'], None)
self.assertEquals(context['template'], 'objectapp/tags/calendar.html')
self.publish_gbobject()
context = get_calendar_gbobjects(source_context,
template='custom_template.html')
self.assertEquals(context['previous_month'], datetime(2010, 1, 1))
self.assertEquals(context['next_month'], None)
self.assertEquals(context['template'], 'custom_template.html')
context = get_calendar_gbobjects(source_context, 2009, 1)
self.assertEquals(context['previous_month'], None)
self.assertEquals(context['next_month'], datetime(2010, 1, 1))
source_context = Context({'month': datetime(2009, 1, 1)})
context = get_calendar_gbobjects(source_context)
self.assertEquals(context['previous_month'], None)
self.assertEquals(context['next_month'], datetime(2010, 1, 1))
source_context = Context({'month': datetime(2010, 1, 1)})
context = get_calendar_gbobjects(source_context)
self.assertEquals(context['previous_month'], None)
self.assertEquals(context['next_month'], None)
params = {'title': 'My second gbobject',
'content': 'My second content',
'tags': 'objectapp, test',
'status': PUBLISHED,
'creation_date': datetime(2008, 1, 1),
'slug': 'my-second-gbobject'}
site = Site.objects.get_current()
second_gbobject = Gbobject.objects.create(**params)
second_gbobject.sites.add(site)
source_context = Context()
context = get_calendar_gbobjects(source_context, 2009, 1)
self.assertEquals(context['previous_month'], datetime(2008, 1, 1))
self.assertEquals(context['next_month'], datetime(2010, 1, 1))
context = get_calendar_gbobjects(source_context)
self.assertEquals(context['previous_month'], datetime(2010, 1, 1))
self.assertEquals(context['next_month'], None)
def test_get_recent_comments(self):
site = Site.objects.get_current()
context = get_recent_comments()
self.assertEquals(len(context['comments']), 0)
self.assertEquals(context['template'],
'objectapp/tags/recent_comments.html')
comment_1 = comments.get_model().objects.create(
comment='My Comment 1', site=site,
content_object=self.gbobject)
context = get_recent_comments(3, 'custom_template.html')
self.assertEquals(len(context['comments']), 0)
self.assertEquals(context['template'], 'custom_template.html')
self.publish_gbobject()
context = get_recent_comments()
self.assertEquals(len(context['comments']), 1)
author = User.objects.create_user(username='webmaster',
email='webmaster@example.com')
comment_2 = comments.get_model().objects.create(
comment='My Comment 2', site=site,
content_object=self.gbobject)
comment_2.flags.create(user=author,
flag=CommentFlag.MODERATOR_APPROVAL)
context = get_recent_comments()
self.assertEquals(list(context['comments']), [comment_2, comment_1])
def test_get_recent_linkbacks(self):
user = User.objects.create_user(username='webmaster',
email='webmaster@example.com')
site = Site.objects.get_current()
context = get_recent_linkbacks()
self.assertEquals(len(context['linkbacks']), 0)
self.assertEquals(context['template'],
'objectapp/tags/recent_linkbacks.html')
linkback_1 = comments.get_model().objects.create(
comment='My Linkback 1', site=site,
content_object=self.gbobject)
linkback_1.flags.create(user=user, flag='pingback')
context = get_recent_linkbacks(3, 'custom_template.html')
self.assertEquals(len(context['linkbacks']), 0)
self.assertEquals(context['template'], 'custom_template.html')
self.publish_gbobject()
context = get_recent_linkbacks()
self.assertEquals(len(context['linkbacks']), 1)
linkback_2 = comments.get_model().objects.create(
comment='My Linkback 2', site=site,
content_object=self.gbobject)
linkback_2.flags.create(user=user, flag='trackback')
context = get_recent_linkbacks()
self.assertEquals(list(context['linkbacks']), [linkback_2, linkback_1])
def test_objectapp_pagination(self):
class FakeRequest(object):
def __init__(self, get_dict):
self.GET = get_dict
source_context = Context({'request': FakeRequest(
{'page': '1', 'key': 'val'})})
paginator = Paginator(range(200), 10)
context = objectapp_pagination(source_context, paginator.page(1))
self.assertEquals(context['page'].number, 1)
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [18, 19, 20])
self.assertEquals(context['GET_string'], '&key=val')
self.assertEquals(context['template'], 'objectapp/tags/pagination.html')
source_context = Context({'request': FakeRequest({})})
context = objectapp_pagination(source_context, paginator.page(2))
self.assertEquals(context['page'].number, 2)
self.assertEquals(context['begin'], [1, 2, 3, 4])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [18, 19, 20])
self.assertEquals(context['GET_string'], '')
context = objectapp_pagination(source_context, paginator.page(3))
self.assertEquals(context['begin'], [1, 2, 3, 4, 5])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(6))
self.assertEquals(context['begin'], [1, 2, 3, 4, 5, 6, 7, 8])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(11))
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [9, 10, 11, 12, 13])
self.assertEquals(context['end'], [18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(15))
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [13, 14, 15, 16, 17, 18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(18))
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [16, 17, 18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(19))
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [17, 18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(20))
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [18, 19, 20])
context = objectapp_pagination(source_context, paginator.page(10),
begin_pages=1, end_pages=3,
before_pages=4, after_pages=3,
template='custom_template.html')
self.assertEquals(context['begin'], [1])
self.assertEquals(context['middle'], [6, 7, 8, 9, 10, 11, 12, 13])
self.assertEquals(context['end'], [18, 19, 20])
self.assertEquals(context['template'], 'custom_template.html')
paginator = Paginator(range(50), 10)
context = objectapp_pagination(source_context, paginator.page(1))
self.assertEquals(context['begin'], [1, 2, 3, 4, 5])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [])
paginator = Paginator(range(60), 10)
context = objectapp_pagination(source_context, paginator.page(1))
self.assertEquals(context['begin'], [1, 2, 3, 4, 5, 6])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [])
paginator = Paginator(range(70), 10)
context = objectapp_pagination(source_context, paginator.page(1))
self.assertEquals(context['begin'], [1, 2, 3])
self.assertEquals(context['middle'], [])
self.assertEquals(context['end'], [5, 6, 7])
def test_objectapp_breadcrumbs(self):
class FakeRequest(object):
def __init__(self, path):
self.path = path
source_context = Context({'request': FakeRequest('/')})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 1)
self.assertEquals(context['breadcrumbs'][0].name, 'Blog')
self.assertEquals(context['breadcrumbs'][0].url,
reverse('objectapp_gbobject_archive_index'))
self.assertEquals(context['separator'], '/')
self.assertEquals(context['template'], 'objectapp/tags/breadcrumbs.html')
context = objectapp_breadcrumbs(source_context,
'>', 'Weblog', 'custom_template.html')
self.assertEquals(len(context['breadcrumbs']), 1)
self.assertEquals(context['breadcrumbs'][0].name, 'Weblog')
self.assertEquals(context['separator'], '>')
self.assertEquals(context['template'], 'custom_template.html')
source_context = Context(
{'request': FakeRequest(self.gbobject.get_absolute_url()),
'object': self.gbobject})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 5)
cat_1 = Objecttype.objects.create(title='Objecttype 1', slug='Objecttype-1')
source_context = Context(
{'request': FakeRequest(cat_1.get_absolute_url()),
'object': cat_1})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 3)
cat_2 = Objecttype.objects.create(title='Objecttype 2', slug='Objecttype-2',
parent=cat_1)
source_context = Context(
{'request': FakeRequest(cat_2.get_absolute_url()),
'object': cat_2})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 4)
tag = Tag.objects.get(name='test')
source_context = Context(
{'request': FakeRequest(reverse('objectapp_tag_detail',
args=['test'])),
'object': tag})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 3)
User.objects.create_user(username='webmaster',
email='webmaster@example.com')
author = Author.objects.get(username='webmaster')
source_context = Context(
{'request': FakeRequest(author.get_absolute_url()),
'object': author})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 3)
source_context = Context(
{'request': FakeRequest(reverse(
'objectapp_gbobject_archive_year', args=[2011]))})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 2)
source_context = Context({'request': FakeRequest(reverse(
'objectapp_gbobject_archive_month', args=[2011, '03']))})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 3)
source_context = Context({'request': FakeRequest(reverse(
'objectapp_gbobject_archive_day', args=[2011, '03', 15]))})
context = objectapp_breadcrumbs(source_context)
self.assertEquals(len(context['breadcrumbs']), 4)
# More tests can be done here, for testing path and objects in context
def test_get_gravatar(self):
self.assertEquals(
get_gravatar('webmaster@example.com'),
'http://www.gravatar.com/avatar/86d4fd4a22de452'
'a9228298731a0b592.jpg?s=80&r=g')
self.assertEquals(
get_gravatar(' WEBMASTER@example.com ', 15, 'x', '404'),
'http://www.gravatar.com/avatar/86d4fd4a22de452'
'a9228298731a0b592.jpg?s=15&r=x&d=404')
def test_get_tags(self):
Tag.objects.create(name='tag')
t = Template("""
{% load objectapp_tags %}
{% get_tags as gbobject_tags %}
{{ gbobject_tags|join:", " }}
""")
html = t.render(Context())
self.assertEquals(html.strip(), '')
self.publish_gbobject()
html = t.render(Context())
self.assertEquals(html.strip(), 'test, objectapp')
template_error_as = """
{% load objectapp_tags %}
{% get_tags a_s gbobject_tags %}"""
self.assertRaises(TemplateSyntaxError, Template, template_error_as)
template_error_args = """
{% load objectapp_tags %}
{% get_tags as gbobject tags %}"""
self.assertRaises(TemplateSyntaxError, Template, template_error_args)
def test_get_tag_cloud(self):
context = get_tag_cloud()
self.assertEquals(len(context['tags']), 0)
self.assertEquals(context['template'], 'objectapp/tags/tag_cloud.html')
self.publish_gbobject()
context = get_tag_cloud(6, 'custom_template.html')
self.assertEquals(len(context['tags']), 2)
self.assertEquals(context['template'], 'custom_template.html')
|