Coverage for tests\test_put_api_projects_texts_rename.py: 100.00%

113 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_rename.py 

5* Description: Unittests for `app` module on the `PUT /api/projects/{project_id}/texts/{text_id}/rename` 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 

15from urllib.parse import quote_plus, urlencode 

16 

17import pytest 

18 

19from tests.dummies_utils import create_dummy_projects 

20 

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

22# test_ko_not_found 

23# ============================================================================== 

24 

25 

26@pytest.mark.asyncio() 

27async def test_ko_not_found_1(async_client): 

28 """ 

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

30 

31 Arguments: 

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

33 """ 

34 # Assert HTTP client is created. 

35 assert async_client 

36 

37 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

38 response_put = await async_client.put( 

39 url="/api/projects/UNKNOWN_PROJECT/texts/UNKNOWN_TEXT/rename?" 

40 + urlencode( 

41 {"text_value": "Changement de valeur !"}, 

42 quote_via=quote_plus, 

43 ) 

44 ) 

45 assert response_put.status_code == 404 

46 assert response_put.json() == { 

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

48 } 

49 

50 

51# ============================================================================== 

52# test_ko_not_found 

53# ============================================================================== 

54 

55 

56@pytest.mark.asyncio() 

57async def test_ko_not_found_2(async_client, tmp_path): 

58 """ 

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

60 

61 Arguments: 

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

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

64 """ 

65 # Assert HTTP client is created. 

66 assert async_client 

67 

68 # Create dummy projects. 

69 create_dummy_projects( 

70 tmp_path=tmp_path, 

71 list_of_dummy_project_ids=[ 

72 "1a_SAMPLING_TODO", 

73 ], 

74 ) 

75 

76 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

77 response_put = await async_client.put( 

78 url="/api/projects/1a_SAMPLING_TODO/texts/UNKNOWN_TEXT/rename?" 

79 + urlencode( 

80 {"text_value": "Changement de valeur !"}, 

81 quote_via=quote_plus, 

82 ) 

83 ) 

84 assert response_put.status_code == 404 

85 assert response_put.json() == { 

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

87 } 

88 

89 

90# ============================================================================== 

91# test_ko_bad_state_1 

92# ============================================================================== 

93 

94 

95@pytest.mark.asyncio() 

96async def test_ko_bad_state_1(async_client, tmp_path): 

97 """ 

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

99 

100 Arguments: 

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

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

103 """ 

104 # Assert HTTP client is created. 

105 assert async_client 

106 

107 # Create dummy projects. 

108 create_dummy_projects( 

109 tmp_path=tmp_path, 

110 list_of_dummy_project_ids=[ 

111 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

112 ], 

113 ) 

114 

115 # Get texts before test. 

116 response_get_texts_before = await async_client.get( 

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

118 ) 

119 assert response_get_texts_before.status_code == 200 

120 

121 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

122 response_put = await async_client.put( 

123 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/texts/0/rename?" 

124 + urlencode( 

125 {"text_value": "Changement de valeur !"}, 

126 quote_via=quote_plus, 

127 ) 

128 ) 

129 assert response_put.status_code == 403 

130 assert response_put.json() == { 

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

132 } 

133 

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

135 response_get_texts_after = await async_client.get( 

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

137 ) 

138 assert response_get_texts_after.status_code == 200 

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

140 

141 

142# ============================================================================== 

143# test_ko_bad_state_2 

144# ============================================================================== 

145 

146 

147@pytest.mark.asyncio() 

148async def test_ko_bad_state_2(async_client, tmp_path): 

149 """ 

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

151 

152 Arguments: 

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

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

155 """ 

156 # Assert HTTP client is created. 

157 assert async_client 

158 

159 # Create dummy projects. 

160 create_dummy_projects( 

161 tmp_path=tmp_path, 

162 list_of_dummy_project_ids=[ 

163 "1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS", 

164 ], 

165 ) 

166 

167 # Get texts before test. 

168 response_get_texts_before = await async_client.get( 

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

170 ) 

171 assert response_get_texts_before.status_code == 200 

172 

173 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

174 response_put = await async_client.put( 

175 url="/api/projects/1f_ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS/texts/0/rename?" 

176 + urlencode( 

177 {"text_value": "Changement de valeur !"}, 

178 quote_via=quote_plus, 

179 ) 

180 ) 

181 assert response_put.status_code == 403 

182 assert response_put.json() == { 

183 "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').", 

184 } 

185 

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

187 response_get_texts_after = await async_client.get( 

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

189 ) 

190 assert response_get_texts_after.status_code == 200 

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

192 

193 

194# ============================================================================== 

195# test_ok_good_state_1 

196# ============================================================================== 

197 

198 

199@pytest.mark.asyncio() 

200async def test_ok_good_state_1(async_client, tmp_path): 

201 """ 

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

203 

204 Arguments: 

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

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

207 """ 

208 # Assert HTTP client is created. 

209 assert async_client 

210 

211 # Create dummy projects. 

212 create_dummy_projects( 

213 tmp_path=tmp_path, 

214 list_of_dummy_project_ids=[ 

215 "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

216 ], 

217 ) 

218 

219 # Get texts before test. 

220 response_get_texts_before = await async_client.get( 

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

222 ) 

223 assert response_get_texts_before.status_code == 200 

224 assert response_get_texts_before.json()["texts"]["0"]["text"] != "Changement de valeur !" 

225 

226 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

227 response_put = await async_client.put( 

228 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/0/rename?" 

229 + urlencode( 

230 {"text_value": "Changement de valeur !"}, 

231 quote_via=quote_plus, 

232 ) 

233 ) 

234 assert response_put.status_code == 202 

235 assert response_put.json() == { 

236 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

237 "text_id": "0", 

238 "text_value": "Changement de valeur !", 

239 "detail": ( 

240 "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '0' has been renamed." 

241 ), 

242 } 

243 

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

245 response_get_texts_after = await async_client.get( 

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

247 ) 

248 assert response_get_texts_after.status_code == 200 

249 assert response_get_texts_after.json()["texts"]["0"]["text"] == "Changement de valeur !" 

250 

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

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

253 assert response_get_status.status_code == 200 

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

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

256 

257 

258# ============================================================================== 

259# test_ok_good_state_2 

260# ============================================================================== 

261 

262 

263@pytest.mark.asyncio() 

264async def test_ok_good_state_2(async_client, tmp_path): 

265 """ 

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

267 

268 Arguments: 

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

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

271 """ 

272 # Assert HTTP client is created. 

273 assert async_client 

274 

275 # Create dummy projects. 

276 create_dummy_projects( 

277 tmp_path=tmp_path, 

278 list_of_dummy_project_ids=[ 

279 "1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS", 

280 ], 

281 ) 

282 

283 # Get texts before test. 

284 response_get_texts_before = await async_client.get( 

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

286 ) 

287 assert response_get_texts_before.status_code == 200 

288 assert response_get_texts_before.json()["texts"]["0"]["text"] != "Changement de valeur !" 

289 

290 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

291 response_put = await async_client.put( 

292 url="/api/projects/1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS/texts/0/rename?" 

293 + urlencode( 

294 {"text_value": "Changement de valeur !"}, 

295 quote_via=quote_plus, 

296 ) 

297 ) 

298 assert response_put.status_code == 202 

299 assert response_put.json() == { 

300 "project_id": "1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS", 

301 "text_id": "0", 

302 "text_value": "Changement de valeur !", 

303 "detail": "In project with id '1e_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITHOUT_CONFLICTS', the text with id '0' has been renamed.", 

304 } 

305 

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

307 response_get_texts_after = await async_client.get( 

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

309 ) 

310 assert response_get_texts_after.status_code == 200 

311 assert response_get_texts_after.json()["texts"]["0"]["text"] == "Changement de valeur !" 

312 

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

314 response_get_status = await async_client.get( 

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

316 ) 

317 assert response_get_status.status_code == 200 

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

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

320 

321 

322# ============================================================================== 

323# test_ok_good_state_3 

324# ============================================================================== 

325 

326 

327@pytest.mark.asyncio() 

328async def test_ok_good_state_3(async_client, tmp_path): 

329 """ 

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

331 

332 Arguments: 

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

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

335 """ 

336 # Assert HTTP client is created. 

337 assert async_client 

338 

339 # Create dummy projects. 

340 create_dummy_projects( 

341 tmp_path=tmp_path, 

342 list_of_dummy_project_ids=[ 

343 "1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS", 

344 ], 

345 ) 

346 

347 # Get texts before test. 

348 response_get_texts_before = await async_client.get( 

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

350 ) 

351 assert response_get_texts_before.status_code == 200 

352 assert response_get_texts_before.json()["texts"]["0"]["text"] != "Changement de valeur !" 

353 

354 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

355 response_put = await async_client.put( 

356 url="/api/projects/1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS/texts/0/rename?" 

357 + urlencode( 

358 {"text_value": "Changement de valeur !"}, 

359 quote_via=quote_plus, 

360 ) 

361 ) 

362 assert response_put.status_code == 202 

363 assert response_put.json() == { 

364 "project_id": "1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS", 

365 "text_id": "0", 

366 "text_value": "Changement de valeur !", 

367 "detail": "In project with id '1i_ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS', the text with id '0' has been renamed.", 

368 } 

369 

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

371 response_get_texts_after = await async_client.get( 

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

373 ) 

374 assert response_get_texts_after.status_code == 200 

375 assert response_get_texts_after.json()["texts"]["0"]["text"] == "Changement de valeur !" 

376 

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

378 response_get_status = await async_client.get( 

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

380 ) 

381 assert response_get_status.status_code == 200 

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

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

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}/rename` 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"]["0"]["text"] != "Changement de valeur !" 

417 

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

419 response_put1 = await async_client.put( 

420 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/0/rename?" 

421 + urlencode( 

422 {"text_value": "Changement de valeur !"}, 

423 quote_via=quote_plus, 

424 ) 

425 ) 

426 assert response_put1.status_code == 202 

427 assert response_put1.json() == { 

428 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

429 "text_id": "0", 

430 "text_value": "Changement de valeur !", 

431 "detail": ( 

432 "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '0' has been renamed." 

433 ), 

434 } 

435 

436 # Assert route `PUT /api/projects/{project_id}/texts/{text_id}/rename` works. 

437 response_put2 = await async_client.put( 

438 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/texts/0/rename?" 

439 + urlencode( 

440 {"text_value": "Changement de valeur !"}, 

441 quote_via=quote_plus, 

442 ) 

443 ) 

444 assert response_put2.status_code == 202 

445 assert response_put2.json() == { 

446 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

447 "text_id": "0", 

448 "text_value": "Changement de valeur !", 

449 "detail": ( 

450 "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the text with id '0' has been renamed." 

451 ), 

452 } 

453 

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

455 response_get_texts_after = await async_client.get( 

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

457 ) 

458 assert response_get_texts_after.status_code == 200 

459 assert response_get_texts_after.json()["texts"]["0"]["text"] == "Changement de valeur !" 

460 

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

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

463 

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

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

466 assert response_get_status.status_code == 200 

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

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