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