Allow books with internationalized metadata.
[wolnelektury.git] / apps / catalogue / tests / search.py
1 # -*- coding: utf-8 -*-
2 from catalogue import models, views
3 from catalogue.test_utils import *
4
5 from nose.tools import raises
6
7 class BasicSearchLogicTests(WLTestCase):
8
9     def setUp(self):
10         WLTestCase.setUp(self)
11         self.author_tag = models.Tag.objects.create(
12                                 name=u'Adam Mickiewicz [SubWord]',
13                                 category=u'author', slug="one")
14
15         self.unicode_tag = models.Tag.objects.create(
16                                 name=u'Tadeusz Żeleński (Boy)',
17                                 category=u'author', slug="two")
18
19         self.polish_tag = models.Tag.objects.create(
20                                 name=u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń',
21                                 category=u'author', slug="three")
22
23     @raises(ValueError)
24     def test_empty_query(self):
25         """ Check that empty queries raise an error. """
26         views.find_best_matches(u'')
27
28     @raises(ValueError)
29     def test_one_letter_query(self):
30         """ Check that one letter queries aren't permitted. """
31         views.find_best_matches(u't')
32
33     def test_match_by_prefix(self):
34         """ Tags should be matched by prefix of words within it's name. """
35         self.assertEqual(views.find_best_matches(u'Ada'), (self.author_tag,))
36         self.assertEqual(views.find_best_matches(u'Mic'), (self.author_tag,))
37         self.assertEqual(views.find_best_matches(u'Mickiewicz'), (self.author_tag,))
38
39     def test_match_case_insensitive(self):
40         """ Tag names should match case insensitive. """
41         self.assertEqual(views.find_best_matches(u'adam mickiewicz'), (self.author_tag,))
42
43     def test_match_case_insensitive_unicode(self):
44         """ Tag names should match case insensitive (unicode). """
45         self.assertEqual(views.find_best_matches(u'tadeusz żeleński (boy)'), (self.unicode_tag,))
46
47     def test_word_boundary(self):
48         self.assertEqual(views.find_best_matches(u'SubWord'), (self.author_tag,))
49         self.assertEqual(views.find_best_matches(u'[SubWord'), (self.author_tag,))
50
51     def test_unrelated_search(self):
52         self.assertEqual(views.find_best_matches(u'alamakota'), tuple())
53         self.assertEqual(views.find_best_matches(u'Adama'), ())
54
55     def test_infix_doesnt_match(self):
56         """ Searching for middle of a word shouldn't match. """
57         self.assertEqual(views.find_best_matches(u'deusz'), tuple())
58
59     def test_diactricts_removal_pl(self):
60         """ Tags should match both with and without national characters. """
61         self.assertEqual(views.find_best_matches(u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń'), (self.polish_tag,))
62         self.assertEqual(views.find_best_matches(u'EOASLZZCNeoaslzzcn'), (self.polish_tag,))
63         self.assertEqual(views.find_best_matches(u'eoaslzzcneoaslzzcn'), (self.polish_tag,))
64
65     def test_diactricts_query_removal_pl(self):
66         """ Tags without national characters shouldn't be matched by queries with them. """
67         self.assertEqual(views.find_best_matches(u'Adąm'), ())
68
69     def test_sloppy(self):
70         self.assertEqual(views.find_best_matches(u'Żelenski'), (self.unicode_tag,))
71         self.assertEqual(views.find_best_matches(u'zelenski'), (self.unicode_tag,))