cab5b1cd5d21c29290ee14e9a364667b3d774c0e
[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 from datetime import date
13
14
15 def check_dcparser(xml_file, result_file):
16     xml = file(xml_file).read()
17     result = codecs.open(result_file, encoding='utf-8').read()
18     info = dcparser.BookInfo.from_string(xml).to_dict()
19     should_be = eval(result)
20     for key in should_be:
21         assert_equals(info[key], should_be[key])
22
23
24 def test_dcparser():
25     for fixture in get_all_fixtures('dcparser', '*.xml'):
26         base_name = splitext(fixture)[0]
27         yield check_dcparser, fixture, base_name + '.out'
28
29
30 def check_serialize(xml_file):
31     xml = file(xml_file).read()
32     info = dcparser.BookInfo.from_string(xml)
33
34     # serialize
35     serialized = etree.tostring(info.to_etree(), encoding=unicode).encode('utf-8')
36     # then parse again
37     info_bis = dcparser.BookInfo.from_string(serialized)
38
39     # check if they are the same
40     for key in vars(info):
41         assert_equals(getattr(info, key), getattr(info_bis, key))
42     for key in vars(info_bis):
43         assert_equals(getattr(info, key), getattr(info_bis, key))
44
45
46 def test_serialize():
47     for fixture in get_all_fixtures('dcparser', '*.xml'):
48         yield check_serialize, fixture
49
50
51 def test_asdate():
52     assert_equals(dcparser.as_date(u"2010-10-03"), date(2010, 10, 03))
53     assert_equals(dcparser.as_date(u"2011"), date(2011, 1, 1))
54     assert_equals(dcparser.as_date(u"2 poł. XIX w."), date(1950, 1, 1))
55     assert_equals(dcparser.as_date(u"XVII w., l. 20"), date(1720, 1, 1))
56     assert_equals(dcparser.as_date(u"po 1460"), date(1460, 1, 1))
57     assert_equals(dcparser.as_date(u"ok. 1813-1814"), date(1813, 1, 1))
58     assert_equals(dcparser.as_date(u"ok.1876-ok.1886"), date(1876, 1, 1))
59     assert_equals(dcparser.as_date(u"1893/1894"), date(1893, 1, 1))