-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_preprocessing.py
More file actions
178 lines (128 loc) · 5.64 KB
/
Copy pathtest_preprocessing.py
File metadata and controls
178 lines (128 loc) · 5.64 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
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
import unittest
from unittest.mock import patch
import copy
from dende_preprocessing import Preprocessing # ajuste conforme necessário
class TestPreprocessingWith10x5Dataset(unittest.TestCase):
def setUp(self):
self.dataset = {
"age": [20, 25, 30, None, 40, 35, None, 28, 50, 60],
"salary": [1000, 2000, None, 4000, 5000, None, 7000, 8000, 9000, 10000],
"score": [10, 20, 30, 40, None, 60, 70, None, 90, 100],
"city": ["SP", "RJ", "MG", "SP", None, "BA", "BA", "MG", "RJ", "SP"],
"department": ["IT", "HR", "IT", None, "Finance", "IT", "HR", "Finance", "IT", None]
}
# ==========================================
# TESTE DE VALIDAÇÃO DO SHAPE
# ==========================================
def test_invalid_dataset_shape(self):
invalid_dataset = {
"a": [1, 2, 3],
"b": [1, 2]
}
with self.assertRaises(ValueError):
Preprocessing(invalid_dataset)
# ==========================================
# TESTE ISNA
# ==========================================
def test_isna(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
result = prep.isna()
self.assertTrue(len(result["age"]) > 0)
self.assertTrue(len(result["age"]) < 10)
# ==========================================
# TESTE FILLNA
# ==========================================
def test_fillna(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
result = prep.fillna(value=0)
for col in result:
self.assertNotIn(None, result[col])
# ==========================================
# TESTE DROPNA
# ==========================================
def test_dropna(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
result = prep.dropna()
# Todas as linhas retornadas não podem conter None
for col in result:
self.assertNotIn(None, result[col])
# ==========================================
# TESTE MINMAX SCALER
# ==========================================
def test_minmax_scaler(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
prep.fillna(value=0)
result = prep.scale(columns={"age", "salary", "score"}, method="minMax")
for col in ["age", "salary", "score"]:
self.assertEqual(min(result[col]), 0)
self.assertEqual(max(result[col]), 1)
# ==========================================
# TESTE STANDARD SCALER
# ==========================================
def test_standard_scaler(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
prep.fillna(columns={"age"}, value=0)
result = prep.scale(columns={"age"}, method="standard")
mean = sum(result["age"]) / len(result["age"])
self.assertAlmostEqual(mean, 0, places=6)
# ==========================================
# TESTE LABEL ENCODER
# ==========================================
def test_label_encoding(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
prep.fillna(value="Unknown")
result = prep.encode(columns={"city"}, method="label")
for value in result["city"]:
self.assertIsInstance(value, int)
# ==========================================
# TESTE ONEHOT ENCODER
# ==========================================
def test_onehot_encoding(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
prep.fillna(value="Unknown")
result = prep.encode(columns={"department"}, method="oneHot")
self.assertNotIn("department", result)
onehot_columns = [col for col in result.keys() if col.startswith("department_")]
self.assertTrue(len(onehot_columns) > 0)
# ==========================================
# TESTE MÉTODO INVÁLIDO
# ==========================================
def test_invalid_scaler_method(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
with self.assertRaises(ValueError):
prep.scale(method="invalid")
def test_invalid_encoder_method(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
with self.assertRaises(ValueError):
prep.encode(columns={"city"}, method="invalid")
# ==========================================
# TESTE DE INTEGRIDADE ESTRUTURAL
# ==========================================
def test_dataset_integrity_after_operations(self):
prep = Preprocessing(copy.deepcopy(self.dataset))
prep.fillna(columns={"age", "salary", "score"}, value=0)
prep.fillna(columns={"city", "department"}, value="Desconhecido")
prep.scale(columns={"age", "salary"}, method="minMax")
prep.encode(columns={"city"}, method="label")
dataset = prep.dataset
row_count = len(next(iter(dataset.values())))
for col in dataset:
self.assertEqual(len(dataset[col]), row_count)
# ==========================================
# MOCK EXEMPLO (caso queira isolar Statistics)
# ==========================================
class TestWithMockedStatistics(unittest.TestCase):
@patch("dende_preprocessing.Statistics")
def test_preprocessing_with_mocked_statistics(self, mock_stats):
dataset = {
"a": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"b": [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
"c": [1]*10,
"d": [2]*10,
"e": [3]*10
}
prep = Preprocessing(dataset)
self.assertIsNotNone(prep.statistics)
mock_stats.assert_called_once()
if __name__ == "__main__":
unittest.main()