Debian Codegraph @

petl/test/transform/test_headers.py

raw
1 from __future__ import absolute_import, print_function, division
2
3 import pytest
4
5 from petl.test.helpers import ieq
6 from petl.errors import FieldSelectionError
7 from petl.util import fieldnames
8 from petl.transform.headers import setheader, extendheader, pushheader, skip, \
9 rename, prefixheader, suffixheader, sortheader
10
11
12 def test_setheader():
13
14 table1 = (('foo', 'bar'),
15 ('a', 1),
16 ('b', 2))
17 table2 = setheader(table1, ['foofoo', 'barbar'])
18 expect2 = (('foofoo', 'barbar'),
19 ('a', 1),
20 ('b', 2))
21 ieq(expect2, table2)
22 ieq(expect2, table2) # can iterate twice?
23
24
25 def test_setheader_empty():
26
27 table1 = (('foo', 'bar'),)
28 table2 = setheader(table1, ['foofoo', 'barbar'])
29 expect2 = (('foofoo', 'barbar'),)
30 ieq(expect2, table2)
31
32
33 def test_setheader_headerless():
34 table = []
35 actual = setheader(table, ['foo', 'bar'])
36 expect = [('foo', 'bar')]
37 ieq(expect, actual)
38
39
40 def test_extendheader():
41
42 table1 = (('foo',),
43 ('a', 1, True),
44 ('b', 2, False))
45 table2 = extendheader(table1, ['bar', 'baz'])
46 expect2 = (('foo', 'bar', 'baz'),
47 ('a', 1, True),
48 ('b', 2, False))
49 ieq(expect2, table2)
50 ieq(expect2, table2) # can iterate twice?
51
52
53 def test_extendheader_empty():
54
55 table1 = (('foo',),)
56 table2 = extendheader(table1, ['bar', 'baz'])
57 expect2 = (('foo', 'bar', 'baz'),)
58 ieq(expect2, table2)
59
60
61 def test_extendheader_headerless():
62 table = []
63 actual = extendheader(table, ['foo', 'bar'])
64 expect = [('foo', 'bar')]
65 ieq(expect, actual)
66 ieq(expect, actual)
67
68
69 def test_pushheader():
70
71 table1 = (('a', 1),
72 ('b', 2))
73 table2 = pushheader(table1, ['foo', 'bar'])
74 expect2 = (('foo', 'bar'),
75 ('a', 1),
76 ('b', 2))
77 ieq(expect2, table2)
78 ieq(expect2, table2) # can iterate twice?
79
80
81 def test_pushheader_empty():
82
83 table1 = (('a', 1),)
84 table2 = pushheader(table1, ['foo', 'bar'])
85 expect2 = (('foo', 'bar'),
86 ('a', 1))
87 ieq(expect2, table2)
88
89 table1 = tuple()
90 table2 = pushheader(table1, ['foo', 'bar'])
91 expect2 = (('foo', 'bar'),)
92 ieq(expect2, table2)
93
94 table1 = tuple()
95 table2 = pushheader(table1, 'foo', 'bar')
96 expect2 = (('foo', 'bar'),)
97 ieq(expect2, table2)
98
99
100 def test_pushheader_headerless():
101 table = []
102 actual = pushheader(table, ['foo', 'bar'])
103 expect = [('foo', 'bar')]
104 ieq(expect, actual)
105 ieq(expect, actual)
106
107
108 def test_pushheader_positional():
109
110 table1 = (('a', 1),
111 ('b', 2))
112 # positional arguments instead of list
113 table2 = pushheader(table1, 'foo', 'bar')
114 expect2 = (('foo', 'bar'),
115 ('a', 1),
116 ('b', 2))
117 ieq(expect2, table2)
118 ieq(expect2, table2) # can iterate twice?
119
120 # test with many fields
121 table1 = (('a', 1, 11, 111, 1111),
122 ('b', 2, 22, 222, 2222))
123 # positional arguments instead of list
124 table2 = pushheader(table1, 'foo', 'bar', 'foo1', 'foo2', 'foo3')
125 expect2 = (('foo', 'bar', 'foo1', 'foo2', 'foo3'),
126 ('a', 1, 11, 111, 1111),
127 ('b', 2, 22, 222, 2222))
128 ieq(expect2, table2)
129 ieq(expect2, table2) # can iterate twice?
130
131 # test with too few fields in header
132 table1 = (('a', 1, 11, 111, 1111),
133 ('b', 2, 22, 222, 2222))
134 # positional arguments instead of list
135 table2 = pushheader(table1, 'foo', 'bar', 'foo1', 'foo2')
136 expect2 = (('foo', 'bar', 'foo1', 'foo2'),
137 ('a', 1, 11, 111, 1111),
138 ('b', 2, 22, 222, 2222))
139 ieq(expect2, table2)
140 ieq(expect2, table2) # can iterate twice?
141
142 # test with too many fields in header
143 table1 = (('a', 1, 11, 111, 1111),
144 ('b', 2, 22, 222, 2222))
145 # positional arguments instead of list
146 table2 = pushheader(table1, 'foo', 'bar', 'foo1', 'foo2', 'foo3', 'foo4')
147 expect2 = (('foo', 'bar', 'foo1', 'foo2', 'foo3', 'foo4'),
148 ('a', 1, 11, 111, 1111),
149 ('b', 2, 22, 222, 2222))
150 ieq(expect2, table2)
151 ieq(expect2, table2) # can iterate twice?
152
153
154 def test_skip():
155
156 table1 = (('#aaa', 'bbb', 'ccc'),
157 ('#mmm',),
158 ('foo', 'bar'),
159 ('a', 1),
160 ('b', 2))
161 table2 = skip(table1, 2)
162 expect2 = (('foo', 'bar'),
163 ('a', 1),
164 ('b', 2))
165 ieq(expect2, table2)
166 ieq(expect2, table2) # can iterate twice?
167
168
169 def test_skip_empty():
170
171 table1 = (('#aaa', 'bbb', 'ccc'),
172 ('#mmm',),
173 ('foo', 'bar'))
174 table2 = skip(table1, 2)
175 expect2 = (('foo', 'bar'),)
176 ieq(expect2, table2)
177
178
179 def test_skip_headerless():
180 table = []
181 actual = skip(table, 2)
182 expect = []
183 ieq(expect, actual)
184
185
186 def test_rename():
187
188 table = (('foo', 'bar'),
189 ('M', 12),
190 ('F', 34),
191 ('-', 56))
192
193 result = rename(table, 'foo', 'foofoo')
194 assert fieldnames(result) == ('foofoo', 'bar')
195
196 result = rename(table, 0, 'foofoo')
197 assert fieldnames(result) == ('foofoo', 'bar')
198
199 result = rename(table, {'foo': 'foofoo', 'bar': 'barbar'})
200 assert fieldnames(result) == ('foofoo', 'barbar')
201
202 result = rename(table, {0: 'baz', 1: 'quux'})
203 assert fieldnames(result) == ('baz', 'quux')
204
205 result = rename(table)
206 result['foo'] = 'spong'
207 assert fieldnames(result) == ('spong', 'bar')
208
209
210 def test_rename_strict():
211
212 table = (('foo', 'bar'),
213 ('M', 12),
214 ('F', 34),
215 ('-', 56))
216
217 result = rename(table, 'baz', 'quux')
218 try:
219 fieldnames(result)
220 except FieldSelectionError:
221 pass
222 else:
223 assert False, 'exception expected'
224
225 result = rename(table, 2, 'quux')
226 try:
227 fieldnames(result)
228 except FieldSelectionError:
229 pass
230 else:
231 assert False, 'exception expected'
232
233 result = rename(table, 'baz', 'quux', strict=False)
234 assert fieldnames(result) == ('foo', 'bar')
235 result = rename(table, 2, 'quux', strict=False)
236 assert fieldnames(result) == ('foo', 'bar')
237
238
239 def test_rename_empty():
240 table = (('foo', 'bar'),)
241 expect = (('foofoo', 'bar'),)
242 actual = rename(table, 'foo', 'foofoo')
243 ieq(expect, actual)
244
245
246 def test_rename_headerless():
247 table = []
248 with pytest.raises(FieldSelectionError):
249 for i in rename(table, 'foo', 'foofoo'):
250 pass
251
252
253 def test_prefixheader():
254
255 table1 = (('foo', 'bar'),
256 (1, 'A'),
257 (2, 'B'))
258
259 expect = (('pre_foo', 'pre_bar'),
260 (1, 'A'),
261 (2, 'B'))
262
263 actual = prefixheader(table1, 'pre_')
264 ieq(expect, actual)
265 ieq(expect, actual)
266
267
268 def test_prefixheader_headerless():
269 table = []
270 actual = prefixheader(table, 'pre_')
271 expect = []
272 ieq(expect, actual)
273
274
275 def test_suffixheader():
276
277 table1 = (('foo', 'bar'),
278 (1, 'A'),
279 (2, 'B'))
280
281 expect = (('foo_suf', 'bar_suf'),
282 (1, 'A'),
283 (2, 'B'))
284
285 actual = suffixheader(table1, '_suf')
286 ieq(expect, actual)
287 ieq(expect, actual)
288
289
290 def test_suffixheader_headerless():
291 table = []
292 actual = suffixheader(table, '_suf')
293 expect = []
294 ieq(expect, actual)
295
296
297 def test_sortheaders():
298 table1 = (
299 ('id', 'foo', 'bar', 'baz'),
300 ('a', 1, 2, 3),
301 ('b', 4, 5, 6))
302
303 expect = (
304 ('bar', 'baz', 'foo', 'id'),
305 (2, 3, 1, 'a'),
306 (5, 6, 4, 'b'),
307 )
308
309 actual = sortheader(table1)
310 ieq(expect, actual)
311
312
313 def test_sortheaders_duplicate_headers():
314 """ Failing test case provided in sortheader()
315 with duplicate column names overlays values #392 """
316 table1 = (
317 ('id', 'foo', 'foo', 'foo'),
318 ('a', 1, 2, 3),
319 ('b', 4, 5, 6))
320
321 expect = (
322 ('foo', 'foo', 'foo', 'id'),
323 (1, 2, 3, 'a'),
324 (4, 5, 6, 'b'),
325 )
326
327 actual = sortheader(table1)
328 ieq(expect, actual)
329
330
331 def test_sortheader_headerless():
332 table = []
333 actual = sortheader(table)
334 expect = []
335 ieq(expect, actual)