-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_model.py
66 lines (54 loc) · 1.46 KB
/
test_model.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
"""
File to test the model
"""
import pandas as pd
import pytest
from sklearn.ensemble import RandomForestClassifier
import joblib
from ml.data import process_data
from ml.clean_data import cleaned_data
from ml.model import train_model
cat_features = [
"workclass",
"education",
"marital-status",
"occupation",
"relationship",
"race",
"sex",
"native-country",
]
@pytest.fixture(name='data')
def data():
"""
This is a fixture for loading cleaned data and will be used by other tests.
Yields:
pd.Dataframe : Cleaned data
"""
yield cleaned_data()
def test_model():
"""
Test case to check if the correct model is loaded
"""
model = joblib.load("model/trained_model.joblib")
assert isinstance(model, RandomForestClassifier)
def test_data(data):
"""
Test case to check if the cleaned data is loaded properly
Args:
data (pd.Dataframe): Cleaned Data from the fixture
"""
assert data.shape[0] > 0 and data.shape[1] > 0
def test_ml_training(data):
"""
Test case to check after the cleaned data is loaded
model is trained propely or not.
Args:
data (pd.Dataframe): Cleaned Data from the fixture
"""
X_train, y_train, encoder, lb = process_data(
data, categorical_features=cat_features, label="salary", training=True)
model = train_model(X_train, y_train)
assert model is not None
assert encoder is not None
assert lb is not None