/
test.py
87 lines (69 loc) · 2.95 KB
/
test.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
#!/usr/bin/env python
"""Unit tests for the relaxml module."""
import unittest
import tempfile
from relaxml import xml as xml2dict
from relaxml import XML2Dict
class TestXML2Dict(unittest.TestCase):
def setUp(self):
self.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n'
def test_simple_xml_to_dict(self):
xml = self.xml + '<a><b>5</b><c>9</c></a>'
expected_output = {'a': {'b': '5', 'c': '9'}}
self.assertEqual(xml2dict(xml), expected_output)
def test_xml_to_list_of_values(self):
xml = self.xml + '<a><b>1</b><b>2</b><b>3</b></a>'
expected_output = {'a': {'b': ['1', '2', '3']}}
self.assertEqual(xml2dict(xml), expected_output)
def test_xml_to_mixture_of_lists_and_dicts(self):
xml = self.xml + '<a><b>1</b><b>2</b><c><d>3</d></c></a>'
expected_output = {'a': {'b': ['1', '2'], 'c': {'d': '3'}}}
self.assertEqual(xml2dict(xml), expected_output)
def test_xml_attributes_retained(self):
xml = self.xml + '<numbers one="1" two="2" />'
expected_output = {'numbers': {'one': '1', 'two': '2'}}
self.assertEqual(xml2dict(xml), expected_output)
def test_both_attributes_and_child_nodes(self):
xml = self.xml + '<a foo="foo">bar</a>'
expected_output = {'a': {'a': 'bar', 'foo': 'foo'}}
self.assertEqual(xml2dict(xml), expected_output)
def test_error_raised_when_passed_complicated_XML(self):
xml = self.xml + '<tag tag="foo">bar</tag>'
self.assertRaises(ValueError, xml2dict, xml)
def test_against_XML_namespaces(self):
namespaces_table = """
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>"""
xml = self.xml + namespaces_table
expected_output = {
'table': {
'tr': {
'td': ['Apples', 'Bananas']
}
}
}
self.assertEquals(xml2dict(xml), expected_output)
def test_node_attribute_has_same_name_as_child(self):
xml = self.xml + '<a b="foo"><b><c>1</c></b></a>'
expected_output = {'a': {'b': ['foo', {'c': '1'}]}}
self.assertEquals(xml2dict(xml), expected_output)
def test_parsing_XML_from_file_from_function(self):
xml = self.xml + '<a foo="bar" hello="word" />'
f = tempfile.TemporaryFile(mode="w+t")
f.write(xml)
f.seek(0)
expected_output = {'a': {'foo': 'bar', 'hello': 'word'}}
self.assertEquals(xml2dict(f), expected_output)
def test_parsing_XML_from_file_with_parse_method(self):
xml = self.xml + '<a foo="bar" hello="word" />'
f = tempfile.NamedTemporaryFile(mode="w+t")
f.write(xml)
f.seek(0)
expected_output = {'a': {'foo': 'bar', 'hello': 'word'}}
self.assertEquals(XML2Dict().parse(f.name), expected_output)
if __name__ == '__main__':
unittest.main()