1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   5 from librarian import dcparser
 
   7 from os.path import splitext
 
   8 from tests.utils import get_all_fixtures
 
  11 class MetaTests(unittest.TestCase):
 
  12     def check_dcparser(self, xml_file, result_file):
 
  13         with open(xml_file, 'rb') as f:
 
  15         with open(result_file) as f:
 
  17         info = dcparser.BookInfo.from_bytes(xml).to_dict()
 
  18         should_be = eval(result)
 
  20             self.assertEqual(info[key], should_be[key])
 
  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')
 
  29     def check_serialize(self, xml_file):
 
  30         with open(xml_file, 'rb') as f:
 
  32         info = dcparser.BookInfo.from_bytes(xml)
 
  35         serialized = etree.tostring(info.to_etree(), encoding='unicode').encode('utf-8')
 
  37         info_bis = dcparser.BookInfo.from_bytes(serialized)
 
  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))
 
  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)