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