| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from petl.errors import FieldSelectionError |
| 6 |
from petl.test.helpers import ieq |
| 7 |
from petl.util import expr, empty, coalesce |
| 8 |
from petl.transform.basics import cut, cat, addfield, rowslice, head, tail, \ |
| 9 |
cutout, skipcomments, annex, addrownumbers, addcolumn, \ |
| 10 |
addfieldusingcontext, movefield, stack, addfields |
| 11 |
|
| 12 |
|
| 13 |
def test_cut(): |
| 14 |
|
| 15 |
table = (('foo', 'bar', 'baz'), |
| 16 |
('A', 1, 2), |
| 17 |
('B', '2', '3.4'), |
| 18 |
(u'B', u'3', u'7.8', True), |
| 19 |
('D', 'xyz', 9.0), |
| 20 |
('E', None)) |
| 21 |
|
| 22 |
cut1 = cut(table, 'foo') |
| 23 |
expectation = (('foo',), |
| 24 |
('A',), |
| 25 |
('B',), |
| 26 |
(u'B',), |
| 27 |
('D',), |
| 28 |
('E',)) |
| 29 |
ieq(expectation, cut1) |
| 30 |
|
| 31 |
cut2 = cut(table, 'foo', 'baz') |
| 32 |
expectation = (('foo', 'baz'), |
| 33 |
('A', 2), |
| 34 |
('B', '3.4'), |
| 35 |
(u'B', u'7.8'), |
| 36 |
('D', 9.0), |
| 37 |
('E', None)) |
| 38 |
ieq(expectation, cut2) |
| 39 |
|
| 40 |
cut3 = cut(table, 0, 2) |
| 41 |
expectation = (('foo', 'baz'), |
| 42 |
('A', 2), |
| 43 |
('B', '3.4'), |
| 44 |
(u'B', u'7.8'), |
| 45 |
('D', 9.0), |
| 46 |
('E', None)) |
| 47 |
ieq(expectation, cut3) |
| 48 |
|
| 49 |
cut4 = cut(table, 'bar', 0) |
| 50 |
expectation = (('bar', 'foo'), |
| 51 |
(1, 'A'), |
| 52 |
('2', 'B'), |
| 53 |
(u'3', u'B'), |
| 54 |
('xyz', 'D'), |
| 55 |
(None, 'E')) |
| 56 |
ieq(expectation, cut4) |
| 57 |
|
| 58 |
cut5 = cut(table, ('foo', 'baz')) |
| 59 |
expectation = (('foo', 'baz'), |
| 60 |
('A', 2), |
| 61 |
('B', '3.4'), |
| 62 |
(u'B', u'7.8'), |
| 63 |
('D', 9.0), |
| 64 |
('E', None)) |
| 65 |
ieq(expectation, cut5) |
| 66 |
|
| 67 |
|
| 68 |
def test_cut_empty(): |
| 69 |
table = (('foo', 'bar'),) |
| 70 |
expect = (('bar',),) |
| 71 |
actual = cut(table, 'bar') |
| 72 |
ieq(expect, actual) |
| 73 |
|
| 74 |
|
| 75 |
def test_cut_headerless(): |
| 76 |
table = () |
| 77 |
with pytest.raises(FieldSelectionError): |
| 78 |
for i in cut(table, 'bar'): |
| 79 |
pass |
| 80 |
|
| 81 |
|
| 82 |
def test_cutout(): |
| 83 |
|
| 84 |
table = (('foo', 'bar', 'baz'), |
| 85 |
('A', 1, 2), |
| 86 |
('B', '2', '3.4'), |
| 87 |
(u'B', u'3', u'7.8', True), |
| 88 |
('D', 'xyz', 9.0), |
| 89 |
('E', None)) |
| 90 |
|
| 91 |
cut1 = cutout(table, 'bar', 'baz') |
| 92 |
expectation = (('foo',), |
| 93 |
('A',), |
| 94 |
('B',), |
| 95 |
(u'B',), |
| 96 |
('D',), |
| 97 |
('E',)) |
| 98 |
ieq(expectation, cut1) |
| 99 |
|
| 100 |
cut2 = cutout(table, 'bar') |
| 101 |
expectation = (('foo', 'baz'), |
| 102 |
('A', 2), |
| 103 |
('B', '3.4'), |
| 104 |
(u'B', u'7.8'), |
| 105 |
('D', 9.0), |
| 106 |
('E', None)) |
| 107 |
ieq(expectation, cut2) |
| 108 |
|
| 109 |
cut3 = cutout(table, 1) |
| 110 |
expectation = (('foo', 'baz'), |
| 111 |
('A', 2), |
| 112 |
('B', '3.4'), |
| 113 |
(u'B', u'7.8'), |
| 114 |
('D', 9.0), |
| 115 |
('E', None)) |
| 116 |
ieq(expectation, cut3) |
| 117 |
|
| 118 |
|
| 119 |
def test_cutout_headerless(): |
| 120 |
table = () |
| 121 |
with pytest.raises(FieldSelectionError): |
| 122 |
for i in cutout(table, 'bar'): |
| 123 |
pass |
| 124 |
|
| 125 |
|
| 126 |
def test_cat(): |
| 127 |
|
| 128 |
table1 = (('foo', 'bar'), |
| 129 |
(1, 'A'), |
| 130 |
(2, 'B')) |
| 131 |
|
| 132 |
table2 = (('bar', 'baz'), |
| 133 |
('C', True), |
| 134 |
('D', False)) |
| 135 |
|
| 136 |
cat1 = cat(table1, table2, missing=None) |
| 137 |
expectation = (('foo', 'bar', 'baz'), |
| 138 |
(1, 'A', None), |
| 139 |
(2, 'B', None), |
| 140 |
(None, 'C', True), |
| 141 |
(None, 'D', False)) |
| 142 |
ieq(expectation, cat1) |
| 143 |
|
| 144 |
# how does cat cope with uneven rows? |
| 145 |
|
| 146 |
table3 = (('foo', 'bar', 'baz'), |
| 147 |
('A', 1, 2), |
| 148 |
('B', '2', '3.4'), |
| 149 |
(u'B', u'3', u'7.8', True), |
| 150 |
('D', 'xyz', 9.0), |
| 151 |
('E', None)) |
| 152 |
|
| 153 |
cat3 = cat(table3, missing=None) |
| 154 |
expectation = (('foo', 'bar', 'baz'), |
| 155 |
('A', 1, 2), |
| 156 |
('B', '2', '3.4'), |
| 157 |
(u'B', u'3', u'7.8'), |
| 158 |
('D', 'xyz', 9.0), |
| 159 |
('E', None, None)) |
| 160 |
ieq(expectation, cat3) |
| 161 |
|
| 162 |
# cat more than two tables? |
| 163 |
cat4 = cat(table1, table2, table3) |
| 164 |
expectation = (('foo', 'bar', 'baz'), |
| 165 |
(1, 'A', None), |
| 166 |
(2, 'B', None), |
| 167 |
(None, 'C', True), |
| 168 |
(None, 'D', False), |
| 169 |
('A', 1, 2), |
| 170 |
('B', '2', '3.4'), |
| 171 |
(u'B', u'3', u'7.8'), |
| 172 |
('D', 'xyz', 9.0), |
| 173 |
('E', None, None)) |
| 174 |
ieq(expectation, cat4) |
| 175 |
|
| 176 |
|
| 177 |
def test_cat_with_header(): |
| 178 |
|
| 179 |
table1 = (('bar', 'foo'), |
| 180 |
('A', 1), |
| 181 |
('B', 2)) |
| 182 |
|
| 183 |
table2 = (('bar', 'baz'), |
| 184 |
('C', True), |
| 185 |
('D', False)) |
| 186 |
|
| 187 |
actual = cat(table1, header=['A', 'foo', 'B', 'bar', 'C']) |
| 188 |
expect = (('A', 'foo', 'B', 'bar', 'C'), |
| 189 |
(None, 1, None, 'A', None), |
| 190 |
(None, 2, None, 'B', None)) |
| 191 |
ieq(expect, actual) |
| 192 |
ieq(expect, actual) |
| 193 |
|
| 194 |
actual = cat(table1, table2, header=['A', 'foo', 'B', 'bar', 'C']) |
| 195 |
expect = (('A', 'foo', 'B', 'bar', 'C'), |
| 196 |
(None, 1, None, 'A', None), |
| 197 |
(None, 2, None, 'B', None), |
| 198 |
(None, None, None, 'C', None), |
| 199 |
(None, None, None, 'D', None)) |
| 200 |
ieq(expect, actual) |
| 201 |
ieq(expect, actual) |
| 202 |
|
| 203 |
|
| 204 |
def test_cat_empty(): |
| 205 |
table1 = (('foo', 'bar'), |
| 206 |
(1, 'A'), |
| 207 |
(2, 'B')) |
| 208 |
table2 = (('bar', 'baz'),) |
| 209 |
expect = (('foo', 'bar', 'baz'), |
| 210 |
(1, 'A', None), |
| 211 |
(2, 'B', None)) |
| 212 |
actual = cat(table1, table2) |
| 213 |
ieq(expect, actual) |
| 214 |
|
| 215 |
|
| 216 |
def test_cat_headerless(): |
| 217 |
table1 = (('foo', 'bar'), |
| 218 |
(1, 'A'), |
| 219 |
(2, 'B')) |
| 220 |
table2 = () |
| 221 |
expect = table1 # basically does nothing |
| 222 |
actual = cat(table1, table2) |
| 223 |
ieq(expect, actual) |
| 224 |
|
| 225 |
|
| 226 |
def test_cat_dupfields(): |
| 227 |
table1 = (('foo', 'foo'), |
| 228 |
(1, 'A'), |
| 229 |
(2,), |
| 230 |
(3, 'B', True)) |
| 231 |
|
| 232 |
# these cases are pathological, including to confirm expected behaviour, |
| 233 |
# but user needs to rename fields to get something sensible |
| 234 |
|
| 235 |
actual = cat(table1) |
| 236 |
expect = (('foo', 'foo'), |
| 237 |
(1, 1), |
| 238 |
(2, 2), |
| 239 |
(3, 3)) |
| 240 |
ieq(expect, actual) |
| 241 |
|
| 242 |
table2 = (('foo', 'foo', 'bar'), |
| 243 |
(4, 'C', True), |
| 244 |
(5, 'D', False)) |
| 245 |
actual = cat(table1, table2) |
| 246 |
expect = (('foo', 'foo', 'bar'), |
| 247 |
(1, 1, None), |
| 248 |
(2, 2, None), |
| 249 |
(3, 3, None), |
| 250 |
(4, 4, True), |
| 251 |
(5, 5, False)) |
| 252 |
ieq(expect, actual) |
| 253 |
|
| 254 |
|
| 255 |
def test_stack_dupfields(): |
| 256 |
table1 = (('foo', 'foo'), |
| 257 |
(1, 'A'), |
| 258 |
(2,), |
| 259 |
(3, 'B', True)) |
| 260 |
|
| 261 |
actual = stack(table1) |
| 262 |
expect = (('foo', 'foo'), |
| 263 |
(1, 'A'), |
| 264 |
(2, None), |
| 265 |
(3, 'B')) |
| 266 |
ieq(expect, actual) |
| 267 |
|
| 268 |
table2 = (('foo', 'foo', 'bar'), |
| 269 |
(4, 'C', True), |
| 270 |
(5, 'D', False)) |
| 271 |
actual = stack(table1, table2) |
| 272 |
expect = (('foo', 'foo'), |
| 273 |
(1, 'A'), |
| 274 |
(2, None), |
| 275 |
(3, 'B'), |
| 276 |
(4, 'C'), |
| 277 |
(5, 'D')) |
| 278 |
ieq(expect, actual) |
| 279 |
|
| 280 |
|
| 281 |
def test_stack_headerless(): |
| 282 |
table1 = (('foo', 'bar'), |
| 283 |
(1, 'A'), |
| 284 |
(2, 'B')) |
| 285 |
table2 = () |
| 286 |
expect = table1 # basically does nothing |
| 287 |
actual = stack(table1, table2) |
| 288 |
ieq(expect, actual) |
| 289 |
|
| 290 |
|
| 291 |
def test_addfield(): |
| 292 |
table = (('foo', 'bar'), |
| 293 |
('M', 12), |
| 294 |
('F', 34), |
| 295 |
('-', 56)) |
| 296 |
|
| 297 |
result = addfield(table, 'baz', 42) |
| 298 |
expectation = (('foo', 'bar', 'baz'), |
| 299 |
('M', 12, 42), |
| 300 |
('F', 34, 42), |
| 301 |
('-', 56, 42)) |
| 302 |
ieq(expectation, result) |
| 303 |
ieq(expectation, result) |
| 304 |
|
| 305 |
result = addfield(table, 'baz', lambda row: '%s,%s' % (row.foo, row.bar)) |
| 306 |
expectation = (('foo', 'bar', 'baz'), |
| 307 |
('M', 12, 'M,12'), |
| 308 |
('F', 34, 'F,34'), |
| 309 |
('-', 56, '-,56')) |
| 310 |
ieq(expectation, result) |
| 311 |
ieq(expectation, result) |
| 312 |
|
| 313 |
result = addfield(table, 'baz', lambda rec: rec['bar'] * 2) |
| 314 |
expectation = (('foo', 'bar', 'baz'), |
| 315 |
('M', 12, 24), |
| 316 |
('F', 34, 68), |
| 317 |
('-', 56, 112)) |
| 318 |
ieq(expectation, result) |
| 319 |
ieq(expectation, result) |
| 320 |
|
| 321 |
result = addfield(table, 'baz', expr('{bar} * 2')) |
| 322 |
expectation = (('foo', 'bar', 'baz'), |
| 323 |
('M', 12, 24), |
| 324 |
('F', 34, 68), |
| 325 |
('-', 56, 112)) |
| 326 |
ieq(expectation, result) |
| 327 |
ieq(expectation, result) |
| 328 |
|
| 329 |
result = addfield(table, 'baz', 42, index=0) |
| 330 |
expectation = (('baz', 'foo', 'bar'), |
| 331 |
(42, 'M', 12), |
| 332 |
(42, 'F', 34), |
| 333 |
(42, '-', 56)) |
| 334 |
ieq(expectation, result) |
| 335 |
ieq(expectation, result) |
| 336 |
|
| 337 |
|
| 338 |
def test_addfield_empty(): |
| 339 |
table = (('foo', 'bar'),) |
| 340 |
expect = (('foo', 'bar', 'baz'),) |
| 341 |
actual = addfield(table, 'baz', 42) |
| 342 |
ieq(expect, actual) |
| 343 |
ieq(expect, actual) |
| 344 |
|
| 345 |
|
| 346 |
def test_addfield_headerless(): |
| 347 |
"""When adding a field to a headerless table, implicitly add a header.""" |
| 348 |
table = () |
| 349 |
expect = (('foo',),) |
| 350 |
actual = addfield(table, 'foo', 1) |
| 351 |
ieq(expect, actual) |
| 352 |
ieq(expect, actual) |
| 353 |
|
| 354 |
|
| 355 |
def test_addfield_coalesce(): |
| 356 |
table = (('foo', 'bar', 'baz', 'quux'), |
| 357 |
('M', 12, 23, 44), |
| 358 |
('F', None, 23, 11), |
| 359 |
('-', None, None, 42)) |
| 360 |
|
| 361 |
result = addfield(table, 'spong', coalesce('bar', 'baz', 'quux')) |
| 362 |
expect = (('foo', 'bar', 'baz', 'quux', 'spong'), |
| 363 |
('M', 12, 23, 44, 12), |
| 364 |
('F', None, 23, 11, 23), |
| 365 |
('-', None, None, 42, 42)) |
| 366 |
ieq(expect, result) |
| 367 |
ieq(expect, result) |
| 368 |
|
| 369 |
result = addfield(table, 'spong', coalesce(1, 2, 3)) |
| 370 |
expect = (('foo', 'bar', 'baz', 'quux', 'spong'), |
| 371 |
('M', 12, 23, 44, 12), |
| 372 |
('F', None, 23, 11, 23), |
| 373 |
('-', None, None, 42, 42)) |
| 374 |
ieq(expect, result) |
| 375 |
ieq(expect, result) |
| 376 |
|
| 377 |
|
| 378 |
def test_addfield_uneven_rows(): |
| 379 |
table = (('foo', 'bar'), |
| 380 |
('M',), |
| 381 |
('F', 34), |
| 382 |
('-', 56, 'spong')) |
| 383 |
result = addfield(table, 'baz', 42) |
| 384 |
expectation = (('foo', 'bar', 'baz'), |
| 385 |
('M', None, 42), |
| 386 |
('F', 34, 42), |
| 387 |
('-', 56, 42)) |
| 388 |
ieq(expectation, result) |
| 389 |
ieq(expectation, result) |
| 390 |
|
| 391 |
|
| 392 |
def test_addfield_dupfield(): |
| 393 |
table = (('foo', 'foo'), |
| 394 |
('M', 12), |
| 395 |
('F', 34), |
| 396 |
('-', 56)) |
| 397 |
|
| 398 |
result = addfield(table, 'bar', 42) |
| 399 |
expectation = (('foo', 'foo', 'bar'), |
| 400 |
('M', 12, 42), |
| 401 |
('F', 34, 42), |
| 402 |
('-', 56, 42)) |
| 403 |
ieq(expectation, result) |
| 404 |
ieq(expectation, result) |
| 405 |
|
| 406 |
|
| 407 |
def test_addfields(): |
| 408 |
table = (('foo', 'bar'), |
| 409 |
('M', 12), |
| 410 |
('F', 34), |
| 411 |
('-', 56)) |
| 412 |
|
| 413 |
result = addfields(table, [('baz', 42), |
| 414 |
('qux', lambda row: '%s,%s' % (row.foo, row.bar)), |
| 415 |
('fiz', lambda rec: rec['bar'] * 2, 0)]) |
| 416 |
expectation = (('fiz', 'foo', 'bar', 'baz', 'qux'), |
| 417 |
(24, 'M', 12, 42, 'M,12'), |
| 418 |
(68, 'F', 34, 42, 'F,34'), |
| 419 |
(112, '-', 56, 42, '-,56')) |
| 420 |
ieq(expectation, result) |
| 421 |
ieq(expectation, result) |
| 422 |
|
| 423 |
|
| 424 |
def test_addfields_uneven_rows(): |
| 425 |
table = (('foo', 'bar'), |
| 426 |
('M',), |
| 427 |
('F', 34), |
| 428 |
('-', 56, 'spong')) |
| 429 |
|
| 430 |
result = addfields(table, [('baz', 42), |
| 431 |
('qux', 100), |
| 432 |
('qux', 200)]) |
| 433 |
expectation = (('foo', 'bar', 'baz', 'qux', 'qux'), |
| 434 |
('M', None, 42, 100, 200), |
| 435 |
('F', 34, 42, 100, 200), |
| 436 |
('-', 56, 42, 100, 200)) |
| 437 |
ieq(expectation, result) |
| 438 |
ieq(expectation, result) |
| 439 |
|
| 440 |
result = addfields(table, [('baz', 42), |
| 441 |
('qux', 100, 0), |
| 442 |
('qux', 200, 0)]) |
| 443 |
expectation = (('qux', 'qux', 'foo', 'bar', 'baz'), |
| 444 |
(200, 100, 'M', None, 42), |
| 445 |
(200, 100, 'F', 34, 42), |
| 446 |
(200, 100, '-', 56, 42)) |
| 447 |
ieq(expectation, result) |
| 448 |
ieq(expectation, result) |
| 449 |
|
| 450 |
|
| 451 |
def test_rowslice(): |
| 452 |
|
| 453 |
table = (('foo', 'bar', 'baz'), |
| 454 |
('A', 1, 2), |
| 455 |
('B', '2', '3.4'), |
| 456 |
(u'B', u'3', u'7.8', True), |
| 457 |
('D', 'xyz', 9.0), |
| 458 |
('E', None)) |
| 459 |
|
| 460 |
result = rowslice(table, 2) |
| 461 |
expectation = (('foo', 'bar', 'baz'), |
| 462 |
('A', 1, 2), |
| 463 |
('B', '2', '3.4')) |
| 464 |
ieq(expectation, result) |
| 465 |
|
| 466 |
result = rowslice(table, 1, 2) |
| 467 |
expectation = (('foo', 'bar', 'baz'), |
| 468 |
('B', '2', '3.4')) |
| 469 |
ieq(expectation, result) |
| 470 |
|
| 471 |
result = rowslice(table, 1, 5, 2) |
| 472 |
expectation = (('foo', 'bar', 'baz'), |
| 473 |
('B', '2', '3.4'), |
| 474 |
('D', 'xyz', 9.0)) |
| 475 |
ieq(expectation, result) |
| 476 |
|
| 477 |
|
| 478 |
def test_rowslice_empty(): |
| 479 |
table = (('foo', 'bar'),) |
| 480 |
expect = (('foo', 'bar'),) |
| 481 |
actual = rowslice(table, 1, 2) |
| 482 |
ieq(expect, actual) |
| 483 |
|
| 484 |
|
| 485 |
def test_rowslice_headerless(): |
| 486 |
table = () |
| 487 |
expect = () |
| 488 |
actual = rowslice(table, 1, 2) |
| 489 |
ieq(expect, actual) |
| 490 |
|
| 491 |
|
| 492 |
def test_head(): |
| 493 |
|
| 494 |
table1 = (('foo', 'bar'), |
| 495 |
('a', 1), |
| 496 |
('b', 2), |
| 497 |
('c', 5), |
| 498 |
('d', 7), |
| 499 |
('f', 42), |
| 500 |
('f', 3), |
| 501 |
('h', 90), |
| 502 |
('k', 12), |
| 503 |
('l', 77), |
| 504 |
('q', 2)) |
| 505 |
|
| 506 |
table2 = head(table1, 4) |
| 507 |
expect = (('foo', 'bar'), |
| 508 |
('a', 1), |
| 509 |
('b', 2), |
| 510 |
('c', 5), |
| 511 |
('d', 7)) |
| 512 |
ieq(expect, table2) |
| 513 |
|
| 514 |
|
| 515 |
def test_head_raises_stop_iteration_for_empty_table(): |
| 516 |
table = iter(head([])) |
| 517 |
with pytest.raises(StopIteration): |
| 518 |
next(table) # header |
| 519 |
|
| 520 |
|
| 521 |
def test_head_raises_stop_iteration_for_header_only(): |
| 522 |
table1 = (('foo', 'bar', 'baz'),) |
| 523 |
table = iter(head(table1)) |
| 524 |
next(table) # header |
| 525 |
with pytest.raises(StopIteration): |
| 526 |
next(table) |
| 527 |
|
| 528 |
|
| 529 |
def test_tail(): |
| 530 |
|
| 531 |
table1 = (('foo', 'bar'), |
| 532 |
('a', 1), |
| 533 |
('b', 2), |
| 534 |
('c', 5), |
| 535 |
('d', 7), |
| 536 |
('f', 42), |
| 537 |
('f', 3), |
| 538 |
('h', 90), |
| 539 |
('k', 12), |
| 540 |
('l', 77), |
| 541 |
('q', 2)) |
| 542 |
|
| 543 |
table2 = tail(table1, 4) |
| 544 |
expect = (('foo', 'bar'), |
| 545 |
('h', 90), |
| 546 |
('k', 12), |
| 547 |
('l', 77), |
| 548 |
('q', 2)) |
| 549 |
ieq(expect, table2) |
| 550 |
|
| 551 |
|
| 552 |
def test_tail_empty(): |
| 553 |
table = (('foo', 'bar'),) |
| 554 |
expect = (('foo', 'bar'),) |
| 555 |
actual = tail(table) |
| 556 |
ieq(expect, actual) |
| 557 |
|
| 558 |
|
| 559 |
def test_tail_headerless(): |
| 560 |
table = () |
| 561 |
expect = () |
| 562 |
actual = tail(table) |
| 563 |
ieq(expect, actual) |
| 564 |
|
| 565 |
|
| 566 |
def test_skipcomments(): |
| 567 |
|
| 568 |
table1 = (('##aaa', 'bbb', 'ccc'), |
| 569 |
('##mmm',), |
| 570 |
('#foo', 'bar'), |
| 571 |
('##nnn', 1), |
| 572 |
('a', 1), |
| 573 |
('b', 2)) |
| 574 |
table2 = skipcomments(table1, '##') |
| 575 |
expect2 = (('#foo', 'bar'), |
| 576 |
('a', 1), |
| 577 |
('b', 2)) |
| 578 |
ieq(expect2, table2) |
| 579 |
ieq(expect2, table2) # can iterate twice? |
| 580 |
|
| 581 |
|
| 582 |
def test_skipcomments_empty(): |
| 583 |
|
| 584 |
table1 = (('##aaa', 'bbb', 'ccc'), |
| 585 |
('##mmm',), |
| 586 |
('#foo', 'bar'), |
| 587 |
('##nnn', 1)) |
| 588 |
table2 = skipcomments(table1, '##') |
| 589 |
expect2 = (('#foo', 'bar'),) |
| 590 |
ieq(expect2, table2) |
| 591 |
|
| 592 |
|
| 593 |
def test_annex(): |
| 594 |
|
| 595 |
table1 = (('foo', 'bar'), |
| 596 |
('A', 9), |
| 597 |
('C', 2), |
| 598 |
('F', 1)) |
| 599 |
table2 = (('foo', 'baz'), |
| 600 |
('B', 3), |
| 601 |
('D', 10)) |
| 602 |
expect = (('foo', 'bar', 'foo', 'baz'), |
| 603 |
('A', 9, 'B', 3), |
| 604 |
('C', 2, 'D', 10), |
| 605 |
('F', 1, None, None)) |
| 606 |
actual = annex(table1, table2) |
| 607 |
ieq(expect, actual) |
| 608 |
ieq(expect, actual) |
| 609 |
|
| 610 |
expect21 = (('foo', 'baz', 'foo', 'bar'), |
| 611 |
('B', 3, 'A', 9), |
| 612 |
('D', 10, 'C', 2), |
| 613 |
(None, None, 'F', 1)) |
| 614 |
actual21 = annex(table2, table1) |
| 615 |
ieq(expect21, actual21) |
| 616 |
ieq(expect21, actual21) |
| 617 |
|
| 618 |
|
| 619 |
def test_annex_uneven_rows(): |
| 620 |
|
| 621 |
table1 = (('foo', 'bar'), |
| 622 |
('A', 9, True), |
| 623 |
('C', 2), |
| 624 |
('F',)) |
| 625 |
table2 = (('foo', 'baz'), |
| 626 |
('B', 3), |
| 627 |
('D', 10)) |
| 628 |
expect = (('foo', 'bar', 'foo', 'baz'), |
| 629 |
('A', 9, 'B', 3), |
| 630 |
('C', 2, 'D', 10), |
| 631 |
('F', None, None, None)) |
| 632 |
actual = annex(table1, table2) |
| 633 |
ieq(expect, actual) |
| 634 |
ieq(expect, actual) |
| 635 |
|
| 636 |
|
| 637 |
def test_annex_headerless(): |
| 638 |
table1 = (('foo', 'bar'), |
| 639 |
('C', 2)) |
| 640 |
table2 = () # does nothing |
| 641 |
expect = table1 |
| 642 |
actual = annex(table1, table2) |
| 643 |
ieq(expect, actual) |
| 644 |
ieq(expect, actual) |
| 645 |
|
| 646 |
|
| 647 |
def test_addrownumbers(): |
| 648 |
|
| 649 |
table1 = (('foo', 'bar'), |
| 650 |
('A', 9), |
| 651 |
('C', 2), |
| 652 |
('F', 1)) |
| 653 |
|
| 654 |
expect = (('row', 'foo', 'bar'), |
| 655 |
(1, 'A', 9), |
| 656 |
(2, 'C', 2), |
| 657 |
(3, 'F', 1)) |
| 658 |
actual = addrownumbers(table1) |
| 659 |
ieq(expect, actual) |
| 660 |
ieq(expect, actual) |
| 661 |
|
| 662 |
|
| 663 |
def test_addrownumbers_field_name(): |
| 664 |
|
| 665 |
table1 = (('foo', 'bar'), |
| 666 |
('A', 9), |
| 667 |
('C', 2)) |
| 668 |
|
| 669 |
expect = (('id', 'foo', 'bar'), |
| 670 |
(1, 'A', 9), |
| 671 |
(2, 'C', 2)) |
| 672 |
actual = addrownumbers(table1, field='id') |
| 673 |
ieq(expect, actual) |
| 674 |
ieq(expect, actual) |
| 675 |
|
| 676 |
|
| 677 |
def test_addrownumbers_headerless(): |
| 678 |
"""Adds a column row if there is none.""" |
| 679 |
table = () |
| 680 |
expect = (('id',),) |
| 681 |
actual = addrownumbers(table, field='id') |
| 682 |
ieq(expect, actual) |
| 683 |
ieq(expect, actual) |
| 684 |
|
| 685 |
|
| 686 |
def test_addcolumn(): |
| 687 |
|
| 688 |
table1 = (('foo', 'bar'), |
| 689 |
('A', 1), |
| 690 |
('B', 2)) |
| 691 |
|
| 692 |
col = [True, False] |
| 693 |
|
| 694 |
expect2 = (('foo', 'bar', 'baz'), |
| 695 |
('A', 1, True), |
| 696 |
('B', 2, False)) |
| 697 |
table2 = addcolumn(table1, 'baz', col) |
| 698 |
ieq(expect2, table2) |
| 699 |
ieq(expect2, table2) |
| 700 |
|
| 701 |
# test short column |
| 702 |
table3 = (('foo', 'bar'), |
| 703 |
('A', 1), |
| 704 |
('B', 2), |
| 705 |
('C', 2)) |
| 706 |
expect4 = (('foo', 'bar', 'baz'), |
| 707 |
('A', 1, True), |
| 708 |
('B', 2, False), |
| 709 |
('C', 2, None)) |
| 710 |
table4 = addcolumn(table3, 'baz', col) |
| 711 |
ieq(expect4, table4) |
| 712 |
|
| 713 |
# test short table |
| 714 |
col = [True, False, False] |
| 715 |
expect5 = (('foo', 'bar', 'baz'), |
| 716 |
('A', 1, True), |
| 717 |
('B', 2, False), |
| 718 |
(None, None, False)) |
| 719 |
table5 = addcolumn(table1, 'baz', col) |
| 720 |
ieq(expect5, table5) |
| 721 |
|
| 722 |
|
| 723 |
def test_empty_addcolumn(): |
| 724 |
|
| 725 |
table1 = empty() |
| 726 |
table2 = addcolumn(table1, 'foo', ['A', 'B']) |
| 727 |
table3 = addcolumn(table2, 'bar', [1, 2]) |
| 728 |
expect = (('foo', 'bar'), |
| 729 |
('A', 1), |
| 730 |
('B', 2)) |
| 731 |
ieq(expect, table3) |
| 732 |
ieq(expect, table3) |
| 733 |
|
| 734 |
|
| 735 |
def test_addcolumn_headerless(): |
| 736 |
"""Adds a header row if none exists.""" |
| 737 |
table1 = () |
| 738 |
expect = (('foo',), |
| 739 |
('A',), |
| 740 |
('B',)) |
| 741 |
actual = addcolumn(table1, 'foo', ['A', 'B']) |
| 742 |
ieq(expect, actual) |
| 743 |
ieq(expect, actual) |
| 744 |
|
| 745 |
|
| 746 |
def test_addfieldusingcontext(): |
| 747 |
|
| 748 |
table1 = (('foo', 'bar'), |
| 749 |
('A', 1), |
| 750 |
('B', 4), |
| 751 |
('C', 5), |
| 752 |
('D', 9)) |
| 753 |
|
| 754 |
expect = (('foo', 'bar', 'baz', 'quux'), |
| 755 |
('A', 1, None, 3), |
| 756 |
('B', 4, 3, 1), |
| 757 |
('C', 5, 1, 4), |
| 758 |
('D', 9, 4, None)) |
| 759 |
|
| 760 |
def upstream(prv, cur, nxt): |
| 761 |
if prv is None: |
| 762 |
return None |
| 763 |
else: |
| 764 |
return cur.bar - prv.bar |
| 765 |
|
| 766 |
def downstream(prv, cur, nxt): |
| 767 |
if nxt is None: |
| 768 |
return None |
| 769 |
else: |
| 770 |
return nxt.bar - cur.bar |
| 771 |
|
| 772 |
table2 = addfieldusingcontext(table1, 'baz', upstream) |
| 773 |
table3 = addfieldusingcontext(table2, 'quux', downstream) |
| 774 |
ieq(expect, table3) |
| 775 |
ieq(expect, table3) |
| 776 |
|
| 777 |
def test_addfieldusingcontext_stateful(): |
| 778 |
|
| 779 |
table1 = (('foo', 'bar'), |
| 780 |
('A', 1), |
| 781 |
('B', 4), |
| 782 |
('C', 5), |
| 783 |
('D', 9)) |
| 784 |
|
| 785 |
expect = (('foo', 'bar', 'baz', 'quux'), |
| 786 |
('A', 1, 1, 5), |
| 787 |
('B', 4, 5, 10), |
| 788 |
('C', 5, 10, 19), |
| 789 |
('D', 9, 19, 19)) |
| 790 |
|
| 791 |
def upstream(prv, cur, nxt): |
| 792 |
if prv is None: |
| 793 |
return cur.bar |
| 794 |
else: |
| 795 |
return cur.bar + prv.baz |
| 796 |
|
| 797 |
def downstream(prv, cur, nxt): |
| 798 |
if nxt is None: |
| 799 |
return prv.quux |
| 800 |
elif prv is None: |
| 801 |
return nxt.bar + cur.bar |
| 802 |
else: |
| 803 |
return nxt.bar + prv.quux |
| 804 |
|
| 805 |
table2 = addfieldusingcontext(table1, 'baz', upstream) |
| 806 |
table3 = addfieldusingcontext(table2, 'quux', downstream) |
| 807 |
ieq(expect, table3) |
| 808 |
ieq(expect, table3) |
| 809 |
|
| 810 |
|
| 811 |
def test_addfieldusingcontext_empty(): |
| 812 |
table = empty() |
| 813 |
expect = (('foo',),) |
| 814 |
|
| 815 |
def query(prv, cur, nxt): |
| 816 |
return 0 |
| 817 |
|
| 818 |
actual = addfieldusingcontext(table, 'foo', query) |
| 819 |
ieq(expect, actual) |
| 820 |
ieq(expect, actual) |
| 821 |
|
| 822 |
|
| 823 |
def test_addfieldusingcontext_headerless(): |
| 824 |
table = () |
| 825 |
expect = (('foo',),) |
| 826 |
|
| 827 |
def query(prv, cur, nxt): |
| 828 |
return 0 |
| 829 |
|
| 830 |
actual = addfieldusingcontext(table, 'foo', query) |
| 831 |
ieq(expect, actual) |
| 832 |
ieq(expect, actual) |
| 833 |
|
| 834 |
|
| 835 |
def test_movefield(): |
| 836 |
|
| 837 |
table1 = (('foo', 'bar', 'baz'), |
| 838 |
(1, 'A', True), |
| 839 |
(2, 'B', False)) |
| 840 |
|
| 841 |
expect = (('bar', 'foo', 'baz'), |
| 842 |
('A', 1, True), |
| 843 |
('B', 2, False)) |
| 844 |
|
| 845 |
actual = movefield(table1, 'bar', 0) |
| 846 |
ieq(expect, actual) |
| 847 |
ieq(expect, actual) |
| 848 |
|
| 849 |
actual = movefield(table1, 'foo', 1) |
| 850 |
ieq(expect, actual) |
| 851 |
ieq(expect, actual) |