Tests for OPDS search
[wolnelektury.git] / apps / opds / tests / __init__.py
1 # -*- coding: utf-8 -*-
2 from lxml import etree
3 from django.core.files.base import ContentFile
4 import catalogue
5 from catalogue.test_utils import (BookInfoStub, PersonStub, info_args,
6         WLTestCase, get_fixture)
7 from catalogue.models import Book
8 from librarian import WLURI, XMLNamespace
9
10 AtomNS = XMLNamespace("http://www.w3.org/2005/Atom")
11
12
13 class OpdsSearchTests(WLTestCase):
14     """Tests search feed in OPDS.."""
15     def setUp(self):
16         WLTestCase.setUp(self)
17         with self.settings(NO_SEARCH_INDEX=False):
18             self.do_doktora = Book.from_xml_file(
19                 get_fixture('do-doktora.xml'))
20             self.do_anusie = Book.from_xml_file(
21                 get_fixture('fraszka-do-anusie.xml', catalogue))
22
23     def assert_finds(self, query, books):
24         """Takes a query and tests against books expected to be found."""
25         tree = etree.fromstring(
26             self.client.get('/opds/search/?%s' % query).content)
27         elem_ids = tree.findall('.//%s/%s' % (AtomNS('entry'), AtomNS('id')))
28         slugs = [WLURI(elem.text).slug for elem in elem_ids]
29         self.assertEqual(set(slugs), set(b.slug for b in books),
30             u"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 = set([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 = set([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:fraszka', [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])