198aee8a9e52d0f931e300a8a4944778f685fe1f
[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         for book in models.Book.objects.all():
133             if book.xml_file:
134                 book.xml_file.delete()
135             if book.html_file:
136                 book.html_file.delete()
137
138     def test_empty_book(self):
139         BOOK_TEXT = "<utwor />"
140         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
141
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())
146
147         # no fragments generated
148         self.assertEqual(book.fragments.count(), 0)
149
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, '')
155
156         tags = [ (tag.category, tag.slug) for tag in book.tags ]
157         tags.sort()
158
159         self.assertEqual(tags, self.expected_tags)
160     
161     def test_not_quite_empty_book(self):
162         """ Not empty, but without any real text.
163         
164         Should work like any other non-empty book.
165         """
166         
167         BOOK_TEXT = """<utwor>
168         <liryka_l>
169             <nazwa_utworu>Nic</nazwa_utworu>
170         </liryka_l></utwor>
171         """
172         
173         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
174         self.assertTrue(book.has_html_file())
175
176     def test_book_with_fragment(self):
177         BOOK_TEXT = """<utwor>
178         <opowiadanie>
179             <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
180         </opowiadanie></utwor>
181         """
182
183         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
184         self.assertTrue(book.has_html_file())
185
186         self.assertEqual(book.fragments.count(), 1)
187         self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
188
189         self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
190
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)
195
196         tags = [ (tag.category, tag.slug) for tag in book.tags ]
197         tags.sort()
198
199         self.assertEqual(tags, self.expected_tags)
200
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)
205
206         tags = [ (tag.category, tag.slug) for tag in book.tags ]
207         tags.sort()
208
209         self.expected_tags.remove(('author', 'jim-lazy'))
210         self.expected_tags.append(('author', 'hans-christian-andersen'))
211         self.expected_tags.sort()
212
213         self.assertEqual(tags, self.expected_tags)
214
215         # the old tag should disappear 
216         self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
217                     slug="jim-lazy", category="author")
218
219
220     
221 class BooksByTagTests(TestCase):
222     """ tests the /katalog/tag page for found books """
223     
224     def setUp(self):
225         author = PersonStub(("Common",), "Man")
226         tags = dict(genre='G', epoch='E', author=author, kind="K")
227
228         # grandchild
229         kwargs = info_args(u"GChild")
230         kwargs.update(tags)
231         gchild_info = BookInfoStub(**kwargs)
232         # child
233         kwargs = info_args(u"Child")
234         kwargs.update(tags)
235         child_info = BookInfoStub(parts=[gchild_info.url], **kwargs)
236         # other grandchild
237         kwargs = info_args(u"Different GChild")
238         kwargs.update(tags)
239         diffgchild_info = BookInfoStub(**kwargs)
240         # other child
241         kwargs = info_args(u"Different Child")
242         kwargs.update(tags)
243         kwargs['kind'] = 'K2'
244         diffchild_info = BookInfoStub(parts=[diffgchild_info.url], **kwargs)
245         # parent
246         kwargs = info_args(u"Parent")
247         kwargs.update(tags)
248         parent_info = BookInfoStub(parts=[child_info.url, diffchild_info.url], **kwargs)
249
250         # create the books
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)
254
255         # useful tags
256         self.author = models.Tag.objects.get(name='Common Man', category='author')
257         tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
258         tag_empty.save()
259         
260         self.client = Client()
261     
262     
263     def tearDown(self):
264         for book in models.Book.objects.all():
265             if book.xml_file:
266                 book.xml_file.delete()
267             if book.html_file:
268                 book.html_file.delete()
269
270     
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)
274         
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)
278     
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']))
283     
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']],
288                          ['Parent'])
289
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']],
294                          ['Different Child'])
295
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']],
300                          ['Parent'])
301         
302
303 class TagRelatedTagsTests(TestCase):
304     """ tests the /katalog/tag/ page for related tags """
305     
306     def setUp(self):
307         author = PersonStub(("Common",), "Man")
308
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"))
319         
320         for info in gchild_info, child1_info, child2_info, parent_info:
321             book_text = """<utwor><opowiadanie><akap>
322                 <begin id="m01" />
323                     <motyw id="m01">Theme, %sTheme</motyw>
324                     Ala ma kąta
325                 <end id="m01" />
326                 </akap></opowiadanie></utwor>
327                 """ % info.title.encode('utf-8')
328             book = models.Book.from_text_and_meta(ContentFile(book_text), info)
329             book.save()
330         
331         tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
332         tag_empty.save()
333
334         self.client = Client()
335     
336     
337     def tearDown(self):
338         for book in models.Book.objects.all():
339             if book.xml_file:
340                 book.xml_file.delete()
341             if book.html_file:
342                 book.html_file.delete()
343     
344     
345     def test_empty(self):
346         """ empty tag should have no related tags """
347         
348         cats = self.client.get('/katalog/empty/').context['categories']
349         self.assertEqual(cats, {}, 'tags related to empty tag')
350     
351     
352     def test_has_related(self):
353         """ related own and descendants' tags should be generated """
354         
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")
374     
375     
376     def test_related_differ(self):
377         """ related tags shouldn't include filtering tags """
378         
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')
385     
386     
387     def test_parent_tag_once(self):
388         """ if parent and descendants have a common tag, count it only once """
389
390         cats = self.client.get('/katalog/kind/').context['categories']
391         self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
392                          [('Epoch', 1)],
393                          'wrong related tag epoch tag on tag page')
394     
395     
396     def test_siblings_tags_add(self):
397         """ if children have tags and parent hasn't, count the children """
398         
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')
402     
403     def test_themes_add(self):
404         """ all occurencies of theme should be counted """
405
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')
409         
410         
411     
412