Added copyright notice about AGPL. Removed setuputils in favour of plain distutils.
[librarian.git] / tests / test_dcparser.py
1 # -*- coding: utf-8 -*-
2 #
3 #    This file is part of Librarian.
4 #
5 #    Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
6 #    
7 #    For full list of contributors see AUTHORS file. 
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU Affero General Public License as published by
11 #    the Free Software Foundation, either version 3 of the License, or
12 #    (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU Affero General Public License for more details.
18 #
19 #    You should have received a copy of the GNU Affero General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 from librarian import dcparser
24 from lxml import etree
25 from nose.tools import *
26 from os.path import splitext
27 from tests.utils import get_all_fixtures
28 import codecs
29
30
31 def check_dcparser(xml_file, result_file):
32     xml = file(xml_file).read()
33     result = codecs.open(result_file, encoding='utf-8').read()
34     info = dcparser.BookInfo.from_string(xml).to_dict()
35     should_be = eval(result)
36     for key in should_be:
37         assert_equals(info[key], should_be[key])
38
39
40 def test_dcparser():
41     for fixture in get_all_fixtures('dcparser', '*.xml'):
42         base_name = splitext(fixture)[0]
43         yield check_dcparser, fixture, base_name + '.out'
44
45
46 def check_serialize(xml_file):
47     xml = file(xml_file).read()
48     info = dcparser.BookInfo.from_string(xml)
49
50     # serialize
51     serialized = etree.tostring(info.to_etree(), encoding=unicode).encode('utf-8')
52     # then parse again
53     info_bis = dcparser.BookInfo.from_string(serialized)
54
55     # check if they are the same
56     for key in vars(info):
57         assert_equals(getattr(info, key), getattr(info_bis, key))
58     for key in vars(info_bis):
59         assert_equals(getattr(info, key), getattr(info_bis, key))
60
61
62 def test_serialize():
63     for fixture in get_all_fixtures('dcparser', '*.xml'):
64         yield check_serialize, fixture
65