wip change fb2 api
[librarian.git] / tests / test_html_examples.py
1 import io
2 import os
3 from unittest import TestCase
4 from librarian.builders import HtmlBuilder
5 from librarian.document import WLDocument
6 from librarian.elements import WL_ELEMENTS
7 from .utils import get_fixture, get_all_fixtures
8
9
10 class HtmlExamplesTests(TestCase):
11     maxDiff = None
12
13     @classmethod
14     def setUpClass(cls):
15         with open(get_fixture('tags', 'base.xml'), 'rb') as f:
16             cls.base_xml = f.read()
17     
18     def test_examples(self):
19         for tag in WL_ELEMENTS:
20             with self.subTest(tag):
21                 self.tag_test(tag)
22         for path in get_all_fixtures('tags'):
23             if os.path.isdir(path):
24                 name = path.rsplit('/', 1)[1]
25                 self.assertIn(name, WL_ELEMENTS)
26
27     def tag_test(self, tag):
28         for fixture in get_all_fixtures(f'tags/{tag}', '*.xml'):
29             with self.subTest(tag=tag, n=fixture.rsplit('/', 1)[-1].rsplit('.', 1)[0]):
30                 with open(fixture, 'rb') as f:
31                     xml_input = f.read()
32                 xml_file = io.BytesIO(self.base_xml.replace(b'<!-- INPUT -->', xml_input))
33                 doc = WLDocument(filename=xml_file)
34                 html = HtmlBuilder(base_url='/').build(doc).get_bytes().decode('utf-8')
35
36                 with open(fixture.rsplit('.', 1)[0] + '.expected.html', 'r') as f:
37                     expected_html = f.read()
38                 try:
39                     with open(fixture.rsplit('.', 1)[0] + '.expected.toc.html', 'r') as f:
40                         expected_toc = f.read()
41                 except:
42                     expected_toc = ''
43                 try:
44                     with open(fixture.rsplit('.', 1)[0] + '.expected.themes.html', 'r') as f:
45                         expected_themes = f.read()
46                 except:
47                     expected_themes = ''
48
49                 self.assertEqual(html, expected_html)