Debian Codegraph @

petl/test/failonerror.py

raw
1 import pytest
2
3 from petl.test.helpers import ieq, eq_
4 import petl.config as config
5
6
7
8 def assert_failonerror(input_fn, expected_output):
9 """In the input rows, the first row should process through the
10 transformation cleanly. The second row should generate an
11 exception. There are no requirements for any other rows."""
12 #=========================================================
13 # Test function parameters with default config settings
14 #=========================================================
15 # test the default config setting: failonerror == False
16 eq_(config.failonerror, False)
17
18 # By default, a bad conversion does not raise an exception, and
19 # values for the failed conversion are returned as None
20 table2 = input_fn()
21 ieq(expected_output, table2)
22 ieq(expected_output, table2)
23
24 # When called with failonerror is False or None, a bad conversion
25 # does not raise an exception, and values for the failed conversion
26 # are returned as None
27 table3 = input_fn(failonerror=False)
28 ieq(expected_output, table3)
29 ieq(expected_output, table3)
30 table3 = input_fn(failonerror=None)
31 ieq(expected_output, table3)
32 ieq(expected_output, table3)
33
34 # When called with failonerror=True, a bad conversion raises an
35 # exception
36 with pytest.raises(Exception):
37 table4 = input_fn(failonerror=True)
38 table4.nrows()
39
40 # When called with failonerror='inline', a bad conversion
41 # does not raise an exception, and an Exception for the failed
42 # conversion is returned in the result.
43 expect5 = expected_output[0], expected_output[1]
44 table5 = input_fn(failonerror='inline')
45 ieq(expect5, table5.head(1))
46 ieq(expect5, table5.head(1))
47 excp = table5[2][0]
48 assert isinstance(excp, Exception)
49
50 #=========================================================
51 # Test config settings
52 #=========================================================
53 # Save config setting
54 saved_config_failonerror = config.failonerror
55
56 # When config.failonerror == True, a bad conversion raises an
57 # exception
58 config.failonerror = True
59 with pytest.raises(Exception):
60 table6 = input_fn()
61 table6.nrows()
62
63 # When config.failonerror == 'inline', a bad conversion
64 # does not raise an exception, and an Exception for the failed
65 # conversion is returned in the result.
66 expect7 = expected_output[0], expected_output[1]
67 config.failonerror = 'inline'
68 table7 = input_fn()
69 ieq(expect7, table7.head(1))
70 ieq(expect7, table7.head(1))
71 excp = table7[2][0]
72 assert isinstance(excp, Exception)
73
74 # When config.failonerror is an invalid value, but still truthy, it
75 # behaves the same as if == True
76 config.failonerror = 'invalid'
77 with pytest.raises(Exception):
78 table8 = input_fn()
79 table8.nrows()
80
81 # When config.failonerror is None, it behaves the same as if
82 # config.failonerror is False
83 config.failonerror = None
84 table9 = input_fn()
85 ieq(expected_output, table9)
86 ieq(expected_output, table9)
87
88 # A False keyword parameter overrides config.failonerror == True
89 config.failonerror = True
90 table10 = input_fn(failonerror=False)
91 ieq(expected_output, table10)
92 ieq(expected_output, table10)
93
94 # A None keyword parameter uses config.failonerror == True
95 config.failonerror = True
96 with pytest.raises(Exception):
97 table11 = input_fn(failonerror=None)
98 table11.nrows()
99
100 # restore config setting
101 config.failonerror = saved_config_failonerror
102