-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server.py
118 lines (94 loc) · 3.03 KB
/
test_server.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
import unittest
import app
from app import TasksProcessor
from io import BytesIO as IO
class TestTaskProcessor(unittest.TestCase):
def setUp(self):
app.tasks_queue.clear()
def test_no_tasks_ok(self):
t = TasksProcessor(0)
t.process_queue()
def test_task_reverse(self):
app.TaskType.reverse_time = 0
task = {
'id': 1,
'type': app.TaskType.reverse,
'payload': '123',
}
t = TasksProcessor(0)
t.process_task(task)
assert task['result'] == '321'
def test_task_mix_even_even(self):
app.TaskType.mix_even_time = 0
task = {
'id': 1,
'type': app.TaskType.mix_even,
'payload': '1234',
'status': app.TaskStatus.queued
}
app.tasks_queue.append(task)
t = TasksProcessor(0)
t.process_queue()
assert task['status'] == app.TaskStatus.done
assert len(app.tasks_queue) == 0
assert task['result'] == '2143'
def test_task_mix_even_odd(self):
app.TaskType.mix_even_time = 0
task = {
'id': 1,
'type': app.TaskType.mix_even,
'payload': '123',
}
t = TasksProcessor(0)
t.process_task(task)
assert task['result'] == '213'
def test_task_bad(self):
task = {
'id': 1,
'type': 'none',
'payload': '123',
}
t = TasksProcessor(0)
t.process_task(task)
assert task['status'] == app.TaskStatus.error
assert 'result' not in task
class MockRequest:
def __init__(self, request_data):
self.data = request_data
def makefile(self, *args, **kwargs):
return IO(self.data)
def sendall(self, b):
pass
class MockServer:
def __init__(self, ip_port, Handler, data):
handler = Handler(MockRequest(data), ip_port, self)
# todo use mock instead
class TestHTTPHandler(app.RestJsonHTTPRequestHandler):
def log_message(self, format_, *args):
global output_msg
output_msg = args
class TestServerNoExceptions(unittest.TestCase):
def setUp(self):
global output_msg
output_msg = (None, None)
def send_request(self, data):
MockServer(('0.0.0.0', 8888), TestHTTPHandler,
data.encode())
def test_get(self):
self.send_request('GET /')
assert output_msg[1] == '404'
def test_get_bad_url(self):
self.send_request(f'GET {app.API_URL}/random')
assert output_msg[1] == '404'
def test_list_tasks(self):
self.send_request(f'GET {app.API_URL}')
assert output_msg[1] == '200'
def test_get_task_status(self):
self.send_request(f'GET {app.API_URL}/1/status/')
assert output_msg[1] == '200'
def test_get_task_result(self):
self.send_request(f'GET {app.API_URL}/1/result/')
assert output_msg[1] == '200'
def test_put(self):
self.send_request(f'PUT {app.API_URL}')
assert output_msg[1] == '400'