Add Book.ancestor m2m.
[wolnelektury.git] / apps / catalogue / tests / search.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from catalogue import models, views
6 from catalogue.test_utils import *
7
8 from nose.tools import raises
9
10 class BasicSearchLogicTests(WLTestCase):
11
12     def setUp(self):
13         WLTestCase.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,))