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
7 from nose.tools import raises
8 from StringIO import StringIO
10 class BasicSearchLogicTests(TestCase):
13 self.author_tag = models.Tag.objects.create(
14 name=u'Adam Mickiewicz [SubWord]',
15 category=u'author', slug="one")
17 self.unicode_tag = models.Tag.objects.create(
18 name=u'Tadeusz Żeleński (Boy)',
19 category=u'author', slug="two")
21 self.polish_tag = models.Tag.objects.create(
22 name=u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń',
23 category=u'author', slug="three")
26 def test_empty_query(self):
27 """ Check that empty queries raise an error. """
28 views.find_best_matches(u'')
31 def test_one_letter_query(self):
32 """ Check that one letter queries aren't permitted. """
33 views.find_best_matches(u't')
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,))
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,))
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,))
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,))
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'), ())
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())
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,))
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'), ())
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,))
76 class PersonStub(object):
78 def __init__(self, first_names, last_name):
79 self.first_names = first_names
80 self.last_name = last_name
82 class BookInfoStub(object):
84 def __init__(self, **kwargs):
87 def __setattr__(self, key, value):
88 if not key.startswith('_'):
89 self.__dict[key] = value
90 return object.__setattr__(self, key, value)
92 def __getattr__(self, key):
93 return self.__dict[key]
96 return dict((key, unicode(value)) for key, value in self.__dict.items())
98 class BookImportLogicTests(TestCase):
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"),
111 self.expected_tags = [
112 ('author', 'jim-lazy'),
113 ('book', 'l-default_book'),
114 ('genre', 'x-genre'),
115 ('epoch', 'x-epoch'),
118 self.expected_tags.sort()
120 def test_empty_book(self):
121 BOOK_TEXT = "<utwor />"
122 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
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())
129 # no fragments generated
130 self.assertEqual(book.fragments.count(), 0)
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, '')
138 tags = [ (tag.category, tag.slug) for tag in book.tags ]
141 self.assertEqual(tags, self.expected_tags)
143 def test_book_with_fragment(self):
144 BOOK_TEXT = """<utwor>
146 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
147 </opowiadanie></utwor>
150 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
152 self.assertEqual(book.fragments.count(), 1)
153 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
155 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ])
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)
162 tags = [ (tag.category, tag.slug) for tag in book.tags ]
165 self.assertEqual(tags, self.expected_tags)
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)
172 tags = [ (tag.category, tag.slug) for tag in book.tags ]
175 self.expected_tags.remove(('author', 'jim-lazy'))
176 self.expected_tags.append(('author', 'hans-christian-andersen'))
177 self.expected_tags.sort()
179 self.assertEqual(tags, self.expected_tags)
181 # the old tag should disappear
182 self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
183 slug="jim-lazy", category="author")