EPUB3 support.
[librarian.git] / tests / test_epub.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from __future__ import unicode_literals
7
8 import subprocess
9 from zipfile import ZipFile
10 from ebooklib import epub
11 from lxml import html
12 from nose.tools import *
13 from librarian import DirDocProvider
14 from librarian.parser import WLDocument
15 from tests.utils import get_fixture
16
17
18 def test_transform():
19     epub_file = WLDocument.from_file(
20             get_fixture('text', 'asnyk_zbior.xml'),
21             provider=DirDocProvider(get_fixture('text', ''))
22         ).as_epub(cover=True, flags=['without_fonts'])
23     zipf = ZipFile(epub_file.get_file())
24
25     # Check contributor list.
26     last = zipf.open('EPUB/last.xhtml')
27     tree = html.parse(last)
28     editors_attribution = False
29     for par in tree.findall("//p"):
30         if par.text.startswith(u'Opracowanie redakcyjne i przypisy:'):
31             editors_attribution = True
32             assert_equal(
33                 par.text.rstrip(),
34                 u'Opracowanie redakcyjne i przypisy: '
35                 u'Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska.')
36     assert_true(editors_attribution)
37
38     # Check that we have a valid EPUB.
39     assert_equal(
40         subprocess.call([
41             'epubcheck', '-quiet', epub_file.get_filename()
42         ]),
43         0
44     )
45
46     book = epub.read_epub(epub_file.get_filename())
47
48     # Check that guide items are there.
49     assert_equals(
50         book.guide,
51         [
52             {'href': 'part1.xhtml', 'title': 'Początek', 'type': 'text'},
53             {'href': 'cover.xhtml', 'title': 'Okładka', 'type': 'cover'}
54         ]
55     )
56
57     # Check that metadata is there.
58     DC = "http://purl.org/dc/elements/1.1/"
59     OPF = "http://www.idpf.org/2007/opf"
60
61     assert_equals(
62         book.get_metadata(OPF, "cover"),
63         [(None, {'name': 'cover', 'content': 'cover-img'})]
64     )
65     assert_equals(
66         book.get_metadata(DC, "title"),
67         [('Poezye', {})]
68     )
69     assert_equals(
70         book.get_metadata(DC, "language"),
71         [('pl', {})]
72     )
73     assert_equals(
74         book.get_metadata(DC, "identifier"),
75         [('http://wolnelektury.pl/katalog/lektura/poezye', {
76             'id': 'id',
77         })]
78     )
79     assert_equals(
80         book.get_metadata(DC, "creator"),
81         [('Adam Asnyk', {"id": "creator"})]
82     )
83     assert_equals(
84         book.get_metadata(DC, "publisher"),
85         [('Fundacja Nowoczesna Polska', {})]
86     )
87     assert_equals(
88         book.get_metadata(DC, "date"),
89         [("2007-09-06", {})]
90     )
91
92
93 def test_transform_hyphenate():
94     epub = WLDocument.from_file(
95             get_fixture('text', 'asnyk_zbior.xml'),
96             provider=DirDocProvider(get_fixture('text', ''))
97         ).as_epub(
98             flags=['without_fonts'],
99             hyphenate=True
100         ).get_file()