Coverage for src/tests/api/v1/trainings/test_endpoints.py: 97%

102 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the trainings endpoints.""" 

2 

3from typing import Any 

4 

5import pytest 

6 

7from fastapi import status 

8from fastapi.testclient import TestClient 

9from kwai_core.domain.value_objects.period import Period 

10from kwai_core.domain.value_objects.timestamp import Timestamp 

11 

12 

13pytestmark = pytest.mark.api 

14 

15 

16def _find(resource_list: list[dict[str, Any]], id_: str): 

17 """Search for a resource with the given id.""" 

18 for resource in resource_list: 

19 if resource["id"] == id_: 

20 return resource 

21 return None 

22 

23 

24async def test_get_trainings(client: TestClient, make_training_in_db): 

25 """Test get trainings api.""" 

26 training = await make_training_in_db() 

27 

28 response = client.get("/api/v1/trainings") 

29 assert response.status_code == status.HTTP_200_OK 

30 

31 json = response.json() 

32 assert "meta" in json, "There should be a meta object in the response" 

33 assert "data" in json, "There should be a data list in the response" 

34 assert len(json["data"]) > 0, "There should be at least one training" 

35 

36 training_resource = _find(json["data"], str(training.id)) 

37 assert training_resource is not None, f"Training with id {training.id} should exist" 

38 

39 

40async def test_get_trainings_filter_year_month( 

41 client: TestClient, make_training_in_db, make_training 

42): 

43 """Test get trainings api with filter on year/month.""" 

44 training = await make_training_in_db( 

45 make_training( 

46 period=Period( 

47 start_date=Timestamp.create_from_string("2023-01-02 19:00:00"), 

48 end_date=Timestamp.create_from_string("2023-01-02 20:00:00"), 

49 ) 

50 ) 

51 ) 

52 response = client.get( 

53 "/api/v1/trainings", params={"filter[year]": 2023, "filter[month]": 1} 

54 ) 

55 assert response.status_code == status.HTTP_200_OK 

56 

57 json = response.json() 

58 assert "meta" in json, "There should be a meta object in the response" 

59 assert "data" in json, "There should be a data list in the response" 

60 assert len(json["data"]) > 0, "There should be at least one training" 

61 

62 training_resource = _find(json["data"], str(training.id)) 

63 assert training_resource is not None, f"Training with id {training.id} should exist" 

64 

65 

66async def test_get_trainings_filter_start_end( 

67 client: TestClient, make_training_in_db, make_training 

68): 

69 """Test get trainings api with a filter on start and end date.""" 

70 training = await make_training_in_db( 

71 make_training( 

72 period=Period( 

73 start_date=Timestamp.create_from_string("2023-01-02 19:00:00"), 

74 end_date=Timestamp.create_from_string("2023-01-02 20:00:00"), 

75 ) 

76 ) 

77 ) 

78 response = client.get( 

79 "/api/v1/trainings", 

80 params={ 

81 "filter[start]": "2023-01-01 00:00:00", 

82 "filter[end]": "2023-01-31 00:00:00", 

83 }, 

84 ) 

85 assert response.status_code == status.HTTP_200_OK 

86 

87 json = response.json() 

88 assert "meta" in json, "There should be a meta object in the response" 

89 assert "data" in json, "There should be a data list in the response" 

90 assert len(json["data"]) > 0, "There should be at least one training" 

91 

92 training_resource = _find(json["data"], str(training.id)) 

93 assert training_resource is not None, f"Training with id {training.id} should exist" 

94 

95 

96def test_get_trainings_filter_coach(client: TestClient): 

97 """Test get trainings api with a filter for a coach.""" 

98 response = client.get( 

99 "/api/v1/trainings", 

100 params={"filter[coach]": "1"}, 

101 ) 

102 assert response.status_code in [status.HTTP_200_OK, status.HTTP_404_NOT_FOUND] 

103 

104 json = response.json() 

105 if response.status_code == status.HTTP_200_OK: 

106 assert "meta" in json, "There should be a meta object in the response" 

107 assert "data" in json, "There should be a data list in the response" 

108 else: 

109 assert "detail" in json 

110 

111 

112async def test_get_trainings_filter_active( 

113 client: TestClient, make_training_in_db, make_training 

114): 

115 """Test get trainings api with a filter for active trainings.""" 

116 training = await make_training_in_db(make_training(active=False)) 

117 response = client.get( 

118 "/api/v1/trainings", 

119 params={"filter[active]": "false"}, 

120 ) 

121 assert response.status_code in [status.HTTP_200_OK] 

122 

123 json = response.json() 

124 assert "meta" in json, "There should be a meta object in the response" 

125 assert "data" in json, "There should be a data list in the response" 

126 assert len(json["data"]) > 0, "There should be at least one training" 

127 

128 training_resource = _find(json["data"], str(training.id)) 

129 assert training_resource is not None, f"Training with id {training.id} should exist" 

130 

131 

132def test_get_trainings_filter_training_schedule(client: TestClient): 

133 """Test get trainings api with a filter for a training schedule.""" 

134 response = client.get( 

135 "/api/v1/trainings", 

136 params={"filter[schedule]": "1"}, 

137 ) 

138 assert response.status_code in [status.HTTP_200_OK, status.HTTP_404_NOT_FOUND] 

139 

140 json = response.json() 

141 if response.status_code == status.HTTP_200_OK: 

142 assert "meta" in json, "There should be a meta object in the response" 

143 assert "data" in json, "There should be a data list in the response" 

144 else: 

145 assert "detail" in json 

146 

147 

148async def test_get_training(client: TestClient, make_training_in_db): 

149 """Test /api/v1/trainings/{training_id}.""" 

150 training = await make_training_in_db() 

151 response = client.get(f"/api/v1/trainings/{training.id}") 

152 assert response.status_code == status.HTTP_200_OK 

153 

154 json = response.json() 

155 assert "data" in json, "There should be data in the response" 

156 

157 training_resource = json["data"] 

158 assert training_resource is not None, f"Training with id {training.id} should exist" 

159 

160 

161def test_create_training(secure_client: TestClient): 

162 """Test POST /api/v1/trainings.""" 

163 payload = { 

164 "data": { 

165 "type": "trainings", 

166 "attributes": { 

167 "texts": [ 

168 { 

169 "locale": "en", 

170 "format": "md", 

171 "title": "U13 Training", 

172 "summary": "", 

173 "original_summary": "Training for U13", 

174 "original_content": "", 

175 "content": "", 

176 } 

177 ], 

178 "event": { 

179 "start_date": "2023-02-02 19:00:00", 

180 "end_date": "2023-02-02 20:00:00", 

181 "active": True, 

182 "cancelled": False, 

183 "location": "", 

184 }, 

185 "remark": "", 

186 }, 

187 "relationships": { 

188 "coaches": {"data": []}, 

189 "teams": {"data": []}, 

190 "schedule": {"data": None}, 

191 }, 

192 } 

193 } 

194 response = secure_client.post("/api/v1/trainings", json=payload) 

195 assert response.status_code == status.HTTP_201_CREATED, response.json() 

196 

197 

198async def test_create_training_with_schedule( 

199 secure_client: TestClient, make_training_schedule_in_db 

200): 

201 """Test POST /api/v1/trainings.""" 

202 training_schedule = await make_training_schedule_in_db() 

203 

204 payload = { 

205 "data": { 

206 "type": "trainings", 

207 "attributes": { 

208 "texts": [ 

209 { 

210 "locale": "en", 

211 "format": "md", 

212 "title": "U13 Training", 

213 "summary": "", 

214 "original_summary": "Training for U13", 

215 "original_content": "", 

216 "content": "", 

217 } 

218 ], 

219 "event": { 

220 "start_date": "2023-02-02 19:00:00", 

221 "end_date": "2023-02-02 20:00:00", 

222 "active": True, 

223 "cancelled": False, 

224 "location": "", 

225 }, 

226 "remark": "", 

227 }, 

228 "relationships": { 

229 "coaches": {"data": []}, 

230 "teams": {"data": []}, 

231 "schedule": { 

232 "data": { 

233 "id": str(training_schedule.id), 

234 "type": "training_schedules", 

235 } 

236 }, 

237 }, 

238 } 

239 } 

240 response = secure_client.post("/api/v1/trainings", json=payload) 

241 assert response.status_code == status.HTTP_201_CREATED, response.json() 

242 

243 

244def test_create_training_with_coaches(secure_client: TestClient): 

245 """Test POST /api/v1/trainings with coaches.""" 

246 payload = { 

247 "data": { 

248 "type": "trainings", 

249 "attributes": { 

250 "texts": [ 

251 { 

252 "locale": "en", 

253 "format": "md", 

254 "title": "U13 Training", 

255 "summary": "", 

256 "original_summary": "Training for U13", 

257 "original_content": "", 

258 "content": "", 

259 } 

260 ], 

261 "event": { 

262 "start_date": "2023-02-02 19:00:00", 

263 "end_date": "2023-02-02 20:00:00", 

264 "active": True, 

265 "cancelled": False, 

266 "location": "", 

267 }, 

268 "remark": "", 

269 }, 

270 "relationships": { 

271 "coaches": {"data": [{"type": "training_coaches", "id": "1"}]}, 

272 "teams": {"data": []}, 

273 "schedule": {"data": None}, 

274 }, 

275 } 

276 } 

277 response = secure_client.post("/api/v1/trainings", json=payload) 

278 assert response.status_code == status.HTTP_201_CREATED, response.json() 

279 

280 

281def test_create_training_with_teams(secure_client: TestClient): 

282 """Test POST /api/v1/trainings with teams.""" 

283 payload = { 

284 "data": { 

285 "type": "trainings", 

286 "attributes": { 

287 "texts": [ 

288 { 

289 "locale": "en", 

290 "format": "md", 

291 "title": "U13 Training", 

292 "summary": "", 

293 "original_summary": "Training for U13", 

294 "original_content": "", 

295 "content": "", 

296 } 

297 ], 

298 "event": { 

299 "start_date": "2023-02-02 19:00:00", 

300 "end_date": "2023-02-02 20:00:00", 

301 "active": True, 

302 "cancelled": False, 

303 "location": "", 

304 }, 

305 "remark": "", 

306 }, 

307 "relationships": { 

308 "coaches": {"data": []}, 

309 "teams": {"data": [{"type": "teams", "id": "1"}]}, 

310 "schedule": {"data": None}, 

311 }, 

312 } 

313 } 

314 response = secure_client.post("/api/v1/trainings", json=payload) 

315 assert response.status_code == status.HTTP_201_CREATED, response.json() 

316 

317 

318async def test_update_training(secure_client: TestClient, make_training_in_db): 

319 """Test PATCH /api/v1/trainings.""" 

320 training_entity = await make_training_in_db() 

321 payload = { 

322 "data": { 

323 "type": "trainings", 

324 "id": str(training_entity.id), 

325 "attributes": { 

326 "texts": [ 

327 { 

328 "locale": "en", 

329 "format": "md", 

330 "title": "U13 Training", 

331 "summary": "", 

332 "original_summary": "Training for U13", 

333 "original_content": "", 

334 "content": "", 

335 } 

336 ], 

337 "event": { 

338 "start_date": "2023-02-02 19:00:00", 

339 "end_date": "2023-02-02 20:00:00", 

340 "active": True, 

341 "cancelled": False, 

342 "location": "", 

343 }, 

344 "remark": "Updated!", 

345 }, 

346 "relationships": { 

347 "coaches": {"data": []}, 

348 "teams": {"data": []}, 

349 "schedule": {"data": None}, 

350 }, 

351 } 

352 } 

353 response = secure_client.patch( 

354 f"/api/v1/trainings/{training_entity.id}", json=payload 

355 ) 

356 assert response.status_code == status.HTTP_200_OK, response.json() 

357 

358 

359async def test_delete_training(secure_client: TestClient, make_training_in_db): 

360 """Test DELETE /api/v1/trainings/{id}.""" 

361 training_entity = await make_training_in_db() 

362 response = secure_client.delete(f"/api/v1/trainings/{training_entity.id}") 

363 assert response.status_code == status.HTTP_200_OK, response.json()