Fundraising in PDF.
[wolnelektury.git] / src / opds / tests / test_opds.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from lxml import etree
5 from django.conf import settings
6 import catalogue
7 from catalogue.test_utils import WLTestCase, get_fixture
8 from catalogue.models import Book
9 from librarian import WLURI, XMLNamespace
10
11 AtomNS = XMLNamespace("http://www.w3.org/2005/Atom")
12
13
14 class OpdsSearchTests(WLTestCase):
15     """Tests search feed in OPDS.."""
16     def setUp(self):
17         WLTestCase.setUp(self)
18
19         self.do_doktora = Book.from_xml_file(
20             get_fixture('do-doktora.xml'))
21         self.do_anusie = Book.from_xml_file(
22             get_fixture('fraszka-do-anusie.xml', catalogue))
23
24     def assert_finds(self, query, books):
25         """Takes a query and tests against books expected to be found."""
26         tree = etree.fromstring(
27             self.client.get('/opds/search/?%s' % query).content)
28         elem_ids = tree.findall('.//%s/%s' % (AtomNS('entry'), AtomNS('id')))
29         slugs = [WLURI.from_text(elem.text).slug for elem in elem_ids]
30         self.assertEqual(set(slugs), set(b.slug for b in books), "OPDS search '%s' failed." % query)
31
32     def test_opds_search_simple(self):
33         """Do a simple q= test, also emulate dumb OPDS clients."""
34         both = {self.do_doktora, self.do_anusie}
35         self.assert_finds('q=fraszka', both)
36         self.assert_finds('q=fraszka&author={opds:author}', both)
37
38     def test_opds_search_title(self):
39         """Search by title."""
40         both = {self.do_doktora, self.do_anusie}
41         self.assert_finds('title=fraszka', both)
42         self.assert_finds('title=fraszka', both)
43         self.assert_finds('q=title:doktora', [self.do_doktora])
44
45     def test_opds_search_author(self):
46         """Search by author."""
47         self.assert_finds('q=fraszka&author=Kochanowski', [self.do_doktora])
48         self.assert_finds('q=fraszka+author:Kochanowski', [self.do_doktora])
49         self.assert_finds('q=Kochanowski', [self.do_doktora])
50
51     def test_opds_search_translator(self):
52         """Search by translator."""
53         self.assert_finds('q=fraszka&translator=Fikcyjny', [self.do_doktora])
54         self.assert_finds('q=fraszka+translator:Fikcyjny', [self.do_doktora])
55         self.assert_finds('q=Fikcyjny', [self.do_doktora])