1 # -*- coding: utf-8 -*-
2 from django.test import TestCase
3 from catalogue import models, views
4 from django.core.files.base import ContentFile
5 from django.contrib.auth.models import User, AnonymousUser
6 from django.test.client import Client
8 from nose.tools import raises
9 from StringIO import StringIO
11 class BasicSearchLogicTests(TestCase):
14 self.author_tag = models.Tag.objects.create(
15 name=u'Adam Mickiewicz [SubWord]',
16 category=u'author', slug="one")
18 self.unicode_tag = models.Tag.objects.create(
19 name=u'Tadeusz Żeleński (Boy)',
20 category=u'author', slug="two")
22 self.polish_tag = models.Tag.objects.create(
23 name=u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń',
24 category=u'author', slug="three")
27 def test_empty_query(self):
28 """ Check that empty queries raise an error. """
29 views.find_best_matches(u'')
32 def test_one_letter_query(self):
33 """ Check that one letter queries aren't permitted. """
34 views.find_best_matches(u't')
36 def test_match_by_prefix(self):
37 """ Tags should be matched by prefix of words within it's name. """
38 self.assertEqual(views.find_best_matches(u'Ada'), (self.author_tag,))
39 self.assertEqual(views.find_best_matches(u'Mic'), (self.author_tag,))
40 self.assertEqual(views.find_best_matches(u'Mickiewicz'), (self.author_tag,))
42 def test_match_case_insensitive(self):
43 """ Tag names should match case insensitive. """
44 self.assertEqual(views.find_best_matches(u'adam mickiewicz'), (self.author_tag,))
46 def test_match_case_insensitive_unicode(self):
47 """ Tag names should match case insensitive (unicode). """
48 self.assertEqual(views.find_best_matches(u'tadeusz żeleński (boy)'), (self.unicode_tag,))
50 def test_word_boundary(self):
51 self.assertEqual(views.find_best_matches(u'SubWord'), (self.author_tag,))
52 self.assertEqual(views.find_best_matches(u'[SubWord'), (self.author_tag,))
54 def test_unrelated_search(self):
55 self.assertEqual(views.find_best_matches(u'alamakota'), tuple())
56 self.assertEqual(views.find_best_matches(u'Adama'), ())
58 def test_infix_doesnt_match(self):
59 """ Searching for middle of a word shouldn't match. """
60 self.assertEqual(views.find_best_matches(u'deusz'), tuple())
62 def test_diactricts_removal_pl(self):
63 """ Tags should match both with and without national characters. """
64 self.assertEqual(views.find_best_matches(u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń'), (self.polish_tag,))
65 self.assertEqual(views.find_best_matches(u'EOASLZZCNeoaslzzcn'), (self.polish_tag,))
66 self.assertEqual(views.find_best_matches(u'eoaslzzcneoaslzzcn'), (self.polish_tag,))
68 def test_diactricts_query_removal_pl(self):
69 """ Tags without national characters shouldn't be matched by queries with them. """
70 self.assertEqual(views.find_best_matches(u'Adąm'), ())
72 def test_sloppy(self):
73 self.assertEqual(views.find_best_matches(u'Żelenski'), (self.unicode_tag,))
74 self.assertEqual(views.find_best_matches(u'zelenski'), (self.unicode_tag,))
77 class PersonStub(object):
79 def __init__(self, first_names, last_name):
80 self.first_names = first_names
81 self.last_name = last_name
83 from slughifi import slughifi
85 class BookInfoStub(object):
87 def __init__(self, **kwargs):
90 def __setattr__(self, key, value):
91 if not key.startswith('_'):
92 self.__dict[key] = value
93 return object.__setattr__(self, key, value)
95 def __getattr__(self, key):
96 return self.__dict[key]
99 return dict((key, unicode(value)) for key, value in self.__dict.items())
101 def info_args(title):
102 """ generate some keywords for comfortable BookInfoCreation """
103 slug = unicode(slughifi(title))
104 return {'title': unicode(title),
106 'url': u"http://wolnelektury.pl/example/%s" % slug,
107 'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
110 class BookImportLogicTests(TestCase):
113 self.book_info = BookInfoStub(
114 url=u"http://wolnelektury.pl/example/default_book",
115 about=u"http://wolnelektury.pl/example/URI/default_book",
116 title=u"Default Book",
117 author=PersonStub(("Jim",), "Lazy"),
123 self.expected_tags = [
124 ('author', 'jim-lazy'),
125 ('genre', 'x-genre'),
126 ('epoch', 'x-epoch'),
129 self.expected_tags.sort()
132 models.Book.objects.all().delete()
134 def test_empty_book(self):
135 BOOK_TEXT = "<utwor />"
136 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
138 self.assertEqual(book.title, "Default Book")
139 self.assertEqual(book.slug, "default_book")
140 self.assert_(book.parent is None)
141 self.assertFalse(book.has_html_file())
143 # no fragments generated
144 self.assertEqual(book.fragments.count(), 0)
146 # TODO: this should be filled out probably...
147 self.assertEqual(book.wiki_link, '')
148 self.assertEqual(book.gazeta_link, '')
149 self.assertEqual(book._short_html, '')
150 self.assertEqual(book.description, '')
152 tags = [ (tag.category, tag.slug) for tag in book.tags ]
155 self.assertEqual(tags, self.expected_tags)
157 def test_not_quite_empty_book(self):
158 """ Not empty, but without any real text.
160 Should work like any other non-empty book.
163 BOOK_TEXT = """<utwor>
165 <nazwa_utworu>Nic</nazwa_utworu>
169 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
170 self.assertTrue(book.has_html_file())
172 def test_book_with_fragment(self):
173 BOOK_TEXT = """<utwor>
175 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
176 </opowiadanie></utwor>
179 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
180 self.assertTrue(book.has_html_file())
182 self.assertEqual(book.fragments.count(), 1)
183 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
185 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
187 def test_book_replace_title(self):
188 BOOK_TEXT = """<utwor />"""
189 self.book_info.title = u"Extraordinary"
190 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
192 tags = [ (tag.category, tag.slug) for tag in book.tags ]
195 self.assertEqual(tags, self.expected_tags)
197 def test_book_replace_author(self):
198 BOOK_TEXT = """<utwor />"""
199 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
200 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
202 tags = [ (tag.category, tag.slug) for tag in book.tags ]
205 self.expected_tags.remove(('author', 'jim-lazy'))
206 self.expected_tags.append(('author', 'hans-christian-andersen'))
207 self.expected_tags.sort()
209 self.assertEqual(tags, self.expected_tags)
211 # the old tag should disappear
212 self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
213 slug="jim-lazy", category="author")
217 class BooksByTagTests(TestCase):
218 """ tests the /katalog/tag page for found books """
221 author = PersonStub(("Common",), "Man")
222 tags = dict(genre='G', epoch='E', author=author, kind="K")
225 kwargs = info_args(u"GChild")
227 gchild_info = BookInfoStub(**kwargs)
229 kwargs = info_args(u"Child")
231 child_info = BookInfoStub(parts=[gchild_info.url], **kwargs)
233 kwargs = info_args(u"Different GChild")
235 diffgchild_info = BookInfoStub(**kwargs)
237 kwargs = info_args(u"Different Child")
239 kwargs['kind'] = 'K2'
240 diffchild_info = BookInfoStub(parts=[diffgchild_info.url], **kwargs)
242 kwargs = info_args(u"Parent")
244 parent_info = BookInfoStub(parts=[child_info.url, diffchild_info.url], **kwargs)
247 book_file = ContentFile('<utwor />')
248 for info in gchild_info, child_info, diffgchild_info, diffchild_info, parent_info:
249 book = models.Book.from_text_and_meta(book_file, info)
252 self.author = models.Tag.objects.get(name='Common Man', category='author')
253 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
256 self.client = Client()
260 models.Book.objects.all().delete()
263 def test_nonexistent_tag(self):
264 """ Looking for a non-existent tag should yield 404 """
265 self.assertEqual(404, self.client.get('/katalog/czeslaw_milosz/').status_code)
267 def test_book_tag(self):
268 """ Looking for a book tag isn't permitted """
269 self.assertEqual(404, self.client.get('/katalog/parent/').status_code)
271 def test_tag_empty(self):
272 """ Tag with no books should return no books """
273 context = self.client.get('/katalog/empty/').context
274 self.assertEqual(0, len(context['object_list']))
276 def test_tag_common(self):
277 """ Filtering by tag should only yield top-level books. """
278 context = self.client.get('/katalog/%s/' % self.author.slug).context
279 self.assertEqual([book.title for book in context['object_list']],
282 def test_tag_child(self):
283 """ Filtering by child's tag should yield the child """
284 context = self.client.get('/katalog/k2/').context
285 self.assertEqual([book.title for book in context['object_list']],
288 def test_tag_child_jump(self):
289 """ Of parent and grandchild, only parent should be returned. """
290 context = self.client.get('/katalog/k/').context
291 self.assertEqual([book.title for book in context['object_list']],
295 class TagRelatedTagsTests(TestCase):
296 """ tests the /katalog/tag/ page for related tags """
299 author = PersonStub(("Common",), "Man")
301 gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind",
302 **info_args(u"GChild"))
303 child1_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
304 parts=[gchild_info.url],
305 **info_args(u"Child1"))
306 child2_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
307 **info_args(u"Child2"))
308 parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind",
309 parts=[child1_info.url, child2_info.url],
310 **info_args(u"Parent"))
312 for info in gchild_info, child1_info, child2_info, parent_info:
313 book_text = """<utwor><opowiadanie><akap>
315 <motyw id="m01">Theme, %sTheme</motyw>
318 </akap></opowiadanie></utwor>
319 """ % info.title.encode('utf-8')
320 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
323 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
326 self.client = Client()
330 models.Book.objects.all().delete()
333 def test_empty(self):
334 """ empty tag should have no related tags """
336 cats = self.client.get('/katalog/empty/').context['categories']
337 self.assertEqual(cats, {}, 'tags related to empty tag')
340 def test_has_related(self):
341 """ related own and descendants' tags should be generated """
343 cats = self.client.get('/katalog/kind/').context['categories']
344 self.assertTrue('Common Man' in [tag.name for tag in cats['author']],
345 'missing `author` related tag')
346 self.assertTrue('Epoch' in [tag.name for tag in cats['epoch']],
347 'missing `epoch` related tag')
348 self.assertTrue("ChildKind" in [tag.name for tag in cats['kind']],
349 "missing `kind` related tag")
350 self.assertTrue("Genre" in [tag.name for tag in cats['genre']],
351 'missing `genre` related tag')
352 self.assertTrue("ChildGenre" in [tag.name for tag in cats['genre']],
353 "missing child's related tag")
354 self.assertTrue("GchildGenre" in [tag.name for tag in cats['genre']],
355 "missing grandchild's related tag")
356 self.assertTrue('Theme' in [tag.name for tag in cats['theme']],
357 "missing related theme")
358 self.assertTrue('Child1Theme' in [tag.name for tag in cats['theme']],
359 "missing child's related theme")
360 self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']],
361 "missing grandchild's related theme")
364 def test_related_differ(self):
365 """ related tags shouldn't include filtering tags """
367 cats = self.client.get('/katalog/kind/').context['categories']
368 self.assertFalse('Kind' in [tag.name for tag in cats['kind']],
369 'filtering tag wrongly included in related')
370 cats = self.client.get('/katalog/theme/').context['categories']
371 self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
372 'filtering theme wrongly included in related')
375 def test_parent_tag_once(self):
376 """ if parent and descendants have a common tag, count it only once """
378 cats = self.client.get('/katalog/kind/').context['categories']
379 self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
381 'wrong related tag epoch tag on tag page')
384 def test_siblings_tags_add(self):
385 """ if children have tags and parent hasn't, count the children """
387 cats = self.client.get('/katalog/epoch/').context['categories']
388 self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
389 'wrong related kind tags on tag page')
391 def test_themes_add(self):
392 """ all occurencies of theme should be counted """
394 cats = self.client.get('/katalog/epoch/').context['categories']
395 self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
396 'wrong related theme count')
400 class CleanTagRelationTests(TestCase):
401 """ tests for tag relations cleaning after deleting things """
404 author = PersonStub(("Common",), "Man")
406 book_info = BookInfoStub(author=author, genre="G", epoch='E', kind="K",
407 **info_args(u"Book"))
408 book_text = """<utwor><opowiadanie><akap>
409 <begin id="m01" /><motyw id="m01">Theme</motyw>Ala ma kota
411 </akap></opowiadanie></utwor>
413 book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
416 self.client = Client()
420 models.Book.objects.all().delete()
423 def test_delete_objects(self):
424 """ there should be no related tags left after deleting some objects """
426 models.Book.objects.all().delete()
427 cats = self.client.get('/katalog/k/').context['categories']
428 self.assertEqual({}, cats)
431 def test_deleted_tag(self):
432 """ there should be no tag relations left after deleting tags """
434 models.Tag.objects.all().delete()
435 cats = self.client.get('/katalog/lektura/book/').context['categories']
436 self.assertEqual(cats, {})
439 class TestIdenticalTag(TestCase):
442 author = PersonStub(("A",), "B")
444 book_info = BookInfoStub(author=author, genre="A B", epoch='A B', kind="A B",
446 book_text = """<utwor><opowiadanie><akap>
447 <begin id="m01" /><motyw id="m01">A B</motyw>Ala ma kota
449 </akap></opowiadanie></utwor>
451 book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
454 self.client = Client()
458 models.Book.objects.all().delete()
461 def test_book_tags(self):
462 """ there should be all related tags in relevant categories """
464 cats = self.client.get('/katalog/lektura/a-b/').context['categories']
465 for category in 'author', 'kind', 'genre', 'epoch', 'theme':
466 self.assertTrue('A B' in [tag.name for tag in cats[category]],
467 'missing related tag for %s' % category)
469 def test_qualified_url(self):
470 categories = {'author': 'autor', 'theme': 'motyw', 'epoch': 'epoka', 'kind':'rodzaj', 'genre':'gatunek'}
471 for cat, localcat in categories.iteritems():
472 context = self.client.get('/katalog/%s/a-b/' % localcat).context
473 self.assertEqual(1, len(context['object_list']))
474 self.assertNotEqual({}, context['categories'])
475 self.assertFalse(cat in context['categories'])