779753013b439ea363e16120ed71568e5ce399d3
[librarian.git] / tests / test_text.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 unittest
5 from librarian import NoDublinCore
6 from librarian.builders import builders
7 from librarian.parser import WLDocument as LegacyWLDocument
8 from librarian.document import WLDocument
9 from .utils import get_fixture
10
11
12 class TextTests(unittest.TestCase):
13     maxDiff = None
14
15     def test_transform_legacy(self):
16         expected_output_file_path = get_fixture('text', 'asnyk_miedzy_nami_expected.txt')
17
18         text = LegacyWLDocument.from_file(
19             get_fixture('text', 'miedzy-nami-nic-nie-bylo.xml')
20         ).as_text().get_bytes().decode('utf-8')
21
22         with open(expected_output_file_path, 'rb') as f:
23             self.assertEqual(text, f.read().decode('utf-8'))
24
25     def test_transform(self):
26         expected_output_file_path = get_fixture('text', 'asnyk_miedzy_nami_expected.txt')
27
28         text = WLDocument(
29             filename=get_fixture('text', 'miedzy-nami-nic-nie-bylo.xml')
30         ).build(builders['txt']).get_bytes().decode('utf-8')
31
32         with open(expected_output_file_path, 'rb') as f:
33             self.assertEqual(text, f.read().decode('utf-8'))
34
35     
36     def test_transform_raw(self):
37         expected_output_file_path = get_fixture('text', 'asnyk_miedzy_nami_expected_raw.txt')
38
39         text = LegacyWLDocument.from_file(
40             get_fixture('text', 'miedzy-nami-nic-nie-bylo.xml')
41         ).as_text(flags=['raw-text']).get_bytes().decode('utf-8')
42
43         with open(expected_output_file_path, 'rb') as f:
44             self.assertEqual(text, f.read().decode('utf-8'))
45
46     def test_no_dublincore(self):
47         with self.assertRaises(NoDublinCore):
48             LegacyWLDocument.from_file(
49                 get_fixture('text', 'asnyk_miedzy_nami_nodc.xml')
50             ).as_text()
51
52     def test_passing_parse_dublincore_to_transform(self):
53         """Passing parse_dublincore=False to the constructor omits DublinCore parsing."""
54         LegacyWLDocument.from_file(
55             get_fixture('text', 'asnyk_miedzy_nami_nodc.xml'),
56             parse_dublincore=False,
57         ).as_text()