Coverage for tests\test_put_api_projects_texts_delete.py: 100.00%

139 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-22 23:23 +0100

1# -*- coding: utf-8 -*- 

2 

3""" 

4* Name: interactive-clustering-gui/tests/test_put_api_projects_texts_delete.py 

5* Description: Unittests for `app` module on the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route. 

6* Author: Erwan Schild 

7* Created: 22/02/2022 

8* Licence: CeCILL (https://cecill.info/licences.fr.html) 

9""" 

10 

11# ============================================================================== 

12# IMPORT PYTHON DEPENDENCIES 

13# ============================================================================== 

14 

15import pytest 

16 

17from tests.dummies_utils import create_dummy_projects 

18 

19# ============================================================================== 

20# test_ko_not_found 

21# ============================================================================== 

22 

23 

24@pytest.mark.asyncio() 

25async def test_ko_not_found_1(async_client): 

26 """ 

27 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with not existing project. 

28 

29 Arguments: 

30 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

31 """ 

32 # Assert HTTP client is created. 

33 assert async_client 

34 

35 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

36 response_put = await async_client.put(url="/api/projects/UNKNOWN_PROJECT/texts/UNKNOWN_TEXT/delete") 

37 assert response_put.status_code == 404 

38 assert response_put.json() == { 

39 "detail": "The project with id 'UNKNOWN_PROJECT' doesn't exist.", 

40 } 

41 

42 

43# ============================================================================== 

44# test_ko_not_found 

45# ============================================================================== 

46 

47 

48@pytest.mark.asyncio() 

49async def test_ko_not_found_2(async_client, tmp_path): 

50 """ 

51 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with not existing project. 

52 

53 Arguments: 

54 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

55 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

56 """ 

57 # Assert HTTP client is created. 

58 assert async_client 

59 

60 # Create dummy projects. 

61 create_dummy_projects( 

62 tmp_path=tmp_path, 

63 list_of_dummy_project_ids=[ 

64 "1a_SAMPLING_TODO", 

65 ], 

66 ) 

67 

68 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

69 response_put = await async_client.put(url="/api/projects/1a_SAMPLING_TODO/texts/UNKNOWN_TEXT/delete") 

70 assert response_put.status_code == 404 

71 assert response_put.json() == { 

72 "detail": "In project with id '1a_SAMPLING_TODO', the text with id 'UNKNOWN_TEXT' to delete doesn't exist.", 

73 } 

74 

75 

76# ============================================================================== 

77# test_ko_bad_state_1 

78# ============================================================================== 

79 

80 

81@pytest.mark.asyncio() 

82async def test_ko_bad_state_1(async_client, tmp_path): 

83 """ 

84 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with bad state. 

85 

86 Arguments: 

87 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

88 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

89 """ 

90 # Assert HTTP client is created. 

91 assert async_client 

92 

93 # Create dummy projects. 

94 create_dummy_projects( 

95 tmp_path=tmp_path, 

96 list_of_dummy_project_ids=[ 

97 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

98 ], 

99 ) 

100 

101 # Get texts before test. 

102 response_get_texts_before = await async_client.get( 

103 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/texts?without_deleted_texts=false" 

104 ) 

105 assert response_get_texts_before.status_code == 200 

106 assert response_get_texts_before.json()["texts"]["11"]["is_deleted"] is False 

107 

108 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

109 response_put = await async_client.put(url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/texts/11/delete") 

110 assert response_put.status_code == 403 

111 assert response_put.json() == { 

112 "detail": "The project with id '0a_INITIALIZATION_WITHOUT_MODELIZATION' doesn't allow modification during this state (state='INITIALIZATION_WITHOUT_MODELIZATION').", 

113 } 

114 

115 # Assert route `GET /api/projects/{project_id}/texts` is still the same. 

116 response_get_texts_after = await async_client.get( 

117 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/texts?without_deleted_texts=false" 

118 ) 

119 assert response_get_texts_after.status_code == 200 

120 assert response_get_texts_after.json() == response_get_texts_before.json() 

121 assert response_get_texts_after.json()["texts"]["11"]["is_deleted"] is False 

122 

123 

124# ============================================================================== 

125# test_ko_bad_state_2 

126# ============================================================================== 

127 

128 

129@pytest.mark.asyncio() 

130async def test_ko_bad_state_2(async_client, tmp_path): 

131 """ 

132 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with bad state. 

133 

134 Arguments: 

135 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

136 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

137 """ 

138 # Assert HTTP client is created. 

139 assert async_client 

140 

141 # Create dummy projects. 

142 create_dummy_projects( 

143 tmp_path=tmp_path, 

144 list_of_dummy_project_ids=[ 

145 "1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS", 

146 ], 

147 ) 

148 

149 # Get texts before test. 

150 response_get_texts_before = await async_client.get( 

151 url="/api/projects/1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS/texts?without_deleted_texts=false" 

152 ) 

153 assert response_get_texts_before.status_code == 200 

154 assert response_get_texts_before.json()["texts"]["11"]["is_deleted"] is False 

155 

156 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

157 response_put = await async_client.put( 

158 url="/api/projects/1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS/texts/11/delete" 

159 ) 

160 assert response_put.status_code == 403 

161 assert response_put.json() == { 

162 "detail": "The project with id '1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS' doesn't allow modification during this state (state='ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS').", 

163 } 

164 

165 # Assert route `GET /api/projects/{project_id}/texts` is still the same. 

166 response_get_texts_after = await async_client.get( 

167 url="/api/projects/1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS/texts?without_deleted_texts=false" 

168 ) 

169 assert response_get_texts_after.status_code == 200 

170 assert response_get_texts_after.json() == response_get_texts_before.json() 

171 assert response_get_texts_after.json()["texts"]["11"]["is_deleted"] is False 

172 

173 

174# ============================================================================== 

175# test_ok_good_state_1 

176# ============================================================================== 

177 

178 

179@pytest.mark.asyncio() 

180async def test_ok_good_state_1(async_client, tmp_path): 

181 """ 

182 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with good state. 

183 

184 Arguments: 

185 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

186 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

187 """ 

188 # Assert HTTP client is created. 

189 assert async_client 

190 

191 # Create dummy projects. 

192 create_dummy_projects( 

193 tmp_path=tmp_path, 

194 list_of_dummy_project_ids=[ 

195 "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

196 ], 

197 ) 

198 

199 # Get texts before test. 

200 response_get_texts_before = await async_client.get( 

201 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false" 

202 ) 

203 assert response_get_texts_before.status_code == 200 

204 assert response_get_texts_before.json()["texts"]["11"]["is_deleted"] is False 

205 

206 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

207 response_put = await async_client.put(url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/11/delete") 

208 assert response_put.status_code == 202 

209 assert response_put.json() == { 

210 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

211 "text_id": "11", 

212 "detail": "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '11' has been deleted. Several constraints have been hidden.", 

213 } 

214 

215 # Assert route `GET /api/projects/{project_id}/texts` is updated. 

216 response_get_texts_after = await async_client.get( 

217 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false" 

218 ) 

219 assert response_get_texts_after.status_code == 200 

220 assert response_get_texts_after.json()["texts"]["11"]["is_deleted"] is True 

221 

222 # Assert route `GET /api/projects/{project_id}/status` is updated. 

223 response_get_status = await async_client.get(url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/status") 

224 assert response_get_status.status_code == 200 

225 assert response_get_status.json()["status"]["iteration_id"] == 1 

226 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS" 

227 

228 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

229 response_get_constraints = await async_client.get( 

230 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints" 

231 ) 

232 assert response_get_constraints.status_code == 200 

233 for constraint_value in response_get_constraints.json()["constraints"].values(): 

234 data_id_1 = constraint_value["data"]["id_1"] 

235 data_id_2 = constraint_value["data"]["id_2"] 

236 assert constraint_value["is_hidden"] is ( 

237 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"] 

238 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"] 

239 ) 

240 

241 

242# ============================================================================== 

243# test_ok_good_state_2 

244# ============================================================================== 

245 

246 

247@pytest.mark.asyncio() 

248async def test_ok_good_state_2(async_client, tmp_path): 

249 """ 

250 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with good state. 

251 

252 Arguments: 

253 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

254 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

255 """ 

256 # Assert HTTP client is created. 

257 assert async_client 

258 

259 # Create dummy projects. 

260 create_dummy_projects( 

261 tmp_path=tmp_path, 

262 list_of_dummy_project_ids=[ 

263 "1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS", 

264 ], 

265 ) 

266 

267 # Get texts before test. 

268 response_get_texts_before = await async_client.get( 

269 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts?without_deleted_texts=false" 

270 ) 

271 assert response_get_texts_before.status_code == 200 

272 assert response_get_texts_before.json()["texts"]["11"]["is_deleted"] is False 

273 

274 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

275 response_put = await async_client.put( 

276 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts/11/delete" 

277 ) 

278 assert response_put.status_code == 202 

279 assert response_put.json() == { 

280 "project_id": "1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS", 

281 "text_id": "11", 

282 "detail": "In project with id '1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS', the text with id '11' has been deleted. Several constraints have been hidden.", 

283 } 

284 

285 # Assert route `GET /api/projects/{project_id}/texts` is updated. 

286 response_get_texts_after = await async_client.get( 

287 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts?without_deleted_texts=false" 

288 ) 

289 assert response_get_texts_after.status_code == 200 

290 assert response_get_texts_after.json()["texts"]["11"]["is_deleted"] is True 

291 

292 # Assert route `GET /api/projects/{project_id}/status` is updated. 

293 response_get_status = await async_client.get( 

294 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/status" 

295 ) 

296 assert response_get_status.status_code == 200 

297 assert response_get_status.json()["status"]["iteration_id"] == 1 

298 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS" 

299 

300 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

301 response_get_constraints = await async_client.get( 

302 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/constraints" 

303 ) 

304 assert response_get_constraints.status_code == 200 

305 for constraint_value in response_get_constraints.json()["constraints"].values(): 

306 data_id_1 = constraint_value["data"]["id_1"] 

307 data_id_2 = constraint_value["data"]["id_2"] 

308 assert constraint_value["is_hidden"] is ( 

309 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"] 

310 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"] 

311 ) 

312 

313 

314# ============================================================================== 

315# test_ok_good_state_3 

316# ============================================================================== 

317 

318 

319@pytest.mark.asyncio() 

320async def test_ok_good_state_3(async_client, tmp_path): 

321 """ 

322 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with good state. 

323 

324 Arguments: 

325 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

326 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

327 """ 

328 # Assert HTTP client is created. 

329 assert async_client 

330 

331 # Create dummy projects. 

332 create_dummy_projects( 

333 tmp_path=tmp_path, 

334 list_of_dummy_project_ids=[ 

335 "1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS", 

336 ], 

337 ) 

338 

339 # Get texts before test. 

340 response_get_texts_before = await async_client.get( 

341 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts?without_deleted_texts=false" 

342 ) 

343 assert response_get_texts_before.status_code == 200 

344 assert response_get_texts_before.json()["texts"]["11"]["is_deleted"] is False 

345 

346 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

347 response_put = await async_client.put( 

348 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts/11/delete" 

349 ) 

350 assert response_put.status_code == 202 

351 assert response_put.json() == { 

352 "project_id": "1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS", 

353 "text_id": "11", 

354 "detail": "In project with id '1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS', the text with id '11' has been deleted. Several constraints have been hidden.", 

355 } 

356 

357 # Assert route `GET /api/projects/{project_id}/texts` is updated. 

358 response_get_texts_after = await async_client.get( 

359 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts?without_deleted_texts=false" 

360 ) 

361 assert response_get_texts_after.status_code == 200 

362 assert response_get_texts_after.json()["texts"]["11"]["is_deleted"] is True 

363 

364 # Assert route `GET /api/projects/{project_id}/status` is updated. 

365 response_get_status = await async_client.get( 

366 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/status" 

367 ) 

368 assert response_get_status.status_code == 200 

369 assert response_get_status.json()["status"]["iteration_id"] == 1 

370 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS" 

371 

372 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

373 response_get_constraints = await async_client.get( 

374 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/constraints" 

375 ) 

376 assert response_get_constraints.status_code == 200 

377 for constraint_value in response_get_constraints.json()["constraints"].values(): 

378 data_id_1 = constraint_value["data"]["id_1"] 

379 data_id_2 = constraint_value["data"]["id_2"] 

380 assert constraint_value["is_hidden"] is ( 

381 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"] 

382 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"] 

383 ) 

384 

385 

386# ============================================================================== 

387# test_ok_double_request 

388# ============================================================================== 

389 

390 

391@pytest.mark.asyncio() 

392async def test_ok_double_request(async_client, tmp_path): 

393 """ 

394 Test the `PUT /api/projects/{project_id}/texts/{text_id}/delete` route with double request. 

395 

396 Arguments: 

397 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

398 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

399 """ 

400 # Assert HTTP client is created. 

401 assert async_client 

402 

403 # Create dummy projects. 

404 create_dummy_projects( 

405 tmp_path=tmp_path, 

406 list_of_dummy_project_ids=[ 

407 "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

408 ], 

409 ) 

410 

411 # Get texts before test. 

412 response_get_texts_before = await async_client.get( 

413 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false" 

414 ) 

415 assert response_get_texts_before.status_code == 200 

416 assert response_get_texts_before.json()["texts"]["11"]["is_deleted"] is False 

417 

418 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

419 response_put1 = await async_client.put(url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/11/delete") 

420 assert response_put1.status_code == 202 

421 assert response_put1.json() == { 

422 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

423 "text_id": "11", 

424 "detail": "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '11' has been deleted. Several constraints have been hidden.", 

425 } 

426 

427 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/delete` works. 

428 response_put2 = await async_client.put(url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/11/delete") 

429 assert response_put2.status_code == 202 

430 assert response_put2.json() == { 

431 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

432 "text_id": "11", 

433 "detail": "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '11' has been deleted. Several constraints have been hidden.", 

434 } 

435 

436 # Assert route `GET /api/projects/{project_id}/texts` is updated. 

437 response_get_texts_after = await async_client.get( 

438 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts?without_deleted_texts=false" 

439 ) 

440 assert response_get_texts_after.status_code == 200 

441 assert response_get_texts_after.json()["texts"]["11"]["is_deleted"] is True 

442 

443 # Assert route `GET /api/projects/{project_id}/status` is updated. 

444 response_get_status = await async_client.get(url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/status") 

445 assert response_get_status.status_code == 200 

446 assert response_get_status.json()["status"]["iteration_id"] == 1 

447 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS" 

448 

449 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

450 response_get_constraints = await async_client.get( 

451 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints" 

452 ) 

453 assert response_get_constraints.status_code == 200 

454 for constraint_value in response_get_constraints.json()["constraints"].values(): 

455 data_id_1 = constraint_value["data"]["id_1"] 

456 data_id_2 = constraint_value["data"]["id_2"] 

457 assert constraint_value["is_hidden"] is ( 

458 response_get_texts_after.json()["texts"][data_id_1]["is_deleted"] 

459 or response_get_texts_after.json()["texts"][data_id_2]["is_deleted"] 

460 )