12f6f7d1c95bcccd7998a11cd6e17eb2238d0618
[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         index.index.delete_all()
22         index.index.commit()
23
24         with self.settings(NO_SEARCH_INDEX=False):
25             self.do_doktora = Book.from_xml_file(
26                 get_fixture('do-doktora.xml', opds))
27             self.do_anusie = Book.from_xml_file(
28                 get_fixture('fraszka-do-anusie.xml', catalogue))
29
30         self.search = Search()
31
32     def test_search_perfect_book_author(self):
33         books = self.search.search_books(self.search.index.query(authors=u"sęp szarzyński"))
34         assert len(books) == 1
35         assert books[0].book_id == self.book.id
36
37     def test_search_perfect_book_title(self):
38         books = self.search.search_books(self.search.index.query(u"fraszka anusie"))
39         assert len(books) == 1
40         assert books[0].book_id == self.book.id
41
42     def test_search_perfect_parts(self):
43         books = self.search.search_phrase(u"Jakoż hamować")
44         assert len(books) == 2
45         for b in books:
46             b.book_id == self.book.id
47         a = SearchResult.aggregate(books)
48         # just one fragment hit.
49         assert len(a[0].hits) == 1
50
51     def test_search_perfect_author_title(self):
52         books = self.search.search_books(self.search.index.query(authors=u"szarzyński anusie"))
53         assert books == []
54
55