Basic biblical tools.
[librarian.git] / tests / test_dcparser.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 dcparser
6 from lxml import etree
7 from os.path import splitext
8 from tests.utils import get_all_fixtures
9
10
11 class MetaTests(unittest.TestCase):
12     def check_dcparser(self, xml_file, result_file):
13         with open(xml_file, 'rb') as f:
14             xml = f.read()
15         with open(result_file) as f:
16             result = f.read()
17         info = dcparser.BookInfo.from_bytes(xml).to_dict()
18         should_be = eval(result)
19         for key in should_be:
20             self.assertEqual(info[key], should_be[key])
21
22
23     def test_dcparser(self):
24         for fixture in get_all_fixtures('dcparser', '*.xml'):
25             base_name = splitext(fixture)[0]
26             with self.subTest(name=base_name):
27                 self.check_dcparser(fixture, base_name + '.out')
28
29     def check_serialize(self, xml_file):
30         with open(xml_file, 'rb') as f:
31             xml = f.read()
32         info = dcparser.BookInfo.from_bytes(xml)
33
34         # serialize
35         serialized = etree.tostring(info.to_etree(), encoding='unicode').encode('utf-8')
36         # then parse again
37         info_bis = dcparser.BookInfo.from_bytes(serialized)
38
39         # check if they are the same
40         for key in vars(info):
41             self.assertEqual(getattr(info, key), getattr(info_bis, key))
42         for key in vars(info_bis):
43             self.assertEqual(getattr(info, key), getattr(info_bis, key))
44
45     def test_serialize(self):
46         for fixture in get_all_fixtures('dcparser', '*.xml'):
47             with self.subTest(name=fixture):
48                 self.check_serialize(fixture)