62e664cd6aa454bec2b853119124f04d4cb9cb4b
[librarian.git] / tests / test_dcparser.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import unittest
5
6 from lxml import etree
7 from utils import get_file_path
8 from librarian import dcparser, html, ParseError
9 from utils import AutoTestMetaclass
10
11 class TestDCParser(unittest.TestCase):
12     __metaclass__ = AutoTestMetaclass
13
14     TEST_DIR = 'dcparser'
15
16     def run_auto_test(self, in_data, out_data):
17         info = dcparser.BookInfo.from_string(in_data).to_dict()
18         should_be = eval(out_data)
19         for key in should_be:
20             self.assertEqual( info[key], should_be[key] )
21
22 class TestDCSerialize(unittest.TestCase):
23     __metaclass__ = AutoTestMetaclass
24
25     TEST_DIR = 'dcserialize'
26
27     def run_auto_test(self, in_data, out_data):
28         import lxml.etree
29         # first parse the input
30         info = dcparser.BookInfo.from_string(in_data)
31
32         # serialize
33         serialized = lxml.etree.tostring(info.to_etree(), encoding=unicode).encode('utf-8')
34
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             self.assertEqual( getattr(info, key), getattr(info_bis, key))
41
42         for key in vars(info_bis):
43             self.assertEqual( getattr(info, key), getattr(info_bis, key))
44
45 class TestParserErrors(unittest.TestCase):
46     def test_error(self):
47         try:
48             html.transform(get_file_path('erroneous', 'asnyk_miedzy_nami.xml'),
49                            get_file_path('erroneous', 'asnyk_miedzy_nami.html'))
50             self.fail()
51         except ParseError:
52             pass
53             #self.assertEqual(e.position, (25, 13))    
54
55 if __name__ == '__main__':
56     unittest.main()