X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/c9a668f42a4a821a920baf1df421045cc9a6e82d..14bdcf6cb3e84ba5af957779b8d1164a68ee7060:/apps/catalogue/tests.py?ds=sidebyside
diff --git a/apps/catalogue/tests.py b/apps/catalogue/tests.py
index fae4ba2e9..068939806 100644
--- a/apps/catalogue/tests.py
+++ b/apps/catalogue/tests.py
@@ -1,53 +1,257 @@
# -*- coding: utf-8 -*-
from django.test import TestCase
from catalogue import models, views
+from django.core.files.base import ContentFile
from django.contrib.auth.models import User, AnonymousUser
+from django.test.client import Client
+
+from nose.tools import raises
+from StringIO import StringIO
+
+class BasicSearchLogicTests(TestCase):
-class SimpleSearchTest(TestCase):
def setUp(self):
- t = models.Tag(name=u'Tadeusz Å»eleÅski (Boy)', category=u'author', slug=u'a1')
- t.save()
- self.t = t
- self.user = AnonymousUser()
-
- def tearDown(self):
- self.t.delete()
-
- def test_too_short(self):
- self.assertRaises(ValueError, views.find_best_matches, u'', self.user)
- self.assertRaises(ValueError, views.find_best_matches, u't', self.user)
-
- def test_match_beginning(self):
- self.assertEqual(views.find_best_matches(u'Tad', self.user), (self.t,))
-
- def test_match_case(self):
- self.assertEqual(views.find_best_matches(u'TAD', self.user), (self.t,))
+ self.author_tag = models.Tag.objects.create(
+ name=u'Adam Mickiewicz [SubWord]',
+ category=u'author', slug="one")
+
+ self.unicode_tag = models.Tag.objects.create(
+ name=u'Tadeusz Å»eleÅski (Boy)',
+ category=u'author', slug="two")
+
+ self.polish_tag = models.Tag.objects.create(
+ name=u'ÄÃÄÅÅŻŹÄÅÄóÄ
ÅÅżźÄÅ',
+ category=u'author', slug="three")
+
+ @raises(ValueError)
+ def test_empty_query(self):
+ """ Check that empty queries raise an error. """
+ views.find_best_matches(u'')
+
+ @raises(ValueError)
+ def test_one_letter_query(self):
+ """ Check that one letter queries aren't permitted. """
+ views.find_best_matches(u't')
+
+ def test_match_by_prefix(self):
+ """ Tags should be matched by prefix of words within it's name. """
+ self.assertEqual(views.find_best_matches(u'Ada'), (self.author_tag,))
+ self.assertEqual(views.find_best_matches(u'Mic'), (self.author_tag,))
+ self.assertEqual(views.find_best_matches(u'Mickiewicz'), (self.author_tag,))
+
+ def test_match_case_insensitive(self):
+ """ Tag names should match case insensitive. """
+ self.assertEqual(views.find_best_matches(u'adam mickiewicz'), (self.author_tag,))
+
+ def test_match_case_insensitive_unicode(self):
+ """ Tag names should match case insensitive (unicode). """
+ self.assertEqual(views.find_best_matches(u'tadeusz żeleÅski (boy)'), (self.unicode_tag,))
def test_word_boundary(self):
- self.assertEqual(views.find_best_matches(u'Boy', self.user), (self.t,))
- self.assertEqual(views.find_best_matches(u'(Boy', self.user), (self.t,))
+ self.assertEqual(views.find_best_matches(u'SubWord'), (self.author_tag,))
+ self.assertEqual(views.find_best_matches(u'[SubWord'), (self.author_tag,))
+
+ def test_unrelated_search(self):
+ self.assertEqual(views.find_best_matches(u'alamakota'), tuple())
+ self.assertEqual(views.find_best_matches(u'Adama'), ())
+
+ def test_infix_doesnt_match(self):
+ """ Searching for middle of a word shouldn't match. """
+ self.assertEqual(views.find_best_matches(u'deusz'), tuple())
+
+ def test_diactricts_removal_pl(self):
+ """ Tags should match both with and without national characters. """
+ self.assertEqual(views.find_best_matches(u'ÄÃÄÅÅŻŹÄÅÄóÄ
ÅÅżźÄÅ'), (self.polish_tag,))
+ self.assertEqual(views.find_best_matches(u'EOASLZZCNeoaslzzcn'), (self.polish_tag,))
+ self.assertEqual(views.find_best_matches(u'eoaslzzcneoaslzzcn'), (self.polish_tag,))
+
+ def test_diactricts_query_removal_pl(self):
+ """ Tags without national characters shouldn't be matched by queries with them. """
+ self.assertEqual(views.find_best_matches(u'AdÄ
m'), ())
- def test_not_found(self):
- self.assertEqual(views.find_best_matches(u'andrzej', self.user), ())
- self.assertEqual(views.find_best_matches(u'deusz', self.user), ())
-
- def test_locale(self):
- self.assertEqual(views.find_best_matches(u'ele', self.user), ())
- self.assertEqual(views.find_best_matches(u'Żele', self.user), (self.t,))
- self.assertEqual(views.find_best_matches(u'żele', self.user), (self.t,))
-
def test_sloppy(self):
- self.assertEqual(views.find_best_matches(u'Żelenski', self.user), (self.t,))
- self.assertEqual(views.find_best_matches(u'zelenski', self.user), (self.t,))
-
+ self.assertEqual(views.find_best_matches(u'Żelenski'), (self.unicode_tag,))
+ self.assertEqual(views.find_best_matches(u'zelenski'), (self.unicode_tag,))
+
+
+class PersonStub(object):
+
+ def __init__(self, first_names, last_name):
+ self.first_names = first_names
+ self.last_name = last_name
+
+class BookInfoStub(object):
+ def __init__(self, **kwargs):
+ self.__dict = kwargs
+
+ def __setattr__(self, key, value):
+ if not key.startswith('_'):
+ self.__dict[key] = value
+ return object.__setattr__(self, key, value)
+
+ def __getattr__(self, key):
+ return self.__dict[key]
+
+ def to_dict(self):
+ return dict((key, unicode(value)) for key, value in self.__dict.items())
+
+class BookImportLogicTests(TestCase):
-class SetSearchTest(TestCase):
def setUp(self):
- self.me = User(name='me')
- self.other = User(name='other')
+ self.book_info = BookInfoStub(
+ url=u"http://wolnelektury.pl/example/default_book",
+ about=u"http://wolnelektury.pl/example/URI/default_book",
+ title=u"Default Book",
+ author=PersonStub(("Jim",), "Lazy"),
+ kind="X-Kind",
+ genre="X-Genre",
+ epoch="X-Epoch",
+ )
+
+ self.expected_tags = [
+ ('author', 'jim-lazy'),
+ ('book', 'l-default_book'),
+ ('genre', 'x-genre'),
+ ('epoch', 'x-epoch'),
+ ('kind', 'x-kind'),
+ ]
+ self.expected_tags.sort()
+
+ def test_empty_book(self):
+ BOOK_TEXT = "
Ala ma kota
\n') + + self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.tags ]) + + def test_book_replace_title(self): + BOOK_TEXT = """