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 for book in models.Book.objects.all():
134 book.xml_file.delete()
136 book.html_file.delete()
138 def test_empty_book(self):
139 BOOK_TEXT = "<utwor />"
140 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
142 self.assertEqual(book.title, "Default Book")
143 self.assertEqual(book.slug, "default_book")
144 self.assert_(book.parent is None)
145 self.assertFalse(book.has_html_file())
147 # no fragments generated
148 self.assertEqual(book.fragments.count(), 0)
150 # TODO: this should be filled out probably...
151 self.assertEqual(book.wiki_link, '')
152 self.assertEqual(book.gazeta_link, '')
153 self.assertEqual(book._short_html, '')
154 self.assertEqual(book.description, '')
156 tags = [ (tag.category, tag.slug) for tag in book.tags ]
159 self.assertEqual(tags, self.expected_tags)
161 def test_not_quite_empty_book(self):
162 """ Not empty, but without any real text.
164 Should work like any other non-empty book.
167 BOOK_TEXT = """<utwor>
169 <nazwa_utworu>Nic</nazwa_utworu>
173 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
174 self.assertTrue(book.has_html_file())
176 def test_book_with_fragment(self):
177 BOOK_TEXT = """<utwor>
179 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
180 </opowiadanie></utwor>
183 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
184 self.assertTrue(book.has_html_file())
186 self.assertEqual(book.fragments.count(), 1)
187 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
189 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
191 def test_book_replace_title(self):
192 BOOK_TEXT = """<utwor />"""
193 self.book_info.title = u"Extraordinary"
194 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
196 tags = [ (tag.category, tag.slug) for tag in book.tags ]
199 self.assertEqual(tags, self.expected_tags)
201 def test_book_replace_author(self):
202 BOOK_TEXT = """<utwor />"""
203 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
204 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
206 tags = [ (tag.category, tag.slug) for tag in book.tags ]
209 self.expected_tags.remove(('author', 'jim-lazy'))
210 self.expected_tags.append(('author', 'hans-christian-andersen'))
211 self.expected_tags.sort()
213 self.assertEqual(tags, self.expected_tags)
215 # the old tag should disappear
216 self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
217 slug="jim-lazy", category="author")
221 class BooksByTagTests(TestCase):
222 """ tests the /katalog/tag page for found books """
225 author = PersonStub(("Common",), "Man")
226 tags = dict(genre='G', epoch='E', author=author, kind="K")
229 kwargs = info_args(u"GChild")
231 gchild_info = BookInfoStub(**kwargs)
233 kwargs = info_args(u"Child")
235 child_info = BookInfoStub(parts=[gchild_info.url], **kwargs)
237 kwargs = info_args(u"Different GChild")
239 diffgchild_info = BookInfoStub(**kwargs)
241 kwargs = info_args(u"Different Child")
243 kwargs['kind'] = 'K2'
244 diffchild_info = BookInfoStub(parts=[diffgchild_info.url], **kwargs)
246 kwargs = info_args(u"Parent")
248 parent_info = BookInfoStub(parts=[child_info.url, diffchild_info.url], **kwargs)
251 book_file = ContentFile('<utwor />')
252 for info in gchild_info, child_info, diffgchild_info, diffchild_info, parent_info:
253 book = models.Book.from_text_and_meta(book_file, info)
256 self.author = models.Tag.objects.get(name='Common Man', category='author')
257 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
260 self.client = Client()
264 for book in models.Book.objects.all():
266 book.xml_file.delete()
268 book.html_file.delete()
271 def test_nonexistent_tag(self):
272 """ Looking for a non-existent tag should yield 404 """
273 self.assertEqual(404, self.client.get('/katalog/czeslaw_milosz/').status_code)
275 def test_book_tag(self):
276 """ Looking for a book tag isn't permitted """
277 self.assertEqual(404, self.client.get('/katalog/parent/').status_code)
279 def test_tag_empty(self):
280 """ Tag with no books should return no books """
281 context = self.client.get('/katalog/empty/').context
282 self.assertEqual(0, len(context['object_list']))
284 def test_tag_common(self):
285 """ Filtering by tag should only yield top-level books. """
286 context = self.client.get('/katalog/%s/' % self.author.slug).context
287 self.assertEqual([book.title for book in context['object_list']],
290 def test_tag_child(self):
291 """ Filtering by child's tag should yield the child """
292 context = self.client.get('/katalog/k2/').context
293 self.assertEqual([book.title for book in context['object_list']],
296 def test_tag_child_jump(self):
297 """ Of parent and grandchild, only parent should be returned. """
298 context = self.client.get('/katalog/k/').context
299 self.assertEqual([book.title for book in context['object_list']],
303 class TagRelatedTagsTests(TestCase):
304 """ tests the /katalog/tag/ page for related tags """
307 author = PersonStub(("Common",), "Man")
309 gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind",
310 **info_args(u"GChild"))
311 child1_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
312 parts=[gchild_info.url],
313 **info_args(u"Child1"))
314 child2_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
315 **info_args(u"Child2"))
316 parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind",
317 parts=[child1_info.url, child2_info.url],
318 **info_args(u"Parent"))
320 for info in gchild_info, child1_info, child2_info, parent_info:
321 book_text = """<utwor><opowiadanie><akap>
323 <motyw id="m01">Theme, %sTheme</motyw>
326 </akap></opowiadanie></utwor>
327 """ % info.title.encode('utf-8')
328 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
331 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
334 self.client = Client()
338 for book in models.Book.objects.all():
340 book.xml_file.delete()
342 book.html_file.delete()
345 def test_empty(self):
346 """ empty tag should have no related tags """
348 cats = self.client.get('/katalog/empty/').context['categories']
349 self.assertEqual(cats, {}, 'tags related to empty tag')
352 def test_has_related(self):
353 """ related own and descendants' tags should be generated """
355 cats = self.client.get('/katalog/kind/').context['categories']
356 self.assertTrue('Common Man' in [tag.name for tag in cats['author']],
357 'missing `author` related tag')
358 self.assertTrue('Epoch' in [tag.name for tag in cats['epoch']],
359 'missing `epoch` related tag')
360 self.assertTrue("ChildKind" in [tag.name for tag in cats['kind']],
361 "missing `kind` related tag")
362 self.assertTrue("Genre" in [tag.name for tag in cats['genre']],
363 'missing `genre` related tag')
364 self.assertTrue("ChildGenre" in [tag.name for tag in cats['genre']],
365 "missing child's related tag")
366 self.assertTrue("GchildGenre" in [tag.name for tag in cats['genre']],
367 "missing grandchild's related tag")
368 self.assertTrue('Theme' in [tag.name for tag in cats['theme']],
369 "missing related theme")
370 self.assertTrue('Child1Theme' in [tag.name for tag in cats['theme']],
371 "missing child's related theme")
372 self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']],
373 "missing grandchild's related theme")
376 def test_related_differ(self):
377 """ related tags shouldn't include filtering tags """
379 cats = self.client.get('/katalog/kind/').context['categories']
380 self.assertFalse('Kind' in [tag.name for tag in cats['kind']],
381 'filtering tag wrongly included in related')
382 cats = self.client.get('/katalog/theme/').context['categories']
383 self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
384 'filtering theme wrongly included in related')
387 def test_parent_tag_once(self):
388 """ if parent and descendants have a common tag, count it only once """
390 cats = self.client.get('/katalog/kind/').context['categories']
391 self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
393 'wrong related tag epoch tag on tag page')
396 def test_siblings_tags_add(self):
397 """ if children have tags and parent hasn't, count the children """
399 cats = self.client.get('/katalog/epoch/').context['categories']
400 self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
401 'wrong related kind tags on tag page')
403 def test_themes_add(self):
404 """ all occurencies of theme should be counted """
406 cats = self.client.get('/katalog/epoch/').context['categories']
407 self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
408 'wrong related theme count')