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