forked from udacity/nd0821-c3-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_main.py
114 lines (91 loc) · 3.06 KB
/
test_main.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
"""
Unit test of main.py API module with pytest
author: Laurent veyssier
Date: Dec. 16th 2022
"""
from fastapi.testclient import TestClient
#from fastapi import HTTPException
import json
import logging
from main import app
client = TestClient(app)
def test_root():
"""
Test welcome message for get at root
"""
r = client.get("/")
assert r.status_code == 200
assert r.json() == "Welcome to our model API"
def test_inference():
"""
Test model inference output
"""
sample = { 'age':50,
'workclass':"Private",
'fnlgt':234721,
'education':"Doctorate",
'education_num':16,
'marital_status':"Separated",
'occupation':"Exec-managerial",
'relationship':"Not-in-family",
'race':"Black",
'sex':"Female",
'capital_gain':0,
'capital_loss':0,
'hours_per_week':50,
'native_country':"United-States"
}
data = json.dumps(sample)
r = client.post("/inference/", data=data )
# test response and output
assert r.status_code == 200
assert r.json()["age"] == 50
assert r.json()["fnlgt"] == 234721
# test prediction vs expected label
logging.info(f'********* prediction = {r.json()["prediction"]} ********')
assert r.json()["prediction"] == '>50K'
def test_inference_class0():
"""
Test model inference output for class 0
"""
sample = { 'age':30,
'workclass':"Private",
'fnlgt':234721,
'education':"HS-grad",
'education_num':1,
'marital_status':"Separated",
'occupation':"Handlers-cleaners",
'relationship':"Not-in-family",
'race':"Black",
'sex':"Male",
'capital_gain':0,
'capital_loss':0,
'hours_per_week':35,
'native_country':"United-States"
}
data = json.dumps(sample)
r = client.post("/inference/", data=data )
# test response and output
assert r.status_code == 200
assert r.json()["age"] == 30
assert r.json()["fnlgt"] == 234721
# test prediction vs expected label
logging.info(f'********* prediction = {r.json()["prediction"]} ********')
assert r.json()["prediction"][0] == '<=50K'
def test_wrong_inference_query():
"""
Test incomplete sample does not generate prediction
"""
sample = { 'age':50,
'workclass':"Private",
'fnlgt':234721,
}
data = json.dumps(sample)
r = client.post("/inference/", data=data )
assert 'prediction' not in r.json().keys()
logging.warning(f"The sample has {len(sample)} features. Must be 14 features")
if '__name__' == '__main__':
test_root()
test_inference()
test_inference_class0()
test_wrong_inference_query()