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.
5 from catalogue import models, views
6 from catalogue.test_utils import *
8 from nose.tools import raises
11 class BasicSearchLogicTests(WLTestCase):
14 WLTestCase.setUp(self)
15 self.author_tag = models.Tag.objects.create(
16 name=u'Adam Mickiewicz [SubWord]',
17 category=u'author', slug="one")
19 self.unicode_tag = models.Tag.objects.create(
20 name=u'Tadeusz Żeleński (Boy)',
21 category=u'author', slug="two")
23 self.polish_tag = models.Tag.objects.create(
24 name=u'ĘÓĄŚŁŻŹĆŃęóąśłżźćń',
25 category=u'author', slug="three")
28 def test_empty_query(self):
29 """ Check that empty queries raise an error. """
30 views.find_best_matches(u'')
33 def test_one_letter_query(self):
34 """ Check that one letter queries aren't permitted. """
35 views.find_best_matches(u't')
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,))
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,))
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,))
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,))
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'), ())
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())
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,))
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'), ())
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,))