-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_main.py
167 lines (140 loc) · 4.77 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
import os
import pytest
import main
from fastapi.testclient import TestClient
from main import app
@pytest.fixture()
def client():
return TestClient(app)
@pytest.fixture()
def mock_instances():
instance1 = {
"taskId": "0",
"itemId": "0",
"itemPrompt": "mock_prompt",
"itemTargets": ["one", "two", "three"],
"learnerId": "0",
"answer": "two",
"label": 1
}
instance2 = {
"taskId": "1",
"itemId": "1",
"itemPrompt": "mock_prompt2",
"itemTargets": ["four", "five", "six"],
"learnerId": "1",
"answer": "two",
"label": 1
}
instance3 = {
"taskId": "2",
"itemId": "2",
"itemPrompt": "mock_prompt3",
"itemTargets": ["four", "five", "six"],
"learnerId": "2",
"answer": "five",
"label": 2
}
instance4 = {
"taskId": "2",
"itemId": "2",
"itemPrompt": "mock_prompt3",
"itemTargets": ["four", "five", "six"],
"learnerId": "2",
"answer": "five",
"label": 2
}
instances = [instance1, instance2, instance3, instance4]
for _ in range(10):
instances.append(instance1)
instances.append(instance2)
instances.append(instance3)
instances.append(instance4)
# The dicionaries are used to set up ShortAnswerInstance objects.
return instances
@pytest.fixture()
def predict_instances():
instance1 = {
"taskId": "0",
"itemId": "0",
"itemPrompt": "mock_prompt",
"itemTargets": ["one", "two", "three"],
"learnerId": "0",
"answer": "two",
}
instance2 = {
"taskId": "1",
"itemId": "1",
"itemPrompt": "mock_prompt2",
"itemTargets": ["two", "three", "four"],
"learnerId": "1",
"answer": "two",
}
instance3 = {
"taskId": "2",
"itemId": "2",
"itemPrompt": "mock_prompt3",
"itemTargets": ["four", "five", "six"],
"learnerId": "2",
"answer": "five",
}
return [instance1, instance2, instance3]
def test_trainFromAnswers(client, mock_instances):
"""
Test the /trainFromAnswers endpoint.
:param client: A client for testing.
:param mock_instances: Mock short answer instances
"""
# Change the onnx model directory for testing purposes.
main.onnx_model_dir = "testdata/train_data/onnx"
main.bow_model_dir = "testdata/train_data/bow"
instance_dict = {
"instances": mock_instances,
"modelId": "random_data",
}
response = client.post("/trainFromAnswers", json=instance_dict)
# Store states to check whether the file and session object were created.
path_exists = os.path.exists(os.path.join(main.onnx_model_dir, "random_data.onnx"))
bow_path_exists = os.path.exists(os.path.join(main.bow_model_dir, "random_data.json"))
metrics_path_exists = os.path.exists(os.path.join("model_metrics", "random_data.json"))
session_stored = "random_data" in main.inf_sessions
# Delete all files that have been created during training.
if session_stored:
del main.inf_sessions["random_data"]
if path_exists:
os.remove(os.path.join(main.onnx_model_dir, "random_data.onnx"))
if bow_path_exists:
os.remove(os.path.join(main.bow_model_dir, "random_data.json"))
if metrics_path_exists:
os.remove(os.path.join("model_metrics", "random_data.json"))
main.onnx_model_dir = "onnx_models"
main.bow_model_dir = "bow_models"
# The assertions are made after the clean-up process on the basis of the
# stored states. This ensures that cleaning is done in any case.
assert response.status_code == 200
assert path_exists
assert bow_path_exists
assert metrics_path_exists
assert session_stored
def test_predictFromAnswers(client, predict_instances):
"""
Test the /predictFromAnswers endpoint.
:param client: A client for testing.
:param mock_instances: Mock short answer instances that do not have labels
"""
pred_instance_dict = {
"instances": predict_instances,
"modelId": "test_pred_data",
}
pred_response = client.post("/predictFromAnswers", json=pred_instance_dict)
assert pred_response.status_code == 200
response_dict = json.loads(pred_response.content.decode("utf-8"))
assert response_dict["predictions"][0]["prediction"] == 1
assert response_dict["predictions"][1]["prediction"] == 1
assert response_dict["predictions"][2]["prediction"] == 2
def test_fetchStoredModels(client):
response = client.get("/fetchStoredModels")
assert response.status_code == 200
response_dict = json.loads(response.content.decode("utf-8"))["modelIds"]
assert list(main.inf_sessions.keys()) == response_dict