Remove DateValue, drop Py<3.6, fix tests.
[librarian.git] / tests / test_dcparser.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from __future__ import unicode_literals
7
8 from librarian import dcparser
9 from lxml import etree
10 from nose.tools import *
11 from os.path import splitext
12 from tests.utils import get_all_fixtures
13 import codecs
14 from datetime import date
15
16
17 def check_dcparser(xml_file, result_file):
18     xml = open(xml_file, 'rb').read()
19     result = codecs.open(result_file, encoding='utf-8').read()
20     info = dcparser.BookInfo.from_bytes(xml).to_dict()
21     should_be = eval(result)
22     for key in should_be:
23         assert_equals(info[key], should_be[key])
24
25
26 def test_dcparser():
27     for fixture in get_all_fixtures('dcparser', '*.xml'):
28         base_name = splitext(fixture)[0]
29         yield check_dcparser, fixture, base_name + '.out'
30
31
32 def check_serialize(xml_file):
33     xml = open(xml_file, 'rb').read()
34     info = dcparser.BookInfo.from_bytes(xml)
35
36     # serialize
37     serialized = etree.tostring(info.to_etree(), encoding='unicode').encode('utf-8')
38     # then parse again
39     info_bis = dcparser.BookInfo.from_bytes(serialized)
40
41     # check if they are the same
42     for key in vars(info):
43         assert_equals(getattr(info, key), getattr(info_bis, key))
44     for key in vars(info_bis):
45         assert_equals(getattr(info, key), getattr(info_bis, key))
46
47
48 def test_serialize():
49     for fixture in get_all_fixtures('dcparser', '*.xml'):
50         yield check_serialize, fixture