-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_table_transformer.py
180 lines (153 loc) · 5.67 KB
/
test_table_transformer.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import itertools
import pytest
from safeds.data.tabular.containers import Table
from safeds.data.tabular.transformation import (
Discretizer,
LabelEncoder,
OneHotEncoder,
RangeScaler,
SimpleImputer,
StandardScaler,
TableTransformer,
)
def transformers_numeric() -> list[TableTransformer]:
"""
Return the list of numeric transformers to test.
After you implemented a new numeric transformer, add it to this list to ensure its `__hash__` method works as
expected. Place tests of methods that are specific to your transformer in a separate test file.
Returns
-------
classifiers : list[TableTransformer]
The list of numeric transformers to test.
"""
return [
StandardScaler(),
RangeScaler(),
Discretizer(),
]
def transformers_non_numeric() -> list[TableTransformer]:
"""
Return the list of non-numeric transformers to test.
After you implemented a new non-numeric transformer, add it to this list to ensure its `__hash__` method works as
expected. Place tests of methods that are specific to your transformer in a separate test file.
Returns
-------
classifiers : list[TableTransformer]
The list of non-numeric transformers to test.
"""
return [
OneHotEncoder(),
LabelEncoder(),
]
def transformers() -> list[TableTransformer]:
"""
Return the list of all transformers to test.
After you implemented a new transformer (which is either applicable to both numeric and non-numeric data or none), add it to one of the three lists to ensure its `__hash__` method works as
expected. Place tests of methods that are specific to your in a separate test file.
Returns
-------
classifiers : list[TableTransformer]
The list of all transformers to test.
"""
return (
transformers_numeric()
+ transformers_non_numeric()
+ [
SimpleImputer(strategy=SimpleImputer.Strategy.mode()),
]
)
@pytest.fixture()
def valid_data_numeric() -> Table:
return Table(
{
"col1": [0.0, 5.0, 10.0],
},
)
@pytest.fixture()
def valid_data_non_numeric() -> Table:
return Table(
{
"col1": ["a", "b", "c"],
},
)
@pytest.fixture()
def valid_data_imputer() -> Table:
return Table(
{
"col1": [1, 1, None],
},
)
class TestHash:
@pytest.mark.parametrize(
("transformer1", "transformer2"),
([(x, y) for x in transformers() for y in transformers() if x.__class__ == y.__class__]),
ids=lambda x: x.__class__.__name__,
)
def test_should_return_same_hash_for_equal_transformer(
self,
transformer1: TableTransformer,
transformer2: TableTransformer,
) -> None:
assert hash(transformer1) == hash(transformer2)
@pytest.mark.parametrize(
("transformer1", "transformer2"),
([(x, y) for x in transformers() for y in transformers() if x.__class__ != y.__class__]),
ids=lambda x: x.__class__.__name__,
)
def test_should_return_different_hash_for_unequal_transformer(
self,
transformer1: TableTransformer,
transformer2: TableTransformer,
) -> None:
assert hash(transformer1) != hash(transformer2)
@pytest.mark.parametrize("transformer1", transformers_numeric(), ids=lambda x: x.__class__.__name__)
def test_should_return_different_hash_for_same_numeric_transformer_fit(
self,
transformer1: TableTransformer,
valid_data_numeric: Table,
) -> None:
transformer1_fit = transformer1.fit(valid_data_numeric, ["col1"])
assert hash(transformer1) != hash(transformer1_fit)
@pytest.mark.parametrize("transformer1", transformers_non_numeric(), ids=lambda x: x.__class__.__name__)
def test_should_return_different_hash_for_same_non_numeric_transformer_fit(
self,
transformer1: TableTransformer,
valid_data_non_numeric: Table,
) -> None:
transformer1_fit = transformer1.fit(valid_data_non_numeric, ["col1"])
assert hash(transformer1) != hash(transformer1_fit)
@pytest.mark.parametrize(
("transformer1", "transformer2"),
(list(itertools.product(transformers_numeric(), transformers()))),
ids=lambda x: x.__class__.__name__,
)
def test_should_return_different_hash_for_numeric_transformer_fit(
self,
transformer1: TableTransformer,
transformer2: TableTransformer,
valid_data_numeric: Table,
) -> None:
transformer1_fit = transformer1.fit(valid_data_numeric, ["col1"])
assert hash(transformer2) != hash(transformer1_fit)
@pytest.mark.parametrize(
("transformer1", "transformer2"),
(list(itertools.product(transformers_non_numeric(), transformers()))),
ids=lambda x: x.__class__.__name__,
)
def test_should_return_different_hash_for_non_numeric_transformer_fit(
self,
transformer1: TableTransformer,
transformer2: TableTransformer,
valid_data_non_numeric: Table,
) -> None:
transformer1_fit = transformer1.fit(valid_data_non_numeric, ["col1"])
assert hash(transformer2) != hash(transformer1_fit)
@pytest.mark.parametrize("transformer2", transformers(), ids=lambda x: x.__class__.__name__)
def test_should_return_different_hash_for_imputer_fit(
self,
transformer2: TableTransformer,
valid_data_imputer: Table,
) -> None:
transformer1 = SimpleImputer(strategy=SimpleImputer.Strategy.mode())
transformer1_fit = transformer1.fit(valid_data_imputer, ["col1"])
assert hash(transformer2) != hash(transformer1_fit)