/
test_headers.py
276 lines (208 loc) · 6.8 KB
/
test_headers.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
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
from __future__ import absolute_import, print_function, division
from petl.test.helpers import ieq
from petl.errors import FieldSelectionError
from petl.util import fieldnames
from petl.transform.headers import setheader, extendheader, pushheader, skip, \
rename, prefixheader, suffixheader, sortheader
def test_setheader():
table1 = (('foo', 'bar'),
('a', 1),
('b', 2))
table2 = setheader(table1, ['foofoo', 'barbar'])
expect2 = (('foofoo', 'barbar'),
('a', 1),
('b', 2))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
def test_setheader_empty():
table1 = (('foo', 'bar'),)
table2 = setheader(table1, ['foofoo', 'barbar'])
expect2 = (('foofoo', 'barbar'),)
ieq(expect2, table2)
def test_extendheader():
table1 = (('foo',),
('a', 1, True),
('b', 2, False))
table2 = extendheader(table1, ['bar', 'baz'])
expect2 = (('foo', 'bar', 'baz'),
('a', 1, True),
('b', 2, False))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
def test_extendheader_empty():
table1 = (('foo',),)
table2 = extendheader(table1, ['bar', 'baz'])
expect2 = (('foo', 'bar', 'baz'),)
ieq(expect2, table2)
def test_pushheader():
table1 = (('a', 1),
('b', 2))
table2 = pushheader(table1, ['foo', 'bar'])
expect2 = (('foo', 'bar'),
('a', 1),
('b', 2))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
def test_pushheader_empty():
table1 = (('a', 1),)
table2 = pushheader(table1, ['foo', 'bar'])
expect2 = (('foo', 'bar'),
('a', 1))
ieq(expect2, table2)
table1 = tuple()
table2 = pushheader(table1, ['foo', 'bar'])
expect2 = (('foo', 'bar'),)
ieq(expect2, table2)
table1 = tuple()
table2 = pushheader(table1, 'foo', 'bar')
expect2 = (('foo', 'bar'),)
ieq(expect2, table2)
def test_pushheader_positional():
table1 = (('a', 1),
('b', 2))
# positional arguments instead of list
table2 = pushheader(table1, 'foo', 'bar')
expect2 = (('foo', 'bar'),
('a', 1),
('b', 2))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
# test with many fields
table1 = (('a', 1, 11, 111, 1111),
('b', 2, 22, 222, 2222))
# positional arguments instead of list
table2 = pushheader(table1, 'foo', 'bar', 'foo1', 'foo2', 'foo3')
expect2 = (('foo', 'bar', 'foo1', 'foo2', 'foo3'),
('a', 1, 11, 111, 1111),
('b', 2, 22, 222, 2222))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
# test with too few fields in header
table1 = (('a', 1, 11, 111, 1111),
('b', 2, 22, 222, 2222))
# positional arguments instead of list
table2 = pushheader(table1, 'foo', 'bar', 'foo1', 'foo2')
expect2 = (('foo', 'bar', 'foo1', 'foo2'),
('a', 1, 11, 111, 1111),
('b', 2, 22, 222, 2222))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
# test with too many fields in header
table1 = (('a', 1, 11, 111, 1111),
('b', 2, 22, 222, 2222))
# positional arguments instead of list
table2 = pushheader(table1, 'foo', 'bar', 'foo1', 'foo2', 'foo3', 'foo4')
expect2 = (('foo', 'bar', 'foo1', 'foo2', 'foo3', 'foo4'),
('a', 1, 11, 111, 1111),
('b', 2, 22, 222, 2222))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
def test_skip():
table1 = (('#aaa', 'bbb', 'ccc'),
('#mmm',),
('foo', 'bar'),
('a', 1),
('b', 2))
table2 = skip(table1, 2)
expect2 = (('foo', 'bar'),
('a', 1),
('b', 2))
ieq(expect2, table2)
ieq(expect2, table2) # can iterate twice?
def test_skip_empty():
table1 = (('#aaa', 'bbb', 'ccc'),
('#mmm',),
('foo', 'bar'))
table2 = skip(table1, 2)
expect2 = (('foo', 'bar'),)
ieq(expect2, table2)
def test_rename():
table = (('foo', 'bar'),
('M', 12),
('F', 34),
('-', 56))
result = rename(table, 'foo', 'foofoo')
assert fieldnames(result) == ('foofoo', 'bar')
result = rename(table, 0, 'foofoo')
assert fieldnames(result) == ('foofoo', 'bar')
result = rename(table, {'foo': 'foofoo', 'bar': 'barbar'})
assert fieldnames(result) == ('foofoo', 'barbar')
result = rename(table, {0: 'baz', 1: 'quux'})
assert fieldnames(result) == ('baz', 'quux')
result = rename(table)
result['foo'] = 'spong'
assert fieldnames(result) == ('spong', 'bar')
def test_rename_strict():
table = (('foo', 'bar'),
('M', 12),
('F', 34),
('-', 56))
result = rename(table, 'baz', 'quux')
try:
fieldnames(result)
except FieldSelectionError:
pass
else:
assert False, 'exception expected'
result = rename(table, 2, 'quux')
try:
fieldnames(result)
except FieldSelectionError:
pass
else:
assert False, 'exception expected'
result = rename(table, 'baz', 'quux', strict=False)
assert fieldnames(result) == ('foo', 'bar')
result = rename(table, 2, 'quux', strict=False)
assert fieldnames(result) == ('foo', 'bar')
def test_rename_empty():
table = (('foo', 'bar'),)
expect = (('foofoo', 'bar'),)
actual = rename(table, 'foo', 'foofoo')
ieq(expect, actual)
def test_prefixheader():
table1 = (('foo', 'bar'),
(1, 'A'),
(2, 'B'))
expect = (('pre_foo', 'pre_bar'),
(1, 'A'),
(2, 'B'))
actual = prefixheader(table1, 'pre_')
ieq(expect, actual)
ieq(expect, actual)
def test_suffixheader():
table1 = (('foo', 'bar'),
(1, 'A'),
(2, 'B'))
expect = (('foo_suf', 'bar_suf'),
(1, 'A'),
(2, 'B'))
actual = suffixheader(table1, '_suf')
ieq(expect, actual)
ieq(expect, actual)
def test_sortheaders():
table1 = (
('id', 'foo', 'bar', 'baz'),
('a', 1, 2, 3),
('b', 4, 5, 6))
expect = (
('bar', 'baz', 'foo', 'id'),
(2, 3, 1, 'a'),
(5, 6, 4, 'b'),
)
actual = sortheader(table1)
ieq(expect, actual)
def test_sortheaders_duplicate_headers():
""" Failing test case provided in sortheader()
with duplicate column names overlays values #392 """
table1 = (
('id', 'foo', 'foo', 'foo'),
('a', 1, 2, 3),
('b', 4, 5, 6))
expect = (
('foo', 'foo', 'foo', 'id'),
(1, 2, 3, 'a'),
(4, 5, 6, 'b'),
)
actual = sortheader(table1)
ieq(expect, actual)