3e3d290e254e20ce857453d3d39165af95c1045b
[wolnelektury.git] / apps / search / tests / index.py
1 # -*- coding: utf-8 -*-
2 from django.conf import settings
3 from django.test.utils import override_settings
4 from catalogue.test_utils import WLTestCase, get_fixture
5 from os import path
6 import tempfile
7 from catalogue.models import Book, Tag
8 from search import Index, Search, SearchResult
9 import catalogue
10 import opds
11
12
13 @override_settings(
14     SEARCH_INDEX = tempfile.mkdtemp(prefix='djangotest_search_'),
15 )
16 class BookSearchTests(WLTestCase):
17     def setUp(self):
18         WLTestCase.setUp(self)
19
20         index = Index()
21         self.search = Search()
22         index.delete_query(self.search.index.query(uid="*"))
23         index.index.commit()
24
25         with self.settings(NO_SEARCH_INDEX=False):
26             self.do_doktora = Book.from_xml_file(
27                 get_fixture('do-doktora.xml', opds))
28             self.do_anusie = Book.from_xml_file(
29                 get_fixture('fraszka-do-anusie.xml', catalogue))
30
31     def test_search_perfect_book_author(self):
32         books = self.search.search_books(self.search.index.query(authors=u"sęp szarzyński"))
33         assert len(books) == 1
34         assert books[0].id == self.do_anusie.id
35
36         # here we lack slop functionality as well
37     def test_search_perfect_book_title(self):
38         books = self.search.search_books(self.search.index.query(title=u"fraszka do anusie"))
39         assert len(books) == 1
40         assert books[0].id == self.do_anusie.id
41
42     # TODO: Add slop option to sunburnt
43     # def test_search_perfect_parts(self):
44     #     books = self.search.search_phrase(u"Jakoż hamować")
45     #     assert len(books) == 2
46     #     for b in books:
47     #         b.book_id == self.book.id
48     #     a = SearchResult.aggregate(books)
49     #     # just one fragment hit.
50     #     assert len(a[0].hits) == 1
51