f5a35a41e2c5f4151387328823c308b6057c5e6f
[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
7 from nose.tools import raises
8 from StringIO import StringIO
9
10 class BasicSearchLogicTests(TestCase):
11
12     def setUp(self):
13         self.author_tag = models.Tag.objects.create(
14                                 name=u'Adam Mickiewicz [SubWord]',
15                                 category=u'author', slug="one")
16
17         self.unicode_tag = models.Tag.objects.create(
18                                 name=u'Tadeusz Żeleński (Boy)',
19                                 category=u'author', slug="two")
20
21         self.polish_tag = models.Tag.objects.create(
22                                 name=u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń',
23                                 category=u'author', slug="three")
24
25     @raises(ValueError)
26     def test_empty_query(self):
27         """ Check that empty queries raise an error. """
28         views.find_best_matches(u'')
29
30     @raises(ValueError)
31     def test_one_letter_query(self):
32         """ Check that one letter queries aren't permitted. """
33         views.find_best_matches(u't')
34
35     def test_match_by_prefix(self):
36         """ Tags should be matched by prefix of words within it's name. """
37         self.assertEqual(views.find_best_matches(u'Ada'), (self.author_tag,))
38         self.assertEqual(views.find_best_matches(u'Mic'), (self.author_tag,))
39         self.assertEqual(views.find_best_matches(u'Mickiewicz'), (self.author_tag,))
40
41     def test_match_case_insensitive(self):
42         """ Tag names should match case insensitive. """
43         self.assertEqual(views.find_best_matches(u'adam mickiewicz'), (self.author_tag,))
44
45     def test_match_case_insensitive_unicode(self):
46         """ Tag names should match case insensitive (unicode). """
47         self.assertEqual(views.find_best_matches(u'tadeusz żeleński (boy)'), (self.unicode_tag,))
48
49     def test_word_boundary(self):
50         self.assertEqual(views.find_best_matches(u'SubWord'), (self.author_tag,))
51         self.assertEqual(views.find_best_matches(u'[SubWord'), (self.author_tag,))
52
53     def test_unrelated_search(self):
54         self.assertEqual(views.find_best_matches(u'alamakota'), tuple())
55         self.assertEqual(views.find_best_matches(u'Adama'), ())
56
57     def test_infix_doesnt_match(self):
58         """ Searching for middle of a word shouldn't match. """
59         self.assertEqual(views.find_best_matches(u'deusz'), tuple())
60
61     def test_diactricts_removal_pl(self):
62         """ Tags should match both with and without national characters. """
63         self.assertEqual(views.find_best_matches(u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń'), (self.polish_tag,))
64         self.assertEqual(views.find_best_matches(u'EOASLZZCNeoaslzzcn'), (self.polish_tag,))
65         self.assertEqual(views.find_best_matches(u'eoaslzzcneoaslzzcn'), (self.polish_tag,))
66
67     def test_diactricts_query_removal_pl(self):
68         """ Tags without national characters shouldn't be matched by queries with them. """
69         self.assertEqual(views.find_best_matches(u'Adąm'), ())
70
71     def test_sloppy(self):
72         self.assertEqual(views.find_best_matches(u'Żelenski'), (self.unicode_tag,))
73         self.assertEqual(views.find_best_matches(u'zelenski'), (self.unicode_tag,))
74
75
76 class PersonStub(object):
77
78     def __init__(self, first_names, last_name):
79         self.first_names = first_names
80         self.last_name = last_name
81
82 class BookInfoStub(object):
83
84     def __init__(self, **kwargs):
85         self.__dict = kwargs
86
87     def __setattr__(self, key, value):
88         if not key.startswith('_'):
89             self.__dict[key] = value
90         return object.__setattr__(self, key, value)
91
92     def __getattr__(self, key):
93         return self.__dict[key]
94
95     def to_dict(self):
96         return dict((key, unicode(value)) for key, value in self.__dict.items())
97
98 class BookImportLogicTests(TestCase):
99
100     def setUp(self):
101         self.book_info = BookInfoStub(
102             url=u"http://wolnelektury.pl/example/default_book",
103             about=u"http://wolnelektury.pl/example/URI/default_book",
104             title=u"Default Book",
105             author=PersonStub(("Jim",), "Lazy"),
106             kind="X-Kind",
107             genre="X-Genre",
108             epoch="X-Epoch",
109         )
110
111         self.expected_tags = [
112            ('author', 'jim-lazy'),
113            ('book', 'l-default_book'),
114            ('genre', 'x-genre'),
115            ('epoch', 'x-epoch'),
116            ('kind', 'x-kind'),
117         ]
118         self.expected_tags.sort()
119
120     def test_empty_book(self):
121         BOOK_TEXT = "<utwor />"
122         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
123
124         self.assertEqual(book.title, "Default Book")
125         self.assertEqual(book.slug, "default_book")
126         self.assert_(book.parent is None)
127         self.assertTrue(book.has_html_file())
128
129         # no fragments generated
130         self.assertEqual(book.fragments.count(), 0)
131
132         # TODO: this should be filled out probably...
133         self.assertEqual(book.wiki_link, '')
134         self.assertEqual(book.gazeta_link, '')
135         self.assertEqual(book._short_html, '')
136         self.assertEqual(book.description, '')
137
138         tags = [ (tag.category, tag.slug) for tag in book.tags ]
139         tags.sort()
140
141         self.assertEqual(tags, self.expected_tags)
142
143     def test_book_with_fragment(self):
144         BOOK_TEXT = """<utwor>
145         <opowiadanie>
146             <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
147         </opowiadanie></utwor>
148         """
149
150         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
151
152         self.assertEqual(book.fragments.count(), 1)
153         self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
154
155         self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
156
157     def test_book_replace_title(self):
158         BOOK_TEXT = """<utwor />"""
159         self.book_info.title = u"Extraordinary"
160         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
161
162         tags = [ (tag.category, tag.slug) for tag in book.tags ]
163         tags.sort()
164
165         self.assertEqual(tags, self.expected_tags)
166
167     def test_book_replace_author(self):
168         BOOK_TEXT = """<utwor />"""
169         self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
170         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
171
172         tags = [ (tag.category, tag.slug) for tag in book.tags ]
173         tags.sort()
174
175         self.expected_tags.remove(('author', 'jim-lazy'))
176         self.expected_tags.append(('author', 'hans-christian-andersen'))
177         self.expected_tags.sort()
178
179         self.assertEqual(tags, self.expected_tags)
180
181         # the old tag should disappear 
182         self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
183                     slug="jim-lazy", category="author")
184
185