-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_led_storm_server.py
253 lines (206 loc) · 9.86 KB
/
test_led_storm_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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# pylint: skip-file
from model import Color, Lightning
from led_storm_server import create_app
import pytest
STRIPES = ["strip1", "strip2"]
def test_health_check():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.get('/')
assert response.status_code == 200
def test_static_js_files():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.get('/22bd2fc073cf8a1b.js')
assert response.status_code == 200
def test_static_wasm_files():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.get('/6a7294aacdcde4e7.wasm')
assert response.status_code == 200
def test_does_not_show_ligtning_if_request_body_is_empty():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lightning')
assert response.status_code == 400
def test_does_not_show_ligtning_if_request_body_is_malformed():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lightning', json={"light": []})
assert response.status_code == 400
def test_does_not_show_ligtning_if_request_does_not_specify_at_least_two_lightnings():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lightning', json={"lightnings": []})
assert response.status_code == 400
def test_does_not_show_ligtning_if_request_contains_only_one_lightning_info():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post(
'/lightning', json={"lightnings": [{"r": 255, "g": 255, "b": 255}]})
assert response.status_code == 400
def test_does_not_show_ligtning_if_request_values_are_out_of_rgb_range():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lightning',
json={"lightnings": [{"r": 255,
"g": 255,
"b": 255},
{"r": 255,
"g": 255,
"b": 300}]})
assert response.status_code == 400
def test_shows_lightning_using_the_request_params_if_the_body_is_valid(mocker):
spy = mocker.patch("led_storm_server.trigger_lightning")
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lightning',
json={"lightnings": [{"r": 255,
"g": 255,
"b": 255},
{"r": 255,
"g": 255,
"b": 255}]})
assert spy.call_count == 1
assert response.status_code == 200
def test_uses_request_params_to_define_lightnings(mocker):
spy = mocker.patch("led_storm_server.trigger_lightning")
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
lightning_1 = {"r": 1, "g": 2, "b": 3}
lightning_2 = {"r": 4, "g": 5, "b": 6}
request_body = {"lightnings": [lightning_1, lightning_2]}
response = test_client.post('/lightning', json=request_body)
expected_lightning_1 = Lightning(
STRIPES[0],
Color(
lightning_1["r"],
lightning_1["g"],
lightning_1["b"]))
expected_lightning_2 = Lightning(
STRIPES[1],
Color(
lightning_2["r"],
lightning_2["g"],
lightning_2["b"]))
expeted_trigger_param = [expected_lightning_1, expected_lightning_2]
assert response.status_code == 200
spy.assert_called_once_with(expeted_trigger_param)
def test_ignores_other_lightning_definitions_after_the_second_one(mocker):
spy = mocker.patch("led_storm_server.trigger_lightning")
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
lightning_1 = {"r": 1, "g": 2, "b": 3}
lightning_2 = {"r": 4, "g": 5, "b": 6}
lightning_3 = {"r": 7, "g": 8, "b": 9}
request_body = {"lightnings": [lightning_1, lightning_2, lightning_3]}
response = test_client.post('/lightning', json=request_body)
expected_lightning_1 = Lightning(
STRIPES[0],
Color(
lightning_1["r"],
lightning_1["g"],
lightning_1["b"]))
expected_lightning_2 = Lightning(
STRIPES[1],
Color(
lightning_2["r"],
lightning_2["g"],
lightning_2["b"]))
expeted_trigger_param = [expected_lightning_1, expected_lightning_2]
assert response.status_code == 200
spy.assert_called_once_with(expeted_trigger_param)
def test_does_not_turn_lamp_on_if_request_body_is_empty():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lamp')
assert response.status_code == 400
def test_does_not_turn_lamp_on_if_request_body_is_malformed():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lamp', json={"light": []})
assert response.status_code == 400
def test_does_not_turn_lamp_on_if_request_does_not_specify_at_least_two_lightnings():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lamp', json={"lightnings": []})
assert response.status_code == 400
def test_does_not_turn_lamp_on_if_request_contains_only_one_lightning_info():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post(
'/lamp', json={"lightnings": [{"r": 255, "g": 255, "b": 255}]})
assert response.status_code == 400
def test_does_not_turn_lamp_on_if_request_values_are_out_of_rgb_range():
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lamp',
json={"lightnings": [{"r": 255,
"g": 255,
"b": 255},
{"r": 255,
"g": 255,
"b": 300}]})
assert response.status_code == 400
def test_turns_lamp_on_using_the_request_params_if_the_body_is_valid(mocker):
spy = mocker.patch("led_storm_server.switch_lamp_on")
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
response = test_client.post('/lamp',
json={"lightnings": [{"r": 255,
"g": 255,
"b": 255},
{"r": 255,
"g": 255,
"b": 255}]})
assert spy.call_count == 1
assert response.status_code == 200
def test_uses_request_params_to_define_the_lamp_color(mocker):
spy = mocker.patch("led_storm_server.switch_lamp_on")
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
lightning_1 = {"r": 1, "g": 2, "b": 3}
lightning_2 = {"r": 4, "g": 5, "b": 6}
request_body = {"lightnings": [lightning_1, lightning_2]}
response = test_client.post('/lamp', json=request_body)
expected_lightning_1 = Lightning(
STRIPES[0],
Color(
lightning_1["r"],
lightning_1["g"],
lightning_1["b"]))
expected_lightning_2 = Lightning(
STRIPES[1],
Color(
lightning_2["r"],
lightning_2["g"],
lightning_2["b"]))
expeted_trigger_param = [expected_lightning_1, expected_lightning_2]
assert response.status_code == 200
spy.assert_called_once_with(expeted_trigger_param)
def test_ignores_other_lightning_definitions_after_the_second_one_turning_lamp_on(mocker):
spy = mocker.patch("led_storm_server.switch_lamp_on")
flask_app = create_led_storm_app()
with flask_app.test_client() as test_client:
lightning_1 = {"r": 1, "g": 2, "b": 3}
lightning_2 = {"r": 4, "g": 5, "b": 6}
lightning_3 = {"r": 7, "g": 8, "b": 9}
request_body = {"lightnings": [lightning_1, lightning_2, lightning_3]}
response = test_client.post('/lamp', json=request_body)
expected_lightning_1 = Lightning(
STRIPES[0],
Color(
lightning_1["r"],
lightning_1["g"],
lightning_1["b"]))
expected_lightning_2 = Lightning(
STRIPES[1],
Color(
lightning_2["r"],
lightning_2["g"],
lightning_2["b"]))
expeted_trigger_param = [expected_lightning_1, expected_lightning_2]
assert response.status_code == 200
spy.assert_called_once_with(expeted_trigger_param)
def create_led_storm_app():
return create_app(STRIPES)