-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_api.py
58 lines (43 loc) · 1.61 KB
/
test_api.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
# test_api.py
import json
import pytest
from api import app # Import the Flask application
from unittest.mock import patch
@pytest.fixture
def client():
with app.test_client() as client:
yield client
def test_predict_valid_input(client):
# Valid input data
data = [
{"Age": 30, "Sex": "male", "Embarked": "S"},
{"Age": 25, "Sex": "female", "Embarked": "C"},
]
response = client.post("/predict", json=data)
assert response.status_code == 200
assert "prediction" in response.json
def test_predict_missing_field(client):
# Missing field in input data
data = [{"Age": 30, "Sex": "male"}, {"Age": 25, "Embarked": "C"}]
response = client.post("/predict", json=data)
assert response.status_code == 200 # Expecting a successful response
def test_predict_empty_input(client):
response = client.post("/predict", json=[])
assert response.status_code != 200
assert "trace" in response.json
def test_predict_no_model(client):
with patch("api.lr", None): # Adjust the path as necessary
data = [
{"Age": 85, "Sex": "male", "Embarked": "S"},
{"Age": 24, "Sex": "female", "Embarked": "C"},
]
response = client.post("/predict", json=data)
assert response.status_code != 200 # Expecting an error status code
def test_predict_success(client):
valid_data = [
{"Age": 30, "Sex": "male", "Embarked": "S"},
{"Age": 24, "Sex": "female", "Embarked": "C"},
]
response = client.post("/predict", json=valid_data)
assert response.status_code == 200
assert "prediction" in response.json