Drop lots of legacy code. Support Python 3.7-3.11.
[librarian.git] / tests / test_epub.py
1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import subprocess
5 import unittest
6 from zipfile import ZipFile
7 from ebooklib import epub
8 from lxml import html
9 from librarian import DirDocProvider
10 from librarian.builders import EpubBuilder
11 from librarian.document import WLDocument
12 from tests.utils import get_fixture
13
14
15 class EpubTests(unittest.TestCase):
16     def test_transform(self):
17         epub_file = EpubBuilder().build(
18             WLDocument(
19                 get_fixture('text', 'asnyk_zbior.xml'),
20                 provider=DirDocProvider(get_fixture('text', ''))
21             )
22         )
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('Opracowanie redakcyjne i przypisy:'):
31                 editors_attribution = True
32                 self.assertEqual(
33                     par.text.rstrip(),
34                     'Opracowanie redakcyjne i przypisy: '
35                     'Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska.')
36         self.assertTrue(editors_attribution)
37
38         # Check that we have a valid EPUB.
39         self.assertEqual(
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         self.assertEqual(
50             book.guide,
51             [
52                 {'href': 'cover.xhtml', 'title': 'Okładka', 'type': 'cover'},
53                 {'href': 'part1.xhtml', 'title': 'Początek', 'type': 'text'},
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         self.assertEqual(
62             book.get_metadata(OPF, "cover"),
63             [(None, {'name': 'cover', 'content': 'cover-img'})]
64         )
65         self.assertEqual(
66             book.get_metadata(DC, "title"),
67             [('Poezye', {})]
68         )
69         self.assertEqual(
70             book.get_metadata(DC, "language"),
71             [('pl', {})]
72         )
73         self.assertEqual(
74             book.get_metadata(DC, "identifier"),
75             [('http://wolnelektury.pl/katalog/lektura/poezye', {
76                 'id': 'id',
77             })]
78         )
79         self.assertEqual(
80             book.get_metadata(DC, "creator"),
81             [('Adam Asnyk', {"id": "creator0"})]
82         )
83         self.assertEqual(
84             book.get_metadata(DC, "publisher"),
85             [('Fundacja Wolne Lektury', {})]
86         )
87         self.assertEqual(
88             book.get_metadata(DC, "date"),
89             [("2007-09-06", {})]
90         )