/
test_xml.py
208 lines (176 loc) · 4.38 KB
/
test_xml.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
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division
from tempfile import NamedTemporaryFile
import sys
from petl.test.helpers import ieq
from petl.util import nrows, look
from petl.io.xml import fromxml
from petl.compat import urlopen
def test_fromxml():
# initial data
f = NamedTemporaryFile(delete=False, mode='wt')
data = """<table>
<tr>
<td>foo</td><td>bar</td>
</tr>
<tr>
<td>a</td><td>1</td>
</tr>
<tr>
<td>b</td><td>2</td>
</tr>
<tr>
<td>c</td><td>2</td>
</tr>
</table>"""
f.write(data)
f.close()
actual = fromxml(f.name, 'tr', 'td')
expect = (('foo', 'bar'),
('a', '1'),
('b', '2'),
('c', '2'))
ieq(expect, actual)
ieq(expect, actual) # verify can iterate twice
def test_fromxml_2():
# initial data
f = NamedTemporaryFile(delete=False, mode='wt')
data = """<table>
<tr>
<td v='foo'/><td v='bar'/>
</tr>
<tr>
<td v='a'/><td v='1'/>
</tr>
<tr>
<td v='b'/><td v='2'/>
</tr>
<tr>
<td v='c'/><td v='2'/>
</tr>
</table>"""
f.write(data)
f.close()
actual = fromxml(f.name, 'tr', 'td', 'v')
expect = (('foo', 'bar'),
('a', '1'),
('b', '2'),
('c', '2'))
ieq(expect, actual)
ieq(expect, actual) # verify can iterate twice
def test_fromxml_3():
# initial data
f = NamedTemporaryFile(delete=False, mode='wt')
data = """<table>
<row>
<foo>a</foo><baz><bar v='1'/></baz>
</row>
<row>
<foo>b</foo><baz><bar v='2'/></baz>
</row>
<row>
<foo>c</foo><baz><bar v='2'/></baz>
</row>
</table>"""
f.write(data)
f.close()
actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': ('baz/bar', 'v')})
# N.B., requested fields come out in name sorted order
expect = (('bar', 'foo'),
('1', 'a'),
('2', 'b'),
('2', 'c'))
ieq(expect, actual)
ieq(expect, actual) # verify can iterate twice
def test_fromxml_4():
# initial data
f = NamedTemporaryFile(delete=False, mode='wt')
data = """<table>
<row>
<foo>a</foo><baz><bar>1</bar><bar>3</bar></baz>
</row>
<row>
<foo>b</foo><baz><bar>2</bar></baz>
</row>
<row>
<foo>c</foo><baz><bar>2</bar></baz>
</row>
</table>"""
f.write(data)
f.close()
actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': './/bar'})
# N.B., requested fields come out in name sorted order
expect = (('bar', 'foo'),
(('1', '3'), 'a'),
('2', 'b'),
('2', 'c'))
ieq(expect, actual)
ieq(expect, actual) # verify can iterate twice
def test_fromxml_5():
# initial data
f = NamedTemporaryFile(delete=False, mode='wt')
data = """<table>
<row>
<foo>a</foo><baz><bar v='1'/><bar v='3'/></baz>
</row>
<row>
<foo>b</foo><baz><bar v='2'/></baz>
</row>
<row>
<foo>c</foo><baz><bar v='2'/></baz>
</row>
</table>"""
f.write(data)
f.close()
actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': ('baz/bar', 'v')})
# N.B., requested fields come out in name sorted order
expect = (('bar', 'foo'),
(('1', '3'), 'a'),
('2', 'b'),
('2', 'c'))
ieq(expect, actual)
ieq(expect, actual) # verify can iterate twice
def test_fromxml_6():
data = """<table class='petl'>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td style='text-align: right'>2</td>
</tr>
<tr>
<td>b</td>
<td style='text-align: right'>1</td>
</tr>
<tr>
<td>c</td>
<td style='text-align: right'>3</td>
</tr>
</tbody>
</table>"""
f = NamedTemporaryFile(delete=False, mode='wt')
f.write(data)
f.close()
actual = fromxml(f.name, './/tr', ('th', 'td'))
print(look(actual))
expect = (('foo', 'bar'),
('a', '2'),
('b', '1'),
('c', '3'))
ieq(expect, actual)
ieq(expect, actual) # verify can iterate twice
url = 'http://feeds.bbci.co.uk/news/rss.xml'
# check internet connection
try:
urlopen(url)
except Exception as e:
print('SKIP test_fromxml_url: %s' % e, file=sys.stderr)
else:
def test_fromxml_url():
tbl = fromxml(url, './/item', 'title')
assert nrows(tbl) > 0