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