-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
173 lines (158 loc) · 5.66 KB
/
test.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
168
169
170
171
172
173
import json
import unittest
from flask_app.app import create_app
class ChatTestCases(unittest.TestCase):
def setUp(self):
self.app = create_app(config_key='test')
self.client = self.app.test_client()
def test_single_chat(self):
mock_data = {
"query": "Hello!",
"with_memory": False
}
try:
response = self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
self.assertEqual(200, response.status_code)
json_data = response.get_json()
self.assertIn('reply', json_data)
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise
def test_multi_chat(self):
history = [
{
'type': 'human',
'data': {
'content': 'hi!'
}
},
{
'type': 'ai',
'data': {
'content': 'whats up?'
}
},
{
'type': 'human',
'data': {
'content': 'I am Dijkstra'
}
},
{
'type': 'ai',
'data': {
'content': 'Hello Dijkstra'
}
}
]
mock_data = {
"query": "Hello! What's my name?",
"history": history,
"with_memory": True
}
try:
response = self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
self.assertEqual(200, response.status_code)
json_data = response.get_json()
self.assertIn('reply', json_data, 'Didn\'t get the reply message')
self.assertIn("Dijkstra", json_data["reply"],
'Expected Dijkstra in the response, but the response is {}'.format(json_data["reply"]))
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise
def test_empty_query(self):
mock_data = {
"with_memory": True
}
try:
response = self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
self.assertEqual(response.status_code, 400)
json_data = response.get_json()
self.assertEqual(json_data['error'], 'Query must not be empty.')
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise
def test_invalid_model(self):
mock_data = {
"query": "Hello!",
"with_memory": False,
"model_name": "ChatGLM",
}
try:
response = self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
self.assertEqual(response.status_code, 400)
json_data = response.get_json()
self.assertEqual(json_data['error'], 'Model does not exist.')
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise
def test_invalid_chat_history(self):
history = [
{
'type': 'Dijkstra',
'date': {
'content': 'hi!'
}
},
{
'type': 'ia',
'data': {
'contet': 'whats u'
}
}
]
mock_data = {
"query": "Hello!",
"history": history,
"with_memory": True
}
try:
response = self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
self.assertEqual(400, response.status_code)
json_data = response.get_json()
self.assertIn('Please check the format of history message you post', json_data["error"],
'Didn\'t get the reply message')
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise
def test_invalid_model(self):
mock_data = {
"query": "Hello!",
"with_memory": False,
"model_name": "Dijkstra"
}
try:
response = self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
self.assertEqual(400, response.status_code)
json_data = response.get_json()
self.assertIn('Model {} does not exist.'.format(mock_data["model_name"]), json_data["error"],
'Didn\'t get the error message')
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise
def test_chat_get(self):
mock_data = {
"query": "Hello!",
"with_memory": False
}
try:
self.client.post('/chat',
data=json.dumps(mock_data),
content_type='application/json')
response = self.client.get('/chat')
self.assertEqual(200, response.status_code)
except AssertionError as e:
print("Test failed. Reason:", str(e))
raise