| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
import logging |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import petl as etl |
| 10 |
from petl.transform.validation import validate |
| 11 |
from petl.test.helpers import ieq |
| 12 |
from petl.errors import FieldSelectionError |
| 13 |
|
| 14 |
|
| 15 |
logger = logging.getLogger(__name__) |
| 16 |
debug = logger.debug |
| 17 |
|
| 18 |
|
| 19 |
def test_constraints(): |
| 20 |
|
| 21 |
constraints = [ |
| 22 |
dict(name='C1', field='foo', test=int), |
| 23 |
dict(name='C2', field='bar', test=etl.dateparser('%Y-%m-%d')), |
| 24 |
dict(name='C3', field='baz', assertion=lambda v: v in ['Y', 'N']), |
| 25 |
dict(name='C4', assertion=lambda row: None not in row) |
| 26 |
] |
| 27 |
|
| 28 |
table = (('foo', 'bar', 'baz'), |
| 29 |
(1, '2000-01-01', 'Y'), |
| 30 |
('x', '2010-10-10', 'N'), |
| 31 |
(2, '2000/01/01', 'Y'), |
| 32 |
(3, '2015-12-12', 'x'), |
| 33 |
(4, None, 'N'), |
| 34 |
('y', '1999-99-99', 'z')) |
| 35 |
|
| 36 |
expect = (('name', 'row', 'field', 'value', 'error'), |
| 37 |
('C1', 2, 'foo', 'x', 'ValueError'), |
| 38 |
('C2', 3, 'bar', '2000/01/01', 'ValueError'), |
| 39 |
('C3', 4, 'baz', 'x', 'AssertionError'), |
| 40 |
('C2', 5, 'bar', None, 'AttributeError'), |
| 41 |
('C4', 5, None, None, 'AssertionError'), |
| 42 |
('C1', 6, 'foo', 'y', 'ValueError'), |
| 43 |
('C2', 6, 'bar', '1999-99-99', 'ValueError'), |
| 44 |
('C3', 6, 'baz', 'z', 'AssertionError')) |
| 45 |
|
| 46 |
actual = validate(table, constraints) |
| 47 |
debug(actual) |
| 48 |
|
| 49 |
ieq(expect, actual) |
| 50 |
ieq(expect, actual) |
| 51 |
|
| 52 |
|
| 53 |
def test_non_optional_constraint_with_missing_field(): |
| 54 |
constraints = [ |
| 55 |
dict(name='C1', field='foo', test=int), |
| 56 |
] |
| 57 |
|
| 58 |
table = (('bar', 'baz'), |
| 59 |
('1999-99-99', 'z')) |
| 60 |
|
| 61 |
actual = validate(table, constraints) |
| 62 |
with pytest.raises(FieldSelectionError): |
| 63 |
debug(actual) |
| 64 |
|
| 65 |
|
| 66 |
def test_optional_constraint_with_missing_field(): |
| 67 |
constraints = [ |
| 68 |
dict(name='C1', field='foo', test=int, optional=True), |
| 69 |
] |
| 70 |
|
| 71 |
table = (('bar', 'baz'), |
| 72 |
('1999-99-99', 'z')) |
| 73 |
|
| 74 |
expect = (('name', 'row', 'field', 'value', 'error'),) |
| 75 |
|
| 76 |
actual = validate(table, constraints) |
| 77 |
debug(actual) |
| 78 |
|
| 79 |
ieq(expect, actual) |
| 80 |
|
| 81 |
|
| 82 |
def test_row_length(): |
| 83 |
|
| 84 |
table = (('foo', 'bar', 'baz'), |
| 85 |
(1, '2000-01-01', 'Y'), |
| 86 |
('x', '2010-10-10'), |
| 87 |
(2, '2000/01/01', 'Y', True)) |
| 88 |
|
| 89 |
expect = (('name', 'row', 'field', 'value', 'error'), |
| 90 |
('__len__', 2, None, 2, 'AssertionError'), |
| 91 |
('__len__', 3, None, 4, 'AssertionError')) |
| 92 |
|
| 93 |
actual = validate(table) |
| 94 |
debug(actual) |
| 95 |
|
| 96 |
ieq(expect, actual) |
| 97 |
ieq(expect, actual) |
| 98 |
|
| 99 |
|
| 100 |
def test_header(): |
| 101 |
|
| 102 |
header = ('foo', 'bar', 'baz') |
| 103 |
|
| 104 |
table = (('foo', 'bar', 'bazzz'), |
| 105 |
(1, '2000-01-01', 'Y'), |
| 106 |
('x', '2010-10-10', 'N')) |
| 107 |
|
| 108 |
expect = (('name', 'row', 'field', 'value', 'error'), |
| 109 |
('__header__', 0, None, None, 'AssertionError')) |
| 110 |
|
| 111 |
actual = validate(table, header=header) |
| 112 |
debug(actual) |
| 113 |
|
| 114 |
ieq(expect, actual) |
| 115 |
ieq(expect, actual) |
| 116 |
|
| 117 |
header = ('foo', 'bar', 'baz', 'quux') |
| 118 |
|
| 119 |
table = (('foo', 'bar', 'baz'), |
| 120 |
(1, '2000-01-01', 'Y'), |
| 121 |
('x', '2010-10-10', 'N')) |
| 122 |
|
| 123 |
expect = (('name', 'row', 'field', 'value', 'error'), |
| 124 |
('__header__', 0, None, None, 'AssertionError'), |
| 125 |
('__len__', 1, None, 3, 'AssertionError'), |
| 126 |
('__len__', 2, None, 3, 'AssertionError')) |
| 127 |
|
| 128 |
actual = validate(table, header=header) |
| 129 |
debug(actual) |
| 130 |
|
| 131 |
ieq(expect, actual) |
| 132 |
ieq(expect, actual) |
| 133 |
|
| 134 |
|
| 135 |
def test_validation_headerless(): |
| 136 |
header = ('foo', 'bar', 'baz') |
| 137 |
table = [] |
| 138 |
# Expect only a missing header - no exceptions please |
| 139 |
expect = (('name', 'row', 'field', 'value', 'error'), |
| 140 |
('__header__', 0, None, None, 'AssertionError')) |
| 141 |
actual = validate(table, header=header) |
| 142 |
ieq(expect, actual) |
| 143 |
ieq(expect, actual) |