forked from PGCSEDS-IIITH/devsecops-iris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
134 lines (119 loc) · 4.92 KB
/
test_app.py
File metadata and controls
134 lines (119 loc) · 4.92 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
from fastapi.testclient import TestClient
from main import app
# test to check the correct functioning of the /ping route
def test_ping():
with TestClient(app) as client:
response = client.get("/ping")
# asserting the correct response is received
assert response.status_code == 200
assert response.json() == {"ping": "pong"}
# test to check if Iris Virginica is classified correctly
def test_pred_virginica():
# defining a sample payload for the testcase
payload = {
"sepal_length": 3,
"sepal_width": 5,
"petal_length": 3.2,
"petal_width": 4.4,
}
with TestClient(app) as client:
response = client.post("/predict_flower", json=payload)
# asserting the correct response is received
assert response.status_code == 200
assert response.json() == {"flower_class": "Iris Virginica"}
# test to check if Iris Virginica is classified correctly
def test_pred_virginica_diff():
# defining a sample payload for the testcase
payload = {
"sepal_length": 3.1,
"sepal_width": 5.7,
"petal_length": 6,
"petal_width": 6,
}
with TestClient(app) as client:
response = client.post("/predict_flower", json=payload)
# asserting the correct response is received
assert response.status_code == 200
assert response.json() == {"flower_class": "Iris Virginica"}
# test to check if invalid param in payload is passed
def test_pred_invalid_payload():
# defining a sample payload for the testcase
payload = {
"sepal_length": 2,
"sepal_wid": 3.5,
"petal_length": 1.4,
"petal_width": 0.2,
}
with TestClient(app) as client:
response = client.post("/predict_flower", json=payload)
# asserting the correct response is received
assert response.status_code == 422
assert response.json() == {'detail': [{'loc': ['body', 'sepal_width'], 'msg': 'field required', 'type': 'value_error.missing'}]}
# test to check if invalid param in payload is passed
def test_pred_empty_payload():
# defining a sample payload for the testcase
payload = {
}
with TestClient(app) as client:
response = client.post("/predict_flower", json=payload)
# asserting the correct response is received
assert response.status_code == 422
assert response.json() == {'detail': [{'loc': ['body', 'sepal_length'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'sepal_width'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'petal_length'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'petal_width'], 'msg': 'field required', 'type': 'value_error.missing'}]}
# test to check if Iris Setosa is classified correctly
def test_pred_setosa():
# defining a sample payload for the testcase
payload = {
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2,
}
with TestClient(app) as client:
response = client.post("/predict_flower", json=payload)
# asserting the correct response is received
assert response.status_code == 200
assert response.json()["flower_class"] == "Iris Setosa"
# test to check if Iris Virginica is classified correctly with different payload
def test_pred_virginica_diff_payload():
# defining a sample payload for the testcase
payload = {
"sepal_length": 5.2,
"sepal_width": 5.4,
"petal_length": 10.4,
"petal_width": 6.2,
}
with TestClient(app) as client:
response = client.post("/predict_flower", json=payload)
# asserting the correct response is received
assert response.status_code == 200
assert response.json()["flower_class"] == "Iris Virginica"
#test to check feedback_loop method for Iris Virginica
def test_feedback_loop_virginica():
# defining a sample payload for the testcase
payload = [{
"sepal_length": 3,
"sepal_width": 5,
"petal_length": 3.2,
"petal_width": 4.4,
"flower_class":"Iris Virginica"
}]
with TestClient(app) as client:
response = client.post("/feedback_loop", json=payload)
# asserting the correct response is received
assert response.status_code == 200
assert response.json() == {"detail": "Feedback loop successful"}
#test to check feedback_loop method
def test_feedback_loop():
# defining a sample payload for the testcase
payload = [{
"sepal_length": 3.8,
"sepal_width": 8,
"petal_length": 5.2,
"petal_width": 4.4,
"flower_class":"Iris Setosa"
}]
with TestClient(app) as client:
response = client.post("/feedback_loop", json=payload)
# asserting the correct response is received
assert response.status_code == 200
assert response.json() == {"detail": "Feedback loop successful"}