forked from udacity/nd0821-c3-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
56 lines (51 loc) · 1.66 KB
/
test_api.py
File metadata and controls
56 lines (51 loc) · 1.66 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
from fastapi.testclient import TestClient
from starter.main import app
import json
client = TestClient(app)
def test_greeting():
""" Test the greeting at the root """
r = client.get("/")
assert r.status_code == 200
assert json.loads(r.text) == {"Greeting": "Welcome to the API!"}
def test_inference_zero():
""" Test the inference post function for a prediction of zero """
data_zero = {
"age": 20,
"workclass": "Private",
"fnlgt": 215646,
"education": "HS-grad",
"education-num": 9,
"marital-status": "Never-married",
"occupation": "Handlers-cleaners",
"relationship": "Not-in-family",
"race": "Black",
"sex": "Male",
"capital-gain": 0,
"capital-loss": 0,
"hours-per-week": 20,
"native-country": "United-States"
}
r = client.post("/inference", json=data_zero)
assert r.status_code == 200
assert json.loads(r.text) == {"prediction": "Salary <= 50K"}
def test_inference_one():
""" Test the inference post function for a prediction of one """
data_one = {
"age": 41,
"workclass": "Private",
"fnlgt": 284582,
"education": "Masters",
"education-num": 14,
"marital-status": "Married-civ-spouse",
"occupation": "Exec-managerial",
"relationship": "Wife",
"race": "White",
"sex": "Female",
"capital-gain": 0,
"capital-loss": 0,
"hours-per-week": 50,
"native-country": "United-States"
}
r = client.post("/inference", json=data_one)
assert r.status_code == 200
assert json.loads(r.text) == {"prediction": "Salary > 50K"}