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.
5 from unittest import skipIf
7 from django.conf import settings
9 from catalogue.test_utils import WLTestCase, get_fixture
10 from catalogue.models import Book
11 from librarian import WLURI, XMLNamespace
12 from search.index import Index
14 AtomNS = XMLNamespace("http://www.w3.org/2005/Atom")
17 @skipIf(getattr(settings, 'NO_SEARCH_INDEX', False), u'Requires search server and NO_SEARCH_INDEX=False.')
18 class OpdsSearchTests(WLTestCase):
19 """Tests search feed in OPDS.."""
21 WLTestCase.setUp(self)
23 index.index.delete_all()
26 self.do_doktora = Book.from_xml_file(
27 get_fixture('do-doktora.xml'))
28 self.do_anusie = Book.from_xml_file(
29 get_fixture('fraszka-do-anusie.xml', catalogue))
31 def assert_finds(self, query, books):
32 """Takes a query and tests against books expected to be found."""
33 tree = etree.fromstring(
34 self.client.get('/opds/search/?%s' % query).content)
35 elem_ids = tree.findall('.//%s/%s' % (AtomNS('entry'), AtomNS('id')))
36 slugs = [WLURI(elem.text).slug for elem in elem_ids]
37 self.assertEqual(set(slugs), set(b.slug for b in books), u"OPDS search '%s' failed." % query)
39 def test_opds_search_simple(self):
40 """Do a simple q= test, also emulate dumb OPDS clients."""
41 both = {self.do_doktora, self.do_anusie}
42 self.assert_finds('q=fraszka', both)
43 self.assert_finds('q=fraszka&author={opds:author}', both)
45 def test_opds_search_title(self):
46 """Search by title."""
47 both = {self.do_doktora, self.do_anusie}
48 self.assert_finds('title=fraszka', both)
49 self.assert_finds('title=fraszka', both)
50 self.assert_finds('q=title:doktora', [self.do_doktora])
52 def test_opds_search_author(self):
53 """Search by author."""
54 self.assert_finds('q=fraszka&author=Kochanowski', [self.do_doktora])
55 self.assert_finds('q=fraszka+author:Kochanowski', [self.do_doktora])
56 self.assert_finds('q=Kochanowski', [self.do_doktora])
58 def test_opds_search_translator(self):
59 """Search by translator."""
60 self.assert_finds('q=fraszka&translator=Fikcyjny', [self.do_doktora])
61 self.assert_finds('q=fraszka+translator:Fikcyjny', [self.do_doktora])
62 self.assert_finds('q=Fikcyjny', [self.do_doktora])