-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xml_utils.py
103 lines (84 loc) · 2.89 KB
/
test_xml_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# coding=utf-8
import os.path
import unittest
from lxml import etree
from lxml_asserts.testcase import LxmlTestCaseMixin
from frontik.xml_util import dict_to_xml, xml_from_file, xml_to_dict
XML = etree.XML('''
<root>
<key1>value</key1>
<key2></key2>
<nested>
<key1>русский текст в utf-8</key1>
<key2>русский текст в unicode</key2>
</nested>
<complexNested>
<nested>
<key>value</key>
<otherKey>otherValue</otherKey>
</nested>
<int>123</int>
<bool>True</bool>
</complexNested>
</root>
''')
DICT_BEFORE = {
'key1': 'value',
'key2': '',
'nested': {
'key1': 'русский текст в utf-8',
'key2': u'русский текст в unicode'
},
'complexNested': {
'nested': {
'key': 'value',
'otherKey': 'otherValue'
},
'int': 123,
'bool': True
}
}
DICT_AFTER = {
'key1': 'value',
'key2': '',
'nested': {
'key1': u'русский текст в utf-8',
'key2': u'русский текст в unicode'
},
'complexNested': {
'nested': {
'key': 'value',
'otherKey': 'otherValue'
},
'int': '123',
'bool': 'True'
}
}
class TestXmlUtils(unittest.TestCase, LxmlTestCaseMixin):
def test_xml_to_dict_and_back_again(self):
self.assertEqual(xml_to_dict(XML), DICT_AFTER)
self.assertXmlEqual(dict_to_xml(DICT_BEFORE, 'root'), XML)
self.assertEqual(xml_to_dict(dict_to_xml(DICT_BEFORE, 'root')), DICT_AFTER)
self.assertXmlEqual(dict_to_xml(xml_to_dict(XML), 'root'), XML)
XML_FILE = os.path.join(os.path.dirname(__file__), 'projects', 'test_app', 'xml', 'aaa.xml')
XML_MISSING_FILE = os.path.join(os.path.dirname(__file__), 'bbb.xml')
XML_SYNTAX_ERROR_FILE = os.path.join(os.path.dirname(__file__), 'projects', 'test_app', 'xsl', 'syntax_error.xsl')
class MockLog(object):
def __init__(self):
self.message = None
def error(self, message, *args):
self.message = message % args
def test_xml_from_file(self):
result = xml_from_file(self.XML_FILE, TestXmlUtils.MockLog())
self.assertIn('Source:', result[0].text)
self.assertEqual(result[1].text, 'aaa')
def test_xml_from_file_does_not_exist(self):
log = TestXmlUtils.MockLog()
with self.assertRaises(IOError):
xml_from_file(self.XML_MISSING_FILE, log)
self.assertIn('failed to read xml file', log.message)
def test_xml_from_file_syntax_error(self):
log = TestXmlUtils.MockLog()
with self.assertRaises(etree.XMLSyntaxError):
xml_from_file(self.XML_SYNTAX_ERROR_FILE, log)
self.assertIn('failed to parse xml file', log.message)