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