1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from unittest import skipIf
6 from django.conf import settings
8 from catalogue.test_utils import WLTestCase, get_fixture
9 from catalogue.models import Book
10 from librarian import WLURI, XMLNamespace
11 from search.index import Index
13 AtomNS = XMLNamespace("http://www.w3.org/2005/Atom")
16 @skipIf(getattr(settings, 'NO_SEARCH_INDEX', False), 'Requires search server and NO_SEARCH_INDEX=False.')
17 class OpdsSearchTests(WLTestCase):
18 """Tests search feed in OPDS.."""
20 WLTestCase.setUp(self)
22 index.index.delete_all()
25 self.do_doktora = Book.from_xml_file(
26 get_fixture('do-doktora.xml'))
27 self.do_anusie = Book.from_xml_file(
28 get_fixture('fraszka-do-anusie.xml', catalogue))
30 def assert_finds(self, query, books):
31 """Takes a query and tests against books expected to be found."""
32 tree = etree.fromstring(
33 self.client.get('/opds/search/?%s' % query).content)
34 elem_ids = tree.findall('.//%s/%s' % (AtomNS('entry'), AtomNS('id')))
35 slugs = [WLURI(elem.text).slug for elem in elem_ids]
36 self.assertEqual(set(slugs), set(b.slug for b in books), "OPDS search '%s' failed." % query)
38 def test_opds_search_simple(self):
39 """Do a simple q= test, also emulate dumb OPDS clients."""
40 both = {self.do_doktora, self.do_anusie}
41 self.assert_finds('q=fraszka', both)
42 self.assert_finds('q=fraszka&author={opds:author}', both)
44 def test_opds_search_title(self):
45 """Search by title."""
46 both = {self.do_doktora, self.do_anusie}
47 self.assert_finds('title=fraszka', both)
48 self.assert_finds('title=fraszka', both)
49 self.assert_finds('q=title:doktora', [self.do_doktora])
51 def test_opds_search_author(self):
52 """Search by author."""
53 self.assert_finds('q=fraszka&author=Kochanowski', [self.do_doktora])
54 self.assert_finds('q=fraszka+author:Kochanowski', [self.do_doktora])
55 self.assert_finds('q=Kochanowski', [self.do_doktora])
57 def test_opds_search_translator(self):
58 """Search by translator."""
59 self.assert_finds('q=fraszka&translator=Fikcyjny', [self.do_doktora])
60 self.assert_finds('q=fraszka+translator:Fikcyjny', [self.do_doktora])
61 self.assert_finds('q=Fikcyjny', [self.do_doktora])