-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.py
88 lines (76 loc) · 2.23 KB
/
unit_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
"""
For test hugchat
"""
# import os
import logging
from .hugchat.message import MSGSTATUS_RESOLVED, RESPONSE_TYPE_FINAL, Message, MSGTYPE_ERROR, RESPONSE_TYPE_WEB
import sys
logging.basicConfig(level=logging.DEBUG)
class MockResponse():
"""
mock a respone from hugchat api
"""
def __init__(self, response_list):
self.index = 0
self.response_list = response_list
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.response_list):
raise StopIteration
result = self.response_list[self.index]
self.index += 1
return result
class Test(object):
"""
test hugchat api
"""
def test_web_search_failed_results(self):
response_list = [{
"type":
RESPONSE_TYPE_WEB,
"messageType":
MSGTYPE_ERROR,
"message":
"Failed to parse webpage",
"args": [
"This operation was aborted",
"https://www.accuweather.com/en/gb/london/ec4a-2/weather-forecast/328328"
]
}, {
"type":
RESPONSE_TYPE_WEB,
"messageType":
RESPONSE_TYPE_WEB,
"sources": [{
"title": "1",
"link": "2",
"hostname": "3"
}]
}, {
"type":
RESPONSE_TYPE_WEB,
"messageType":
MSGTYPE_ERROR,
"message":
"Failed to parse webpage",
"args": [
"This operation was aborted",
"https://www.accuweather.com/en/gb/london/ec4a-2/weather-forecast/328328"
]
}, {
"type": RESPONSE_TYPE_FINAL,
"messageType": "answer",
"text": "Funny joke"
}]
mock = MockResponse(response_list)
response = Message(mock, web_search=True)
while True:
try:
print(response)
response = next(response)
except StopIteration:
break
assert len(response.web_search_sources
) == 1 # only the second web search response is valid.
assert response.text == "Funny joke"