-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementtree_test.py
More file actions
341 lines (266 loc) · 9.21 KB
/
elementtree_test.py
File metadata and controls
341 lines (266 loc) · 9.21 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""Processing XML using `xml.etree.ElementTree`."""
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import List, Optional, Tuple, Union
from xml.dom import minidom
import pytest
def test_parse_string_pretty_print() -> None:
"""Parse XML from a string.
- Pretty-print using `xml.dom.minidom`
"""
root: ET.Element = ET.fromstring(
'<data><country name="Liechtenstein"/><country name="Panama"/></data>'
)
xml_string = ET.tostring(root, encoding="unicode")
assert (
xml_string
== '<data><country name="Liechtenstein" /><country name="Panama" /></data>'
)
minidom_doc: minidom.Document = minidom.parseString(xml_string)
assert (
minidom_doc.toprettyxml(indent=" ")
== """\
<?xml version="1.0" ?>
<data>
<country name="Liechtenstein"/>
<country name="Panama"/>
</data>
"""
)
def test_parse_file() -> None:
"""Parse XML from a file."""
sample_path = Path(__file__).parent.joinpath("elementtree_sample1.xml")
with open(sample_path) as xml_file:
tree: ET.ElementTree = ET.parse(xml_file)
root: ET.Element = tree.getroot()
assert root.tag == "data"
def test_traverse_all() -> None:
"""Traverse through all elements in a tree."""
root = ET.fromstring(
'<data><country name="Liechtenstein"/><country name="Panama"/></data>'
)
assert [elem.tag for elem in root.iter()] == ["data", "country", "country"]
def test_find() -> None:
"""Find elements in a tree."""
sample_path = Path(__file__).parent.joinpath("elementtree_sample1.xml")
with open(sample_path) as xml_file:
tree = ET.parse(xml_file)
assert [neighbor.get("name") for neighbor in tree.findall(".//neighbor")] == [
"Austria",
"Switzerland",
"Costa Rica",
"Colombia",
]
ranks = []
for country in tree.findall("country"):
rank: Optional[ET.Element] = country.find("rank")
ranks.append((country.get("name"), rank.text if rank is not None else ""))
assert ranks == [("Liechtenstein", "1"), ("Panama", "68")]
XMLType = Union[str, ET.Element, ET.ElementTree]
def assert_xml_equal(actual: XMLType, expected: XMLType) -> None:
"""Assert that the XML canonical forms of `expected` and `actual` are equal."""
def to_string(xml_data: XMLType) -> str:
if isinstance(xml_data, str):
return xml_data
if isinstance(xml_data, ET.ElementTree):
xml_data = xml_data.getroot()
return ET.tostring(xml_data, encoding="unicode")
assert ET.canonicalize(to_string(actual), strip_text=True) == ET.canonicalize(
to_string(expected), strip_text=True
)
def test_modify() -> None:
"""Modify elements and attributes in an XML document."""
sample_path = Path(__file__).parent.joinpath("elementtree_sample1.xml")
with open(sample_path) as xml_file:
tree = ET.parse(xml_file)
remove_neighbors(tree)
insert_country(tree)
append_country(tree)
replace_rank_text_with_attribute(tree)
def remove_neighbors(tree: ET.ElementTree) -> None:
"""Remove `neighbor` elements from `tree`."""
for country in tree.findall("country"):
for neighbor in country.findall("neighbor"):
country.remove(neighbor)
expected = """\
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
</country>
</data>"""
assert_xml_equal(tree, expected)
def insert_country(tree: ET.ElementTree) -> None:
"""Insert a `country` element after 'Liechtenstein'."""
monaco: ET.Element = ET.XML('<country name="Monaco"><rank>2</rank></country>')
tree.getroot().insert(1, monaco)
expected = """\
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
</country>
<country name="Monaco">
<rank>2</rank>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
</country>
</data>"""
assert_xml_equal(tree, expected)
def append_country(tree: ET.ElementTree) -> None:
"""Append a `country` element to `tree`."""
malaysia = ET.XML('<country name="Malaysia"><rank>69</rank></country>')
tree.getroot().append(malaysia)
expected = """\
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
</country>
<country name="Monaco">
<rank>2</rank>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
</country>
<country name="Malaysia">
<rank>69</rank>
</country>
</data>"""
assert_xml_equal(tree, expected)
def replace_rank_text_with_attribute(tree: ET.ElementTree) -> None:
"""Replace `rank` element text with a `value` attribute."""
for country in tree.findall("country"):
rank_elem = country.find("rank")
assert rank_elem is not None
rank_elem.set("value", rank_elem.text if rank_elem.text else "")
rank_elem.text = None
expected = """\
<data>
<country name="Liechtenstein">
<rank value="1"/>
<year>2008</year>
<gdppc>141100</gdppc>
</country>
<country name="Monaco">
<rank value="2"/>
</country>
<country name="Panama">
<rank value="68"/>
<year>2011</year>
<gdppc>13600</gdppc>
</country>
<country name="Malaysia">
<rank value="69"/>
</country>
</data>"""
assert_xml_equal(tree, expected)
def test_build() -> None:
"""Build an XML document."""
root = ET.Element("data")
root.append(ET.Comment("GDP per capita ranking"))
country = ET.SubElement(root, "country", {"name": "Liechtenstein"})
rank = ET.SubElement(country, "rank")
rank.text = "1"
def create_country(name: str, rank: str) -> ET.Element:
country = ET.Element("country", {"name": name})
ET.SubElement(country, "rank").text = rank
return country
country_ranks: List[Tuple[str, str]] = [("Monaco", "2"), ("Panama", "68")]
root.extend([create_country(cr[0], cr[1]) for cr in country_ranks])
expected = """\
<data>
<!-- GDP per capita ranking -->
<country name="Liechtenstein">
<rank>1</rank>
</country>
<country name="Monaco">
<rank>2</rank>
</country>
<country name="Panama">
<rank>68</rank>
</country>
</data>"""
assert_xml_equal(root, expected)
def test_write(tmp_path: Path) -> None:
"""Write an XML document to a file."""
root = ET.fromstring(
'<data><country name="Liechtenstein"><rank>1</rank></country></data>'
)
tmp_xml_path: Path = tmp_path.joinpath("gdp.xml")
ET.ElementTree(root).write(tmp_xml_path, "UTF-8", True)
with open(tmp_xml_path) as xml_file:
xml_data = xml_file.read()
assert (
xml_data
== """\
<?xml version='1.0' encoding='UTF-8'?>
<data><country name="Liechtenstein"><rank>1</rank></country></data>"""
)
def test_pretty_write(tmp_path: Path) -> None:
"""Write a pretty XML document to a file."""
root = ET.fromstring(
'<data><country name="Liechtenstein"><rank>1</rank></country></data>'
)
minidom_doc: minidom.Document = minidom.parseString(
ET.tostring(root, encoding="unicode")
)
tmp_xml_path = tmp_path.joinpath("gdp_pretty.xml")
with open(tmp_xml_path, mode="w") as pretty_xml_file:
minidom_doc.writexml(pretty_xml_file, addindent=" ", newl="\n")
with open(tmp_xml_path) as pretty_read:
pretty_data = pretty_read.read()
assert (
pretty_data
== """\
<?xml version="1.0" ?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
</country>
</data>
"""
)
def test_namespace_manual() -> None:
"""Search elements with namespace by manually adding the URI to every tag."""
sample_path = Path(__file__).parent.joinpath("elementtree_sample2.xml")
with open(sample_path) as xml_file:
tree = ET.parse(xml_file)
actor = tree.find("{http://people.example.com}actor")
assert actor is not None
name = actor.find("{http://people.example.com}name")
assert name is not None
characters = [
char.text for char in actor.findall("{http://characters.example.com}character")
]
actor_chars = {name.text: characters}
assert actor_chars == {"John Cleese": ["Lancelot", "Archie Leach"]}
def test_namespace_dict() -> None:
"""Search elements with namespace added to a dictionary."""
sample_path = Path(__file__).parent.joinpath("elementtree_sample2.xml")
with open(sample_path) as xml_file:
tree = ET.parse(xml_file)
namespaces = {
"people": "http://people.example.com",
"role": "http://characters.example.com",
}
actor = tree.find("people:actor", namespaces)
assert actor is not None
name = actor.find("people:name", namespaces)
assert name is not None
characters = [char.text for char in actor.findall("role:character", namespaces)]
actor_chars = {name.text: characters}
assert actor_chars == {"John Cleese": ["Lancelot", "Archie Leach"]}