Merge branch 'master' of git+ssh://github.com/fnp/wolnelektury
[wolnelektury.git] / apps / catalogue / tests.py
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
7
8 from nose.tools import raises
9 from StringIO import StringIO
10
11 class BasicSearchLogicTests(TestCase):
12
13     def setUp(self):
14         self.author_tag = models.Tag.objects.create(
15                                 name=u'Adam Mickiewicz [SubWord]',
16                                 category=u'author', slug="one")
17
18         self.unicode_tag = models.Tag.objects.create(
19                                 name=u'Tadeusz Żeleński (Boy)',
20                                 category=u'author', slug="two")
21
22         self.polish_tag = models.Tag.objects.create(
23                                 name=u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń',
24                                 category=u'author', slug="three")
25
26     @raises(ValueError)
27     def test_empty_query(self):
28         """ Check that empty queries raise an error. """
29         views.find_best_matches(u'')
30
31     @raises(ValueError)
32     def test_one_letter_query(self):
33         """ Check that one letter queries aren't permitted. """
34         views.find_best_matches(u't')
35
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,))
41
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,))
45
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,))
49
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,))
53
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'), ())
57
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())
61
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,))
67
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'), ())
71
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,))
75
76
77 class PersonStub(object):
78
79     def __init__(self, first_names, last_name):
80         self.first_names = first_names
81         self.last_name = last_name
82
83 from slughifi import slughifi
84
85 class BookInfoStub(object):
86
87     def __init__(self, **kwargs):            
88         self.__dict = kwargs
89
90     def __setattr__(self, key, value):
91         if not key.startswith('_'):
92             self.__dict[key] = value
93         return object.__setattr__(self, key, value)
94
95     def __getattr__(self, key):
96         return self.__dict[key]
97
98     def to_dict(self):
99         return dict((key, unicode(value)) for key, value in self.__dict.items())
100
101 def info_args(title):
102     """ generate some keywords for comfortable BookInfoCreation  """
103     slug = unicode(slughifi(title))
104     return {'title': unicode(title),
105             'slug': slug,
106             'url': u"http://wolnelektury.pl/example/%s" % slug,
107             'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
108             }
109
110 class BookImportLogicTests(TestCase):
111
112     def setUp(self):
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"),
118             kind="X-Kind",
119             genre="X-Genre",
120             epoch="X-Epoch",
121         )
122
123         self.expected_tags = [
124            ('author', 'jim-lazy'),
125            ('genre', 'x-genre'),
126            ('epoch', 'x-epoch'),
127            ('kind', 'x-kind'),
128         ]
129         self.expected_tags.sort()
130
131     def tearDown(self):
132         models.Book.objects.all().delete()
133
134     def test_empty_book(self):
135         BOOK_TEXT = "<utwor />"
136         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
137
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())
142
143         # no fragments generated
144         self.assertEqual(book.fragments.count(), 0)
145
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, '')
151
152         tags = [ (tag.category, tag.slug) for tag in book.tags ]
153         tags.sort()
154
155         self.assertEqual(tags, self.expected_tags)
156     
157     def test_not_quite_empty_book(self):
158         """ Not empty, but without any real text.
159         
160         Should work like any other non-empty book.
161         """
162         
163         BOOK_TEXT = """<utwor>
164         <liryka_l>
165             <nazwa_utworu>Nic</nazwa_utworu>
166         </liryka_l></utwor>
167         """
168         
169         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
170         self.assertTrue(book.has_html_file())
171
172     def test_book_with_fragment(self):
173         BOOK_TEXT = """<utwor>
174         <opowiadanie>
175             <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
176         </opowiadanie></utwor>
177         """
178
179         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
180         self.assertTrue(book.has_html_file())
181
182         self.assertEqual(book.fragments.count(), 1)
183         self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
184
185         self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
186
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)
191
192         tags = [ (tag.category, tag.slug) for tag in book.tags ]
193         tags.sort()
194
195         self.assertEqual(tags, self.expected_tags)
196
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)
201
202         tags = [ (tag.category, tag.slug) for tag in book.tags ]
203         tags.sort()
204
205         self.expected_tags.remove(('author', 'jim-lazy'))
206         self.expected_tags.append(('author', 'hans-christian-andersen'))
207         self.expected_tags.sort()
208
209         self.assertEqual(tags, self.expected_tags)
210
211         # the old tag should disappear 
212         self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
213                     slug="jim-lazy", category="author")
214
215
216     
217 class BooksByTagTests(TestCase):
218     """ tests the /katalog/tag page for found books """
219     
220     def setUp(self):
221         author = PersonStub(("Common",), "Man")
222         tags = dict(genre='G', epoch='E', author=author, kind="K")
223
224         # grandchild
225         kwargs = info_args(u"GChild")
226         kwargs.update(tags)
227         gchild_info = BookInfoStub(**kwargs)
228         # child
229         kwargs = info_args(u"Child")
230         kwargs.update(tags)
231         child_info = BookInfoStub(parts=[gchild_info.url], **kwargs)
232         # other grandchild
233         kwargs = info_args(u"Different GChild")
234         kwargs.update(tags)
235         diffgchild_info = BookInfoStub(**kwargs)
236         # other child
237         kwargs = info_args(u"Different Child")
238         kwargs.update(tags)
239         kwargs['kind'] = 'K2'
240         diffchild_info = BookInfoStub(parts=[diffgchild_info.url], **kwargs)
241         # parent
242         kwargs = info_args(u"Parent")
243         kwargs.update(tags)
244         parent_info = BookInfoStub(parts=[child_info.url, diffchild_info.url], **kwargs)
245
246         # create the books
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)
250
251         # useful tags
252         self.author = models.Tag.objects.get(name='Common Man', category='author')
253         tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
254         tag_empty.save()
255         
256         self.client = Client()
257     
258     
259     def tearDown(self):
260         models.Book.objects.all().delete()
261
262     
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)
266         
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)
270     
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']))
275     
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']],
280                          ['Parent'])
281
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']],
286                          ['Different Child'])
287
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']],
292                          ['Parent'])
293         
294
295 class TagRelatedTagsTests(TestCase):
296     """ tests the /katalog/tag/ page for related tags """
297     
298     def setUp(self):
299         author = PersonStub(("Common",), "Man")
300
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"))
311         
312         for info in gchild_info, child1_info, child2_info, parent_info:
313             book_text = """<utwor><opowiadanie><akap>
314                 <begin id="m01" />
315                     <motyw id="m01">Theme, %sTheme</motyw>
316                     Ala ma kota
317                 <end id="m01" />
318                 </akap></opowiadanie></utwor>
319                 """ % info.title.encode('utf-8')
320             book = models.Book.from_text_and_meta(ContentFile(book_text), info)
321             book.save()
322         
323         tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
324         tag_empty.save()
325
326         self.client = Client()
327     
328     
329     def tearDown(self):
330         models.Book.objects.all().delete()
331     
332     
333     def test_empty(self):
334         """ empty tag should have no related tags """
335         
336         cats = self.client.get('/katalog/empty/').context['categories']
337         self.assertEqual(cats, {}, 'tags related to empty tag')
338     
339     
340     def test_has_related(self):
341         """ related own and descendants' tags should be generated """
342         
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")
362     
363     
364     def test_related_differ(self):
365         """ related tags shouldn't include filtering tags """
366         
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')
373     
374     
375     def test_parent_tag_once(self):
376         """ if parent and descendants have a common tag, count it only once """
377
378         cats = self.client.get('/katalog/kind/').context['categories']
379         self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
380                          [('Epoch', 1)],
381                          'wrong related tag epoch tag on tag page')
382     
383     
384     def test_siblings_tags_add(self):
385         """ if children have tags and parent hasn't, count the children """
386         
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')
390     
391     def test_themes_add(self):
392         """ all occurencies of theme should be counted """
393
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')
397     
398
399
400 class CleanTagRelationTests(TestCase):
401     """ tests for tag relations cleaning after deleting things """
402     
403     def setUp(self):
404         author = PersonStub(("Common",), "Man")
405
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
410             <end id="m01" />
411             </akap></opowiadanie></utwor>
412             """
413         book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
414         book.save()
415         
416         self.client = Client()
417     
418     
419     def tearDown(self):
420         models.Book.objects.all().delete()
421     
422     
423     def test_delete_objects(self):
424         """ there should be no related tags left after deleting some objects """
425         
426         models.Book.objects.all().delete()
427         cats = self.client.get('/katalog/k/').context['categories']
428         self.assertEqual({}, cats)
429
430
431     def test_deleted_tag(self):
432         """ there should be no tag relations left after deleting tags """
433         
434         models.Tag.objects.all().delete()
435         cats = self.client.get('/katalog/lektura/book/').context['categories']
436         self.assertEqual(cats, {})
437
438
439 class TestIdenticalTag(TestCase):
440     
441     def setUp(self):
442         author = PersonStub(("A",), "B")
443
444         book_info = BookInfoStub(author=author, genre="A B", epoch='A B', kind="A B", 
445                                    **info_args(u"A B"))
446         book_text = """<utwor><opowiadanie><akap>
447             <begin id="m01" /><motyw id="m01">A B</motyw>Ala ma kota
448             <end id="m01" />
449             </akap></opowiadanie></utwor>
450             """
451         book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
452         book.save()
453         
454         self.client = Client()
455     
456     
457     def tearDown(self):
458         models.Book.objects.all().delete()
459     
460     
461     def test_book_tags(self):
462         """ there should be all related tags in relevant categories """
463         
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)
468
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'])
476