-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathtest_pipeline.py
More file actions
executable file
·100 lines (76 loc) · 2.38 KB
/
Copy pathtest_pipeline.py
File metadata and controls
executable file
·100 lines (76 loc) · 2.38 KB
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
import pytest
from sklearn_pandas.pipeline import TransformerPipeline, _call_fit
# In py3, mock is included with the unittest standard library
# In py2, it's a separate package
try:
from unittest.mock import patch
except ImportError:
from mock import patch
class NoTransformT(object):
"""Transformer without transform method.
"""
def fit(self, x):
return self
class NoFitT(object):
"""Transformer without fit method.
"""
def transform(self, x):
return self
class Trans(object):
"""
Transformer with fit and transform methods
"""
def fit(self, x, y=None):
return self
def transform(self, x):
return self
def func_x_y(x, y, kwarg='kwarg'):
"""
Function with required x and y arguments
"""
return
def func_x(x, kwarg='kwarg'):
"""
Function with required x argument
"""
return
def func_raise_type_err(x, y, kwarg='kwarg'):
"""
Function with required x and y arguments,
raises TypeError
"""
raise TypeError
def test_all_steps_fit_transform():
"""
All steps must implement fit and transform. Otherwise, raise TypeError.
"""
with pytest.raises(TypeError):
TransformerPipeline([('svc', NoTransformT())])
with pytest.raises(TypeError):
TransformerPipeline([('svc', NoFitT())])
@patch.object(Trans, 'fit', side_effect=func_x_y)
def test_called_with_x_and_y(mock_fit):
"""
Fit method with required X and y arguments is called with both and with
any additional keywords
"""
_call_fit(Trans().fit, 'X', 'y', kwarg='kwarg')
mock_fit.assert_called_with('X', 'y', kwarg='kwarg')
@patch.object(Trans, 'fit', side_effect=func_x)
def test_called_with_x(mock_fit):
"""
Fit method with a required X arguments is called with it and with
any additional keywords
"""
_call_fit(Trans().fit, 'X', 'y', kwarg='kwarg')
mock_fit.assert_called_with('X', kwarg='kwarg')
_call_fit(Trans().fit, 'X', kwarg='kwarg')
mock_fit.assert_called_with('X', kwarg='kwarg')
@patch.object(Trans, 'fit', side_effect=func_raise_type_err)
def test_raises_type_error(mock_fit):
"""
If a fit method with required X and y arguments raises a TypeError, it's
re-raised (for a different reason) when it's called with one argument
"""
with pytest.raises(TypeError):
_call_fit(Trans().fit, 'X', 'y', kwarg='kwarg')