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