-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_main.py
More file actions
282 lines (238 loc) · 8.9 KB
/
test_main.py
File metadata and controls
282 lines (238 loc) · 8.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# ------------------------------------------------------------------------------
# Test
# ------------------------------------------------------------------------------
import json
from tests.player_stub import existing_player, nonexistent_player, unknown_player
PATH = "/players/"
# GET /players/ ----------------------------------------------------------------
def test_given_get_when_request_is_initial_then_response_header_x_cache_should_be_miss(client):
"""
Given GET /players/
when request is initial
then response Header X-Cache value should be MISS
"""
# Act
response = client.get(PATH)
# Assert
assert "X-Cache" in response.headers
assert response.headers.get("X-Cache") == "MISS"
def test_given_get_when_request_is_subsequent_then_response_header_x_cache_should_be_hit(client):
"""
Given GET /players/
when request is subsequent
then response Header X-Cache should be HIT
"""
# Act
client.get(PATH) # initial
response = client.get(PATH) # subsequent (cached)
# Assert
assert "X-Cache" in response.headers
assert response.headers.get("X-Cache") == "HIT"
def test_given_get_when_request_path_has_no_id_then_response_status_code_should_be_200_ok(client):
"""
Given GET /players/
when request path has no ID
then response Status Code should be 200 OK
"""
# Act
response = client.get(PATH)
# Assert
assert response.status_code == 200
def test_given_get_when_request_path_has_no_id_then_response_body_should_be_collection_of_players(client):
"""
Given GET /players/
when request path has no ID
then response Body should be collection of players
"""
# Act
response = client.get(PATH)
# Assert
players = response.json()
player_id = 0
for player in players:
player_id += 1
assert player["id"] == player_id
# GET /players/{player_id} -----------------------------------------------------
def test_given_get_when_request_path_is_nonexistent_id_then_response_status_code_should_be_404_not_found(client):
"""
Given GET /players/{player_id}
when request path is nonexistent ID
then response Status Code should be 404 (Not Found)
"""
# Arrange
player_id = nonexistent_player().id
# Act
response = client.get(PATH + str(player_id))
# Assert
assert response.status_code == 404
def test_given_get_when_request_path_is_existing_id_then_response_status_code_should_be_200_ok(client):
"""
Given GET /players/{player_id}
when request path is existing ID
then response Status Code should be 200 (OK)
"""
# Arrange
player_id = existing_player().id
# Act
response = client.get(PATH + str(player_id))
# Assert
assert response.status_code == 200
def test_given_get_when_request_path_is_existing_id_then_response_body_should_be_matching_player(client):
"""
Given GET /players/{player_id}
when request path is existing ID
then response body should be matching Player
"""
# Arrange
player_id = existing_player().id
# Act
response = client.get(PATH + str(player_id))
# Assert
player = response.json()
assert player["id"] == player_id
# GET /players/squadnumber/{squad_number} --------------------------------------
def test_given_get_when_request_path_is_nonexistent_squad_number_then_response_status_code_should_be_404_not_found(client):
"""
Given GET /players/squadnumber/{squad_number}
when request path is nonexistent Squad Number
then response Status Code should be 404 (Not Found)
"""
# Arrange
squad_number = nonexistent_player().squad_number
# Act
response = client.get(PATH + "squadnumber" + "/" + str(squad_number))
# Assert
assert response.status_code == 404
def test_given_get_when_request_path_is_existing_squad_number_then_response_status_code_should_be_200_ok(client):
"""
Given GET /players/squadnumber/{squad_number}
when request path is existing Squad Number
then response Status Code should be 200 (OK)
"""
# Arrange
squad_number = existing_player().squad_number
# Act
response = client.get(PATH + "squadnumber" + "/" + str(squad_number))
# Assert
assert response.status_code == 200
def test_given_get_when_request_path_is_existing_squad_number_then_response_body_should_be_matching_player(client):
"""
Given GET /players/squadnumber/{squad_number}
when request path is existing Squad Number
then response body should be matching Player
"""
# Arrange
squad_number = existing_player().squad_number
# Act
response = client.get(PATH + "squadnumber" + "/" + str(squad_number))
# Assert
player = response.json()
assert player["squadNumber"] == squad_number
# POST /players/ ---------------------------------------------------------------
def test_given_post_when_request_body_is_empty_then_response_status_code_should_be_422_unprocessable_entity(client):
"""
Given POST /players/
when request body is empty
then response Status Code should be 422 (Unprocessable Entity)
"""
# Arrange
body = {}
# Act
response = client.post(PATH, data=body)
# Assert
assert response.status_code == 422
def test_given_post_when_request_body_is_existing_player_then_response_status_code_should_be_409_conflict(client):
"""
Given POST /players/
when request body is existing Player
then response Status Code should be 409 (Conflict)
"""
# Arrange
player = existing_player()
body = json.dumps(player.__dict__)
# Act
response = client.post(PATH, data=body)
# Assert
assert response.status_code == 409
def test_given_post_when_request_body_is_nonexistent_player_then_response_status_code_should_be_201_created(client):
"""
Given POST /players/
when request body is nonexistent Player
then response Status Code should be 201 (Created)
"""
# Arrange
player = nonexistent_player()
body = json.dumps(player.__dict__)
# Act
response = client.post(PATH, data=body)
# Assert
assert response.status_code == 201
# PUT /players/{player_id} -----------------------------------------------------
def test_given_put_when_request_body_is_empty_then_response_status_code_should_be_422_unprocessable_entity(client):
"""
Given PUT /players/{player_id}
when request body is empty
then response Status Code should be 422 (Unprocessable Entity)
"""
# Arrange
player_id = existing_player().id
body = {}
# Act
response = client.put(PATH + str(player_id), data=body)
# Assert
assert response.status_code == 422
def test_given_put_when_request_path_is_unknown_id_then_response_status_code_should_be_404_not_found(client):
"""
Given PUT /players/{player_id}
when request path is unknown ID
then response Status Code should be 404 (Not Found)
"""
# Arrange
player_id = unknown_player().id
player = unknown_player()
body = json.dumps(player.__dict__)
# Act
response = client.put(PATH + str(player_id), data=body)
# Assert
assert response.status_code == 404
def test_given_put_when_request_path_is_existing_id_then_response_status_code_should_be_204_no_content(client):
"""
Given PUT /players/{player_id}
when request path is existing ID
then response Status Code should be 204 (No Content)
"""
# Arrange
player_id = existing_player().id
player = existing_player()
player.first_name = "Emiliano"
player.middle_name = ""
body = json.dumps(player.__dict__)
# Act
response = client.put(PATH + str(player_id), data=body)
# Assert
assert response.status_code == 204
# DELETE /players/{player_id} --------------------------------------------------
def test_given_delete_when_request_path_is_unknown_id_then_response_status_code_should_be_404_not_found(client):
"""
Given DELETE /players/{player_id}
when request path is unknown ID
then response Status Code should be 404 (Not Found)
"""
# Arrange
player_id = unknown_player().id
# Act
response = client.delete(PATH + str(player_id))
# Assert
assert response.status_code == 404
def test_given_delete_when_request_path_is_existing_id_then_response_status_code_should_be__204_no_content(client):
"""
Given DELETE /players/{player_id}
when request path is existing ID
then response Status Code should be 204 (No Content)
"""
# Arrange
player_id = 12 # nonexistent_player() previously created
# Act
response = client.delete(PATH + str(player_id))
# Assert
assert response.status_code == 204