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