| 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, eq_ |
| 7 |
from petl.compat import PY3, next |
| 8 |
from petl.util.base import header, fieldnames, data, dicts, records, \ |
| 9 |
namedtuples, itervalues, values, rowgroupby, expr |
| 10 |
|
| 11 |
|
| 12 |
def test_header(): |
| 13 |
table = (('foo', 'bar'), ('a', 1), ('b', 2)) |
| 14 |
actual = header(table) |
| 15 |
expect = ('foo', 'bar') |
| 16 |
eq_(expect, actual) |
| 17 |
table = (['foo', 'bar'], ['a', 1], ['b', 2]) |
| 18 |
actual = header(table) |
| 19 |
eq_(expect, actual) |
| 20 |
|
| 21 |
|
| 22 |
def test_fieldnames(): |
| 23 |
table = (('foo', 'bar'), ('a', 1), ('b', 2)) |
| 24 |
actual = fieldnames(table) |
| 25 |
expect = ('foo', 'bar') |
| 26 |
eq_(expect, actual) |
| 27 |
|
| 28 |
class CustomField(object): |
| 29 |
|
| 30 |
def __init__(self, key, description): |
| 31 |
self.key = key |
| 32 |
self.description = description |
| 33 |
|
| 34 |
def __str__(self): |
| 35 |
return self.key |
| 36 |
|
| 37 |
def __repr__(self): |
| 38 |
return 'CustomField(%r, %r)' % (self.key, self.description) |
| 39 |
|
| 40 |
table = ((CustomField('foo', 'Get some foo.'), |
| 41 |
CustomField('bar', 'A lot of bar.')), |
| 42 |
('a', 1), |
| 43 |
('b', 2)) |
| 44 |
actual = fieldnames(table) |
| 45 |
expect = ('foo', 'bar') |
| 46 |
eq_(expect, actual) |
| 47 |
|
| 48 |
|
| 49 |
def test_data(): |
| 50 |
table = (('foo', 'bar'), ('a', 1), ('b', 2)) |
| 51 |
actual = data(table) |
| 52 |
expect = (('a', 1), ('b', 2)) |
| 53 |
ieq(expect, actual) |
| 54 |
|
| 55 |
|
| 56 |
def test_data_headerless(): |
| 57 |
table = [] |
| 58 |
actual = data(table) |
| 59 |
expect = [] |
| 60 |
ieq(expect, actual) |
| 61 |
|
| 62 |
|
| 63 |
def test_dicts(): |
| 64 |
table = (('foo', 'bar'), ('a', 1), ('b', 2)) |
| 65 |
actual = dicts(table) |
| 66 |
expect = ({'foo': 'a', 'bar': 1}, {'foo': 'b', 'bar': 2}) |
| 67 |
ieq(expect, actual) |
| 68 |
|
| 69 |
|
| 70 |
def test_dicts_headerless(): |
| 71 |
table = [] |
| 72 |
actual = dicts(table) |
| 73 |
expect = [] |
| 74 |
ieq(expect, actual) |
| 75 |
|
| 76 |
|
| 77 |
def test_dicts_shortrows(): |
| 78 |
table = (('foo', 'bar'), ('a', 1), ('b',)) |
| 79 |
actual = dicts(table) |
| 80 |
expect = ({'foo': 'a', 'bar': 1}, {'foo': 'b', 'bar': None}) |
| 81 |
ieq(expect, actual) |
| 82 |
|
| 83 |
|
| 84 |
def test_records(): |
| 85 |
table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 3)) |
| 86 |
actual = records(table) |
| 87 |
# access items |
| 88 |
it = iter(actual) |
| 89 |
o = next(it) |
| 90 |
eq_('a', o['foo']) |
| 91 |
eq_(1, o['bar']) |
| 92 |
o = next(it) |
| 93 |
eq_('b', o['foo']) |
| 94 |
eq_(2, o['bar']) |
| 95 |
# access attributes |
| 96 |
it = iter(actual) |
| 97 |
o = next(it) |
| 98 |
eq_('a', o.foo) |
| 99 |
eq_(1, o.bar) |
| 100 |
o = next(it) |
| 101 |
eq_('b', o.foo) |
| 102 |
eq_(2, o.bar) |
| 103 |
# access with get() method |
| 104 |
it = iter(actual) |
| 105 |
o = next(it) |
| 106 |
eq_('a', o.get('foo')) |
| 107 |
eq_(1, o.get('bar')) |
| 108 |
eq_(None, o.get('baz')) |
| 109 |
eq_('qux', o.get('baz', default='qux')) |
| 110 |
|
| 111 |
|
| 112 |
def test_records_headerless(): |
| 113 |
table = [] |
| 114 |
actual = records(table) |
| 115 |
expect = [] |
| 116 |
ieq(expect, actual) |
| 117 |
|
| 118 |
|
| 119 |
def test_records_errors(): |
| 120 |
table = (('foo', 'bar'), ('a', 1), ('b', 2)) |
| 121 |
actual = records(table) |
| 122 |
# access items |
| 123 |
it = iter(actual) |
| 124 |
o = next(it) |
| 125 |
try: |
| 126 |
o['baz'] |
| 127 |
except KeyError: |
| 128 |
pass |
| 129 |
else: |
| 130 |
raise Exception('expected exception not raised') |
| 131 |
try: |
| 132 |
o.baz |
| 133 |
except AttributeError: |
| 134 |
pass |
| 135 |
else: |
| 136 |
raise Exception('expected exception not raised') |
| 137 |
|
| 138 |
|
| 139 |
def test_records_unevenrows(): |
| 140 |
table = (('foo', 'bar'), ('a', 1, True), ('b',)) |
| 141 |
actual = records(table) |
| 142 |
# access items |
| 143 |
it = iter(actual) |
| 144 |
o = next(it) |
| 145 |
eq_('a', o['foo']) |
| 146 |
eq_(1, o['bar']) |
| 147 |
o = next(it) |
| 148 |
eq_('b', o['foo']) |
| 149 |
eq_(None, o['bar']) |
| 150 |
# access attributes |
| 151 |
it = iter(actual) |
| 152 |
o = next(it) |
| 153 |
eq_('a', o.foo) |
| 154 |
eq_(1, o.bar) |
| 155 |
o = next(it) |
| 156 |
eq_('b', o.foo) |
| 157 |
eq_(None, o.bar) |
| 158 |
|
| 159 |
|
| 160 |
def test_namedtuples(): |
| 161 |
table = (('foo', 'bar'), ('a', 1), ('b', 2)) |
| 162 |
actual = namedtuples(table) |
| 163 |
it = iter(actual) |
| 164 |
o = next(it) |
| 165 |
eq_('a', o.foo) |
| 166 |
eq_(1, o.bar) |
| 167 |
o = next(it) |
| 168 |
eq_('b', o.foo) |
| 169 |
eq_(2, o.bar) |
| 170 |
|
| 171 |
|
| 172 |
def test_namedtuples_headerless(): |
| 173 |
table = [] |
| 174 |
actual = namedtuples(table) |
| 175 |
expect = [] |
| 176 |
ieq(expect, actual) |
| 177 |
|
| 178 |
|
| 179 |
def test_namedtuples_unevenrows(): |
| 180 |
table = (('foo', 'bar'), ('a', 1, True), ('b',)) |
| 181 |
actual = namedtuples(table) |
| 182 |
it = iter(actual) |
| 183 |
o = next(it) |
| 184 |
eq_('a', o.foo) |
| 185 |
eq_(1, o.bar) |
| 186 |
o = next(it) |
| 187 |
eq_('b', o.foo) |
| 188 |
eq_(None, o.bar) |
| 189 |
|
| 190 |
|
| 191 |
def test_itervalues(): |
| 192 |
|
| 193 |
table = (('foo', 'bar', 'baz'), |
| 194 |
('a', 1, True), |
| 195 |
('b', 2), |
| 196 |
('b', 7, False)) |
| 197 |
|
| 198 |
actual = itervalues(table, 'foo') |
| 199 |
expect = ('a', 'b', 'b') |
| 200 |
ieq(expect, actual) |
| 201 |
|
| 202 |
actual = itervalues(table, 'bar') |
| 203 |
expect = (1, 2, 7) |
| 204 |
ieq(expect, actual) |
| 205 |
|
| 206 |
actual = itervalues(table, ('foo', 'bar')) |
| 207 |
expect = (('a', 1), ('b', 2), ('b', 7)) |
| 208 |
ieq(expect, actual) |
| 209 |
|
| 210 |
actual = itervalues(table, 'baz') |
| 211 |
expect = (True, None, False) |
| 212 |
ieq(expect, actual) |
| 213 |
|
| 214 |
actual = itervalues(table, ('foo', 'baz')) |
| 215 |
expect = (('a', True), ('b', None), ('b', False)) |
| 216 |
ieq(expect, actual) |
| 217 |
|
| 218 |
|
| 219 |
def test_itervalues_headerless(): |
| 220 |
table = [] |
| 221 |
actual = itervalues(table, 'foo') |
| 222 |
with pytest.raises(FieldSelectionError): |
| 223 |
for i in actual: |
| 224 |
pass |
| 225 |
|
| 226 |
|
| 227 |
def test_values(): |
| 228 |
|
| 229 |
table = (('foo', 'bar', 'baz'), |
| 230 |
('a', 1, True), |
| 231 |
('b', 2), |
| 232 |
('b', 7, False)) |
| 233 |
|
| 234 |
actual = values(table, 'foo') |
| 235 |
expect = ('a', 'b', 'b') |
| 236 |
ieq(expect, actual) |
| 237 |
ieq(expect, actual) |
| 238 |
|
| 239 |
actual = values(table, 'bar') |
| 240 |
expect = (1, 2, 7) |
| 241 |
ieq(expect, actual) |
| 242 |
ieq(expect, actual) |
| 243 |
|
| 244 |
# old style signature for multiple fields, still supported |
| 245 |
actual = values(table, ('foo', 'bar')) |
| 246 |
expect = (('a', 1), ('b', 2), ('b', 7)) |
| 247 |
ieq(expect, actual) |
| 248 |
ieq(expect, actual) |
| 249 |
|
| 250 |
# as of 0.24 new style signature for multiple fields |
| 251 |
actual = values(table, 'foo', 'bar') |
| 252 |
expect = (('a', 1), ('b', 2), ('b', 7)) |
| 253 |
ieq(expect, actual) |
| 254 |
ieq(expect, actual) |
| 255 |
|
| 256 |
actual = values(table, 'baz') |
| 257 |
expect = (True, None, False) |
| 258 |
ieq(expect, actual) |
| 259 |
ieq(expect, actual) |
| 260 |
|
| 261 |
|
| 262 |
def test_values_headerless(): |
| 263 |
table = [] |
| 264 |
actual = values(table, 'foo') |
| 265 |
with pytest.raises(FieldSelectionError): |
| 266 |
for i in actual: |
| 267 |
pass |
| 268 |
|
| 269 |
|
| 270 |
def test_rowgroupby(): |
| 271 |
|
| 272 |
table = (('foo', 'bar', 'baz'), |
| 273 |
('a', 1, True), |
| 274 |
('b', 2, True), |
| 275 |
('b', 3)) |
| 276 |
|
| 277 |
# simplest form |
| 278 |
|
| 279 |
g = rowgroupby(table, 'foo') |
| 280 |
|
| 281 |
key, vals = next(g) |
| 282 |
vals = list(vals) |
| 283 |
eq_('a', key) |
| 284 |
eq_(1, len(vals)) |
| 285 |
eq_(('a', 1, True), vals[0]) |
| 286 |
|
| 287 |
key, vals = next(g) |
| 288 |
vals = list(vals) |
| 289 |
eq_('b', key) |
| 290 |
eq_(2, len(vals)) |
| 291 |
eq_(('b', 2, True), vals[0]) |
| 292 |
eq_(('b', 3), vals[1]) |
| 293 |
|
| 294 |
# specify value |
| 295 |
|
| 296 |
g = rowgroupby(table, 'foo', 'bar') |
| 297 |
|
| 298 |
key, vals = next(g) |
| 299 |
vals = list(vals) |
| 300 |
eq_('a', key) |
| 301 |
eq_(1, len(vals)) |
| 302 |
eq_(1, vals[0]) |
| 303 |
|
| 304 |
key, vals = next(g) |
| 305 |
vals = list(vals) |
| 306 |
eq_('b', key) |
| 307 |
eq_(2, len(vals)) |
| 308 |
eq_(2, vals[0]) |
| 309 |
eq_(3, vals[1]) |
| 310 |
|
| 311 |
# callable key |
| 312 |
|
| 313 |
g = rowgroupby(table, lambda r: r['foo'], lambda r: r['baz']) |
| 314 |
|
| 315 |
key, vals = next(g) |
| 316 |
vals = list(vals) |
| 317 |
eq_('a', key) |
| 318 |
eq_(1, len(vals)) |
| 319 |
eq_(True, vals[0]) |
| 320 |
|
| 321 |
key, vals = next(g) |
| 322 |
vals = list(vals) |
| 323 |
eq_('b', key) |
| 324 |
eq_(2, len(vals)) |
| 325 |
eq_(True, vals[0]) |
| 326 |
eq_(None, vals[1]) # gets padded |
| 327 |
|
| 328 |
|
| 329 |
def test_rowgroupby_headerless(): |
| 330 |
table = [] |
| 331 |
with pytest.raises(FieldSelectionError): |
| 332 |
rowgroupby(table, 'foo') |
| 333 |
|
| 334 |
|
| 335 |
def test_expr_ok(): |
| 336 |
fu = expr("{foo} * {bar}") |
| 337 |
res = fu({'foo': 3, 'bar': 2}) |
| 338 |
eq_(6, res) |
| 339 |
|
| 340 |
|
| 341 |
def test_expr_nok(): |
| 342 |
fu = expr("{foo2} * {bar2}", trusted=True) |
| 343 |
with pytest.raises(KeyError) as exc_info: |
| 344 |
fu({'foo': 3, 'bar': 2}) |
| 345 |
assert exc_info is not None |
| 346 |
|
| 347 |
|
| 348 |
def test_expr_inject(): |
| 349 |
with pytest.raises(Exception) as exc_info: |
| 350 |
# trick: using trusted=None will allow to skip using asteval |
| 351 |
fu = expr("__import__('os').system('ls')", trusted=None) |
| 352 |
fu({'foo': 3, 'bar': 2}) |
| 353 |
assert exc_info is not None |
| 354 |
|
| 355 |
|
| 356 |
def _has_asteval(): |
| 357 |
if PY3: |
| 358 |
try: |
| 359 |
import asteval |
| 360 |
return True |
| 361 |
except ImportError: |
| 362 |
pass |
| 363 |
return False |
| 364 |
|
| 365 |
|
| 366 |
def test_expr_untrusted(): |
| 367 |
if _has_asteval(): |
| 368 |
with pytest.raises((ValueError, TypeError)) as exc_info: |
| 369 |
_fu = expr("__import__('os').system('ls')", trusted=False) |
| 370 |
if _fu is not None: |
| 371 |
_fu({'foo': 3, 'bar': 2}) |
| 372 |
assert exc_info is not None |
| 373 |
|
| 374 |
|
| 375 |
def test_expr_ok_untrusted(): |
| 376 |
if _has_asteval(): |
| 377 |
fu = expr("{foo} * {bar}") |
| 378 |
res = fu({'foo': 3, 'bar': 2}) |
| 379 |
eq_(6, res) |
| 380 |
|
| 381 |
|
| 382 |
def test_expr_nok_untrusted(): |
| 383 |
if _has_asteval(): |
| 384 |
fu = expr("{foo2} * {bar2}", trusted=True) |
| 385 |
with pytest.raises(KeyError) as exc_info: |
| 386 |
fu({'foo': 3, 'bar': 2}) |
| 387 |
assert exc_info is not None |