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