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 class BookInfoStub(object):
85 def __init__(self, **kwargs):
88 def __setattr__(self, key, value):
89 if not key.startswith('_'):
90 self.__dict[key] = value
91 return object.__setattr__(self, key, value)
93 def __getattr__(self, key):
94 return self.__dict[key]
97 return dict((key, unicode(value)) for key, value in self.__dict.items())
99 class BookImportLogicTests(TestCase):
102 self.book_info = BookInfoStub(
103 url=u"http://wolnelektury.pl/example/default_book",
104 about=u"http://wolnelektury.pl/example/URI/default_book",
105 title=u"Default Book",
106 author=PersonStub(("Jim",), "Lazy"),
112 self.expected_tags = [
113 ('author', 'jim-lazy'),
114 ('book', 'l-default_book'),
115 ('genre', 'x-genre'),
116 ('epoch', 'x-epoch'),
119 self.expected_tags.sort()
121 def test_empty_book(self):
122 BOOK_TEXT = "<utwor />"
123 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
125 self.assertEqual(book.title, "Default Book")
126 self.assertEqual(book.slug, "default_book")
127 self.assert_(book.parent is None)
128 self.assertFalse(book.has_html_file())
130 # no fragments generated
131 self.assertEqual(book.fragments.count(), 0)
133 # TODO: this should be filled out probably...
134 self.assertEqual(book.wiki_link, '')
135 self.assertEqual(book.gazeta_link, '')
136 self.assertEqual(book._short_html, '')
137 self.assertEqual(book.description, '')
139 tags = [ (tag.category, tag.slug) for tag in book.tags ]
142 self.assertEqual(tags, self.expected_tags)
144 def test_book_with_fragment(self):
145 BOOK_TEXT = """<utwor>
147 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
148 </opowiadanie></utwor>
151 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
152 self.assertTrue(book.has_html_file())
154 self.assertEqual(book.fragments.count(), 1)
155 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
157 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
159 def test_book_replace_title(self):
160 BOOK_TEXT = """<utwor />"""
161 self.book_info.title = u"Extraordinary"
162 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
164 tags = [ (tag.category, tag.slug) for tag in book.tags ]
167 self.assertEqual(tags, self.expected_tags)
169 def test_book_replace_author(self):
170 BOOK_TEXT = """<utwor />"""
171 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
172 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
174 tags = [ (tag.category, tag.slug) for tag in book.tags ]
177 self.expected_tags.remove(('author', 'jim-lazy'))
178 self.expected_tags.append(('author', 'hans-christian-andersen'))
179 self.expected_tags.sort()
181 self.assertEqual(tags, self.expected_tags)
183 # the old tag should disappear
184 self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
185 slug="jim-lazy", category="author")
189 class BooksByTagFlat(TestCase):
191 self.tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
192 self.tag_common = models.Tag(name='Common author', slug='common', category='author')
194 self.tag_kind1 = models.Tag(name='Type 1', slug='type1', category='kind')
195 self.tag_kind2 = models.Tag(name='Type 2', slug='type2', category='kind')
196 self.tag_kind3 = models.Tag(name='Type 3', slug='type3', category='kind')
197 for tag in self.tag_empty, self.tag_common, self.tag_kind1, self.tag_kind2, self.tag_kind3:
201 self.parent = models.Book(title='Parent', slug='parent')
204 self.similar_child = models.Book(title='Similar child',
205 slug='similar_child',
207 self.similar_child.save()
208 self.similar_grandchild = models.Book(title='Similar grandchild',
209 slug='similar_grandchild',
210 parent=self.similar_child)
211 self.similar_grandchild.save()
212 for book in self.parent, self.similar_child, self.similar_grandchild:
213 book.tags = [self.tag_common, self.tag_kind1]
216 self.different_child = models.Book(title='Different child',
217 slug='different_child',
219 self.different_child.save()
220 self.different_child.tags = [self.tag_common, self.tag_kind2]
221 self.different_child.save()
222 self.different_grandchild = models.Book(title='Different grandchild',
223 slug='different_grandchild',
224 parent=self.different_child)
225 self.different_grandchild.save()
226 self.different_grandchild.tags = [self.tag_common, self.tag_kind3]
227 self.different_grandchild.save()
229 for book in models.Book.objects.all():
230 l_tag = models.Tag(name=book.title, slug='l-'+book.slug, category='book')
232 book.tags = list(book.tags) + [l_tag]
235 self.client = Client()
237 def test_nonexistent_tag(self):
238 """ Looking for a non-existent tag should yield 404 """
239 self.assertEqual(404, self.client.get('/katalog/czeslaw_milosz/').status_code)
241 def test_book_tag(self):
242 """ Looking for a book tag isn't permitted """
243 self.assertEqual(404, self.client.get('/katalog/parent/').status_code)
245 def test_tag_empty(self):
246 """ Tag with no books should return no books and no related tags """
247 context = self.client.get('/katalog/empty/').context
248 self.assertEqual(0, len(context['object_list']))
249 self.assertEqual(0, len(context['categories']))
251 def test_tag_common(self):
252 """ Filtering by tag should only yield top-level books """
253 context = self.client.get('/katalog/%s/' % self.tag_common.slug).context
254 self.assertEqual(list(context['object_list']),