OPDS with new SOLR interface.
[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 from search import Index, Search
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         index = Index()
19         index.index.delete_all()
20         index.index.commit()
21
22         with self.settings(NO_SEARCH_INDEX=False):
23             self.do_doktora = Book.from_xml_file(
24                 get_fixture('do-doktora.xml'))
25             self.do_anusie = Book.from_xml_file(
26                 get_fixture('fraszka-do-anusie.xml', catalogue))
27
28     def assert_finds(self, query, books):
29         """Takes a query and tests against books expected to be found."""
30         tree = etree.fromstring(
31             self.client.get('/opds/search/?%s' % query).content)
32         elem_ids = tree.findall('.//%s/%s' % (AtomNS('entry'), AtomNS('id')))
33         slugs = [WLURI(elem.text).slug for elem in elem_ids]
34         self.assertEqual(set(slugs), set(b.slug for b in books),
35             u"OPDS search '%s' failed." % query)
36
37     def test_opds_search_simple(self):
38         """Do a simple q= test, also emulate dumb OPDS clients."""
39         both = set([self.do_doktora, self.do_anusie])
40         self.assert_finds('q=fraszka', both)
41         self.assert_finds('q=fraszka&author={opds:author}', both)
42
43     def test_opds_search_title(self):
44         """Search by title."""
45         both = set([self.do_doktora, self.do_anusie])
46         self.assert_finds('title=fraszka', both)
47         self.assert_finds('title=fraszka', both)
48         self.assert_finds('q=title:doktora', [self.do_doktora])
49
50     def test_opds_search_author(self):
51         """Search by author."""
52         self.assert_finds('q=fraszka&author=Kochanowski', [self.do_doktora])
53         self.assert_finds('q=fraszka+author:Kochanowski', [self.do_doktora])
54         self.assert_finds('q=Kochanowski', [self.do_doktora])
55
56     def test_opds_search_translator(self):
57         """Search by translator."""
58         self.assert_finds('q=fraszka&translator=Fikcyjny', [self.do_doktora])
59         self.assert_finds('q=fraszka+translator:Fikcyjny', [self.do_doktora])
60         self.assert_finds('q=Fikcyjny', [self.do_doktora])