-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
258 lines (171 loc) · 5.21 KB
/
test.py
File metadata and controls
258 lines (171 loc) · 5.21 KB
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
254
255
256
257
258
import json
import os
import psycopg
import socketio
import pytest
import requests
from dotenv import load_dotenv
load_dotenv()
API_URL = os.getenv("API_URL")
SOCKET_URL = os.getenv("SOCKET_URL")
POSTGRES_URL = os.getenv("POSTGRES_URL")
## Testing the API
def clean_up_games():
with psycopg.connect(POSTGRES_URL) as conn:
with conn.cursor() as cur:
cur.execute("DELETE FROM games")
conn.commit()
def create_game(players_needed: int = 2):
response = requests.get(f"{API_URL}/create-game?players_needed={players_needed}")
assert response.status_code == 200
data = response.json()
assert "id" in data
return data["id"]
def test_create_game():
id = create_game()
# check if there is a row in the db
with psycopg.connect(POSTGRES_URL) as conn:
with conn.cursor() as cur:
cur.execute("SELECT id FROM games WHERE id = %s", (id,))
conn.commit()
assert cur.fetchone() is not None
def test_get_game():
PLAYERS_NEEDED = 2
id = create_game(PLAYERS_NEEDED)
response = requests.get(f"{API_URL}/get-game/{id}")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert "playersNeeded" in data
assert "maxTimeS" in data
assert "code" in data
assert data["status"] == 0
assert data["playersNeeded"] == PLAYERS_NEEDED
assert type(data["code"]) == str and len(data["code"]) > 0
def test_list_games():
create_game()
response = requests.get(f"{API_URL}/games-list?amount=5")
assert response.status_code == 200
data = response.json()
assert type(data) == list
assert len(data) > 0
for game in data:
assert "id" in game
assert "playersNeeded" in game
assert "maxTimeS" in game
def test_connect_to_socket():
sio = socketio.Client()
sio.connect(SOCKET_URL)
sio.disconnect()
### Testing the worker
def two_players_join_game(id):
# player 1: join game
sio = socketio.Client()
sio.connect(SOCKET_URL)
sio.emit("join_game", {"gameId": id})
# player 2: join game
sio2 = socketio.Client()
sio2.connect(SOCKET_URL)
sio2.emit("join_game", {"gameId": id})
return sio, sio2
def test_join_game():
id = create_game()
sio, sio2 = two_players_join_game(id)
# player 1 should get 'players_joined' event (with player 2)
# player 2 should get 'players_joined' event (with player 1)
events_1 = 0
events_2 = 0
player_1_in_data = 0
player_2_in_data = 0
@sio.on("players_joined")
def on_players_joined(msg):
nonlocal events_1
events_1 += 1
data = json.loads(msg)
nonlocal player_2_in_data
player_2_in_data |= sio2.get_sid() in data["playerIds"]
@sio2.on("players_joined")
def on_players_joined(msg):
nonlocal events_2
events_2 += 1
data = json.loads(msg)
nonlocal player_1_in_data
player_1_in_data |= sio.get_sid() in data["playerIds"]
sio.sleep(1)
assert events_1 >= 1
assert events_2 >= 1
assert player_1_in_data
assert player_2_in_data
sio.disconnect()
sio2.disconnect()
def test_update_progress():
id = create_game()
sio, sio2 = two_players_join_game(id)
sio.sleep(1)
# player 1: update progress
sio.emit("update_progress", {"progress": 5})
# player 2 should get 'updated_progress' event
progress = 0
@sio2.on("updated_progress")
def on_updated_progress(msg):
nonlocal progress
data = json.loads(msg)
if data["playerId"] == sio.get_sid():
progress = data["progress"]
sio.sleep(1)
assert progress == 5
sio.disconnect()
sio2.disconnect()
def test_finish_typing():
id = create_game()
sio, sio2 = two_players_join_game(id)
sio.sleep(1)
# player 1: finish typing
sio.emit("finished_typing")
# player 2 should get 'player_finished_typing' event
player_finished = False
place = -1
@sio2.on("player_finished")
def on_player_finished(msg):
nonlocal player_finished
nonlocal place
player_finished = True
data = json.loads(msg)
place = data["place"]
sio.sleep(1)
assert player_finished
assert place == 1
sio.disconnect()
sio2.disconnect()
def test_end_game():
id = create_game()
sio, sio2 = two_players_join_game(id)
sio.sleep(1)
# player 1 & 2: finish typing
sio.emit("finished_typing")
sio.sleep(1)
sio2.emit("finished_typing")
# player 1 & 2 should get 'end_game' event
game_ended = 0
def on_end_game():
nonlocal game_ended
game_ended += 1
sio.on("end_game", on_end_game)
sio2.on("end_game", on_end_game)
sio.sleep(1)
assert game_ended == 2
# player 1 & 2: disconnect
sio.disconnect()
sio2.disconnect()
sio.sleep(1)
# check if the game has been deleted
with psycopg.connect(POSTGRES_URL) as conn:
with conn.cursor() as cur:
cur.execute("SELECT id FROM games WHERE id = %s", (id,))
conn.commit()
assert cur.fetchone() is None
## Cleanup
@pytest.fixture(autouse=True)
def run_around_tests():
yield
clean_up_games()