| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
|
| 4 |
from datetime import datetime |
| 5 |
|
| 6 |
|
| 7 |
from petl.test.helpers import ieq |
| 8 |
from petl.transform.setops import complement, intersection, diff, \ |
| 9 |
recordcomplement, recorddiff, hashcomplement, hashintersection |
| 10 |
|
| 11 |
|
| 12 |
def _test_complement_1(complement_impl): |
| 13 |
|
| 14 |
table1 = (('foo', 'bar'), |
| 15 |
('A', 1), |
| 16 |
('B', 2), |
| 17 |
('C', 7)) |
| 18 |
|
| 19 |
table2 = (('foo', 'bar'), |
| 20 |
('A', 9), |
| 21 |
('B', 2), |
| 22 |
('B', 3)) |
| 23 |
|
| 24 |
expectation = (('foo', 'bar'), |
| 25 |
('A', 1), |
| 26 |
('C', 7)) |
| 27 |
|
| 28 |
result = complement_impl(table1, table2) |
| 29 |
ieq(expectation, result) |
| 30 |
|
| 31 |
|
| 32 |
def _test_complement_2(complement_impl): |
| 33 |
|
| 34 |
tablea = (('foo', 'bar', 'baz'), |
| 35 |
('A', 1, True), |
| 36 |
('C', 7, False), |
| 37 |
('B', 2, False), |
| 38 |
('C', 9, True)) |
| 39 |
|
| 40 |
tableb = (('x', 'y', 'z'), |
| 41 |
('B', 2, False), |
| 42 |
('A', 9, False), |
| 43 |
('B', 3, True), |
| 44 |
('C', 9, True)) |
| 45 |
|
| 46 |
aminusb = (('foo', 'bar', 'baz'), |
| 47 |
('A', 1, True), |
| 48 |
('C', 7, False)) |
| 49 |
|
| 50 |
result = complement_impl(tablea, tableb) |
| 51 |
ieq(aminusb, result) |
| 52 |
|
| 53 |
bminusa = (('x', 'y', 'z'), |
| 54 |
('A', 9, False), |
| 55 |
('B', 3, True)) |
| 56 |
|
| 57 |
result = complement_impl(tableb, tablea) |
| 58 |
ieq(bminusa, result) |
| 59 |
|
| 60 |
|
| 61 |
def _test_complement_3(complement_impl): |
| 62 |
|
| 63 |
# make sure we deal with empty tables |
| 64 |
|
| 65 |
table1 = (('foo', 'bar'), |
| 66 |
('A', 1), |
| 67 |
('B', 2)) |
| 68 |
|
| 69 |
table2 = (('foo', 'bar'),) |
| 70 |
|
| 71 |
expectation = (('foo', 'bar'), |
| 72 |
('A', 1), |
| 73 |
('B', 2)) |
| 74 |
result = complement_impl(table1, table2) |
| 75 |
ieq(expectation, result) |
| 76 |
ieq(expectation, result) |
| 77 |
|
| 78 |
expectation = (('foo', 'bar'),) |
| 79 |
result = complement_impl(table2, table1) |
| 80 |
ieq(expectation, result) |
| 81 |
ieq(expectation, result) |
| 82 |
|
| 83 |
|
| 84 |
def _test_complement_4(complement_impl): |
| 85 |
|
| 86 |
# test behaviour with duplicate rows |
| 87 |
|
| 88 |
table1 = (('foo', 'bar'), |
| 89 |
('A', 1), |
| 90 |
('B', 2), |
| 91 |
('B', 2), |
| 92 |
('C', 7)) |
| 93 |
|
| 94 |
table2 = (('foo', 'bar'), |
| 95 |
('B', 2)) |
| 96 |
|
| 97 |
result = complement_impl(table1, table2) |
| 98 |
expectation = (('foo', 'bar'), |
| 99 |
('A', 1), |
| 100 |
('B', 2), |
| 101 |
('C', 7)) |
| 102 |
ieq(expectation, result) |
| 103 |
ieq(expectation, result) |
| 104 |
|
| 105 |
# strict behaviour |
| 106 |
result = complement_impl(table1, table2, strict=True) |
| 107 |
expectation = (('foo', 'bar'), |
| 108 |
('A', 1), |
| 109 |
('C', 7)) |
| 110 |
ieq(expectation, result) |
| 111 |
ieq(expectation, result) |
| 112 |
|
| 113 |
|
| 114 |
def _test_complement_none(complement_impl): |
| 115 |
# test behaviour with unsortable types |
| 116 |
now = datetime.now() |
| 117 |
|
| 118 |
ta = [['a', 'b'], [None, None]] |
| 119 |
tb = [['a', 'b'], [None, now]] |
| 120 |
|
| 121 |
expectation = (('a', 'b'), (None, None)) |
| 122 |
result = complement_impl(ta, tb) |
| 123 |
ieq(expectation, result) |
| 124 |
|
| 125 |
ta = [['a'], [now], [None]] |
| 126 |
tb = [['a'], [None], [None]] |
| 127 |
|
| 128 |
expectation = (('a',), (now,)) |
| 129 |
result = complement_impl(ta, tb) |
| 130 |
ieq(expectation, result) |
| 131 |
|
| 132 |
|
| 133 |
def _test_complement(f): |
| 134 |
_test_complement_1(f) |
| 135 |
_test_complement_2(f) |
| 136 |
_test_complement_3(f) |
| 137 |
_test_complement_4(f) |
| 138 |
_test_complement_none(f) |
| 139 |
|
| 140 |
|
| 141 |
def test_complement(): |
| 142 |
_test_complement(complement) |
| 143 |
|
| 144 |
|
| 145 |
def test_complement_seqtypes(): |
| 146 |
# test complement isn't confused by list vs tuple |
| 147 |
ta = [['a', 'b'], ['A', 1], ['B', 2]] |
| 148 |
tb = [('a', 'b'), ('A', 1), ('B', 2)] |
| 149 |
expectation = (('a', 'b'),) |
| 150 |
actual = complement(ta, tb, presorted=True) |
| 151 |
ieq(expectation, actual) |
| 152 |
|
| 153 |
|
| 154 |
def test_hashcomplement_seqtypes(): |
| 155 |
# test complement isn't confused by list vs tuple |
| 156 |
ta = [['a', 'b'], ['A', 1], ['B', 2]] |
| 157 |
tb = [('a', 'b'), ('A', 1), ('B', 2)] |
| 158 |
expectation = (('a', 'b'),) |
| 159 |
actual = hashcomplement(ta, tb) |
| 160 |
ieq(expectation, actual) |
| 161 |
|
| 162 |
|
| 163 |
def test_diff(): |
| 164 |
|
| 165 |
tablea = (('foo', 'bar', 'baz'), |
| 166 |
('A', 1, True), |
| 167 |
('C', 7, False), |
| 168 |
('B', 2, False), |
| 169 |
('C', 9, True)) |
| 170 |
|
| 171 |
tableb = (('x', 'y', 'z'), |
| 172 |
('B', 2, False), |
| 173 |
('A', 9, False), |
| 174 |
('B', 3, True), |
| 175 |
('C', 9, True)) |
| 176 |
|
| 177 |
aminusb = (('foo', 'bar', 'baz'), |
| 178 |
('A', 1, True), |
| 179 |
('C', 7, False)) |
| 180 |
|
| 181 |
bminusa = (('x', 'y', 'z'), |
| 182 |
('A', 9, False), |
| 183 |
('B', 3, True)) |
| 184 |
|
| 185 |
added, subtracted = diff(tablea, tableb) |
| 186 |
ieq(bminusa, added) |
| 187 |
ieq(aminusb, subtracted) |
| 188 |
|
| 189 |
|
| 190 |
def test_recordcomplement_1(): |
| 191 |
|
| 192 |
table1 = (('foo', 'bar'), |
| 193 |
('A', 1), |
| 194 |
('B', 2), |
| 195 |
('C', 7)) |
| 196 |
|
| 197 |
table2 = (('bar', 'foo'), |
| 198 |
(9, 'A'), |
| 199 |
(2, 'B'), |
| 200 |
(3, 'B')) |
| 201 |
|
| 202 |
expectation = (('foo', 'bar'), |
| 203 |
('A', 1), |
| 204 |
('C', 7)) |
| 205 |
|
| 206 |
result = recordcomplement(table1, table2) |
| 207 |
ieq(expectation, result) |
| 208 |
|
| 209 |
|
| 210 |
def test_recordcomplement_2(): |
| 211 |
|
| 212 |
tablea = (('foo', 'bar', 'baz'), |
| 213 |
('A', 1, True), |
| 214 |
('C', 7, False), |
| 215 |
('B', 2, False), |
| 216 |
('C', 9, True)) |
| 217 |
|
| 218 |
tableb = (('bar', 'foo', 'baz'), |
| 219 |
(2, 'B', False), |
| 220 |
(9, 'A', False), |
| 221 |
(3, 'B', True), |
| 222 |
(9, 'C', True)) |
| 223 |
|
| 224 |
aminusb = (('foo', 'bar', 'baz'), |
| 225 |
('A', 1, True), |
| 226 |
('C', 7, False)) |
| 227 |
|
| 228 |
result = recordcomplement(tablea, tableb) |
| 229 |
ieq(aminusb, result) |
| 230 |
|
| 231 |
bminusa = (('bar', 'foo', 'baz'), |
| 232 |
(3, 'B', True), |
| 233 |
(9, 'A', False)) |
| 234 |
|
| 235 |
result = recordcomplement(tableb, tablea) |
| 236 |
ieq(bminusa, result) |
| 237 |
|
| 238 |
|
| 239 |
def test_recordcomplement_3(): |
| 240 |
|
| 241 |
# make sure we deal with empty tables |
| 242 |
|
| 243 |
table1 = (('foo', 'bar'), |
| 244 |
('A', 1), |
| 245 |
('B', 2)) |
| 246 |
|
| 247 |
table2 = (('bar', 'foo'),) |
| 248 |
|
| 249 |
expectation = (('foo', 'bar'), |
| 250 |
('A', 1), |
| 251 |
('B', 2)) |
| 252 |
result = recordcomplement(table1, table2) |
| 253 |
ieq(expectation, result) |
| 254 |
ieq(expectation, result) |
| 255 |
|
| 256 |
expectation = (('bar', 'foo'),) |
| 257 |
result = recordcomplement(table2, table1) |
| 258 |
ieq(expectation, result) |
| 259 |
ieq(expectation, result) |
| 260 |
|
| 261 |
|
| 262 |
def test_recordcomplement_4(): |
| 263 |
|
| 264 |
# test behaviour with duplicate rows |
| 265 |
|
| 266 |
table1 = (('foo', 'bar'), |
| 267 |
('A', 1), |
| 268 |
('B', 2), |
| 269 |
('B', 2), |
| 270 |
('C', 7)) |
| 271 |
|
| 272 |
table2 = (('bar', 'foo'), |
| 273 |
(2, 'B')) |
| 274 |
|
| 275 |
result = recordcomplement(table1, table2) |
| 276 |
expectation = (('foo', 'bar'), |
| 277 |
('A', 1), |
| 278 |
('B', 2), |
| 279 |
('C', 7)) |
| 280 |
ieq(expectation, result) |
| 281 |
ieq(expectation, result) |
| 282 |
|
| 283 |
# strict behaviour |
| 284 |
result = recordcomplement(table1, table2, strict=True) |
| 285 |
expectation = (('foo', 'bar'), |
| 286 |
('A', 1), |
| 287 |
('C', 7)) |
| 288 |
ieq(expectation, result) |
| 289 |
ieq(expectation, result) |
| 290 |
|
| 291 |
|
| 292 |
def test_recorddiff(): |
| 293 |
|
| 294 |
tablea = (('foo', 'bar', 'baz'), |
| 295 |
('A', 1, True), |
| 296 |
('C', 7, False), |
| 297 |
('B', 2, False), |
| 298 |
('C', 9, True)) |
| 299 |
|
| 300 |
tableb = (('bar', 'foo', 'baz'), |
| 301 |
(2, 'B', False), |
| 302 |
(9, 'A', False), |
| 303 |
(3, 'B', True), |
| 304 |
(9, 'C', True)) |
| 305 |
|
| 306 |
aminusb = (('foo', 'bar', 'baz'), |
| 307 |
('A', 1, True), |
| 308 |
('C', 7, False)) |
| 309 |
|
| 310 |
bminusa = (('bar', 'foo', 'baz'), |
| 311 |
(3, 'B', True), |
| 312 |
(9, 'A', False)) |
| 313 |
|
| 314 |
added, subtracted = recorddiff(tablea, tableb) |
| 315 |
ieq(aminusb, subtracted) |
| 316 |
ieq(bminusa, added) |
| 317 |
|
| 318 |
|
| 319 |
def _test_intersection_1(intersection_impl): |
| 320 |
|
| 321 |
table1 = (('foo', 'bar'), |
| 322 |
('A', 1), |
| 323 |
('B', 2), |
| 324 |
('C', 7)) |
| 325 |
|
| 326 |
table2 = (('foo', 'bar'), |
| 327 |
('A', 9), |
| 328 |
('B', 2), |
| 329 |
('B', 3)) |
| 330 |
|
| 331 |
expectation = (('foo', 'bar'), |
| 332 |
('B', 2)) |
| 333 |
|
| 334 |
result = intersection_impl(table1, table2) |
| 335 |
ieq(expectation, result) |
| 336 |
|
| 337 |
|
| 338 |
def _test_intersection_2(intersection_impl): |
| 339 |
|
| 340 |
table1 = (('foo', 'bar', 'baz'), |
| 341 |
('A', 1, True), |
| 342 |
('C', 7, False), |
| 343 |
('B', 2, False), |
| 344 |
('C', 9, True)) |
| 345 |
|
| 346 |
table2 = (('x', 'y', 'z'), |
| 347 |
('B', 2, False), |
| 348 |
('A', 9, False), |
| 349 |
('B', 3, True), |
| 350 |
('C', 9, True)) |
| 351 |
|
| 352 |
expect = (('foo', 'bar', 'baz'), |
| 353 |
('B', 2, False), |
| 354 |
('C', 9, True)) |
| 355 |
|
| 356 |
table3 = intersection_impl(table1, table2) |
| 357 |
ieq(expect, table3) |
| 358 |
|
| 359 |
|
| 360 |
def _test_intersection_3(intersection_impl): |
| 361 |
|
| 362 |
# empty table |
| 363 |
table1 = (('foo', 'bar'), |
| 364 |
('A', 1), |
| 365 |
('B', 2), |
| 366 |
('C', 7)) |
| 367 |
|
| 368 |
table2 = (('foo', 'bar'),) |
| 369 |
|
| 370 |
expectation = (('foo', 'bar'),) |
| 371 |
result = intersection_impl(table1, table2) |
| 372 |
ieq(expectation, result) |
| 373 |
ieq(expectation, result) |
| 374 |
|
| 375 |
|
| 376 |
def _test_intersection_4(intersection_impl): |
| 377 |
|
| 378 |
# duplicate rows |
| 379 |
|
| 380 |
table1 = (('foo', 'bar'), |
| 381 |
('A', 1), |
| 382 |
('B', 2), |
| 383 |
('B', 2), |
| 384 |
('B', 2), |
| 385 |
('C', 7)) |
| 386 |
|
| 387 |
table2 = (('foo', 'bar'), |
| 388 |
('A', 9), |
| 389 |
('B', 2), |
| 390 |
('B', 2), |
| 391 |
('B', 3)) |
| 392 |
|
| 393 |
expectation = (('foo', 'bar'), |
| 394 |
('B', 2), |
| 395 |
('B', 2)) |
| 396 |
|
| 397 |
result = intersection_impl(table1, table2) |
| 398 |
ieq(expectation, result) |
| 399 |
ieq(expectation, result) |
| 400 |
|
| 401 |
|
| 402 |
def _test_intersection_empty(intersection_impl): |
| 403 |
|
| 404 |
table1 = (('foo', 'bar'), |
| 405 |
('A', 1), |
| 406 |
('B', 2), |
| 407 |
('C', 7)) |
| 408 |
table2 = (('foo', 'bar'),) |
| 409 |
expectation = (('foo', 'bar'),) |
| 410 |
result = intersection_impl(table1, table2) |
| 411 |
ieq(expectation, result) |
| 412 |
|
| 413 |
|
| 414 |
def _test_intersection(intersection_impl): |
| 415 |
_test_intersection_1(intersection_impl) |
| 416 |
_test_intersection_2(intersection_impl) |
| 417 |
_test_intersection_3(intersection_impl) |
| 418 |
_test_intersection_4(intersection_impl) |
| 419 |
_test_intersection_empty(intersection_impl) |
| 420 |
|
| 421 |
|
| 422 |
def test_intersection(): |
| 423 |
_test_intersection(intersection) |
| 424 |
|
| 425 |
|
| 426 |
def test_hashcomplement(): |
| 427 |
_test_complement(hashcomplement) |
| 428 |
|
| 429 |
|
| 430 |
def test_hashintersection(): |
| 431 |
_test_intersection(hashintersection) |