Coverage for tests\test_get_api_projects_sampling.py: 100.00%

68 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-sampling-gui/tests/test_get_api_projects_sampling.py 

5* Description: Unittests for `app` module on the `GET /api/projects/{project_id}/sampling/{iteration_id}` 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 json 

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(async_client): 

28 """ 

29 Test the `GET /api/projects/{project_id}/sampling` 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 `GET /api/projects/{project_id}/sampling` works. 

38 response_get = await async_client.get(url="/api/projects/UNKNOWN_PROJECT/sampling") 

39 assert response_get.status_code == 404 

40 assert response_get.json() == { 

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

42 } 

43 

44 

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

46# test_ko_bad_iteration_id_1 

47# ============================================================================== 

48 

49 

50@pytest.mark.asyncio() 

51async def test_ko_bad_iteration_id_1(async_client, tmp_path): 

52 """ 

53 Test the `GET /api/projects/{project_id}/sampling` route with not existing project. 

54 

55 Arguments: 

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

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

58 """ 

59 # Assert HTTP client is created. 

60 assert async_client 

61 

62 # Create dummy projects. 

63 create_dummy_projects( 

64 tmp_path=tmp_path, 

65 list_of_dummy_project_ids=[ 

66 "0d_CLUSTERING_TODO", 

67 ], 

68 ) 

69 

70 # Assert route `GET /api/projects/{project_id}/sampling` works. 

71 response_get = await async_client.get(url="/api/projects/0d_CLUSTERING_TODO/sampling") 

72 assert response_get.status_code == 403 

73 assert response_get.json() == { 

74 "detail": "The iteration `0` has no sampling step.", 

75 } 

76 

77 

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

79# test_ko_bad_iteration_id_2 

80# ============================================================================== 

81 

82 

83@pytest.mark.asyncio() 

84async def test_ko_bad_iteration_id_2(async_client, tmp_path): 

85 """ 

86 Test the `GET /api/projects/{project_id}/sampling` route with not existing project. 

87 

88 Arguments: 

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

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

91 """ 

92 # Assert HTTP client is created. 

93 assert async_client 

94 

95 # Create dummy projects. 

96 create_dummy_projects( 

97 tmp_path=tmp_path, 

98 list_of_dummy_project_ids=[ 

99 "1a_SAMPLING_TODO", 

100 ], 

101 ) 

102 

103 # Assert route `GET /api/projects/{project_id}/sampling` works. 

104 response_get = await async_client.get(url="/api/projects/1a_SAMPLING_TODO/sampling?iteration_id=0") 

105 assert response_get.status_code == 403 

106 assert response_get.json() == { 

107 "detail": "The iteration `0` has no sampling step.", 

108 } 

109 

110 

111# ============================================================================== 

112# test_ko_bad_iteration_id_3 

113# ============================================================================== 

114 

115 

116@pytest.mark.asyncio() 

117async def test_ko_bad_iteration_id_3(async_client, tmp_path): 

118 """ 

119 Test the `GET /api/projects/{project_id}/sampling` route with not existing project. 

120 

121 Arguments: 

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

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

124 """ 

125 # Assert HTTP client is created. 

126 assert async_client 

127 

128 # Create dummy projects. 

129 create_dummy_projects( 

130 tmp_path=tmp_path, 

131 list_of_dummy_project_ids=[ 

132 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

133 ], 

134 ) 

135 

136 # Assert route `GET /api/projects/{project_id}/sampling` works. 

137 response_get = await async_client.get( 

138 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/sampling?iteration_id=99" 

139 ) 

140 assert response_get.status_code == 404 

141 assert response_get.json() == { 

142 "detail": "The project with id '0a_INITIALIZATION_WITHOUT_MODELIZATION' has no iteration with id '99'.", 

143 } 

144 

145 

146# ============================================================================== 

147# test_ko_bad_iteration_id_4 

148# ============================================================================== 

149 

150 

151@pytest.mark.asyncio() 

152async def test_ko_bad_iteration_id_4(async_client, tmp_path): 

153 """ 

154 Test the `GET /api/projects/{project_id}/sampling` route with not existing project. 

155 

156 Arguments: 

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

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

159 """ 

160 # Assert HTTP client is created. 

161 assert async_client 

162 

163 # Create dummy projects. 

164 create_dummy_projects( 

165 tmp_path=tmp_path, 

166 list_of_dummy_project_ids=[ 

167 "1a_SAMPLING_TODO", 

168 ], 

169 ) 

170 

171 # Assert route `GET /api/projects/{project_id}/sampling` works. 

172 response_get = await async_client.get(url="/api/projects/1a_SAMPLING_TODO/sampling?iteration_id=1") 

173 assert response_get.status_code == 403 

174 assert response_get.json() == { 

175 "detail": "The project with id '1a_SAMPLING_TODO' hasn't completed its sampling step on iteration '1'.", 

176 } 

177 

178 

179# ============================================================================== 

180# test_ok_default_1 

181# ============================================================================== 

182 

183 

184@pytest.mark.asyncio() 

185async def test_ok_default_1(async_client, tmp_path): 

186 """ 

187 Test the `GET /api/projects/{project_id}/sampling` route with some projects. 

188 

189 Arguments: 

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

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

192 """ 

193 # Assert HTTP client is created. 

194 assert async_client 

195 

196 # Create dummy projects. 

197 create_dummy_projects( 

198 tmp_path=tmp_path, 

199 list_of_dummy_project_ids=[ 

200 "2a_SAMPLING_TODO", 

201 ], 

202 ) 

203 

204 # Assert route `GET /api/projects/{project_id}/sampling` works. 

205 response_get = await async_client.get(url="/api/projects/2a_SAMPLING_TODO/sampling") 

206 assert response_get.status_code == 200 

207 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "sampling"] 

208 assert response_get.json() == { 

209 "project_id": "2a_SAMPLING_TODO", 

210 "iteration_id": 1, 

211 "sampling": [ 

212 "(0,4)", 

213 "(12,9)", 

214 "(10,9)", 

215 "(1,2)", 

216 "(14,9)", 

217 "(22,6)", 

218 "(20,22)", 

219 "(1,3)", 

220 "(4,7)", 

221 "(21,22)", 

222 "(11,12)", 

223 "(10,11)", 

224 "(1,6)", 

225 "(2,4)", 

226 "(8,9)", 

227 "(17,22)", 

228 "(19,22)", 

229 "(1,7)", 

230 "(22,8)", 

231 "(18,22)", 

232 "(11,14)", 

233 "(15,16)", 

234 "(13,16)", 

235 "(4,6)", 

236 "(18,8)", 

237 ], 

238 } 

239 

240 # Assert file content is the same. 

241 with open(tmp_path / "2a_SAMPLING_TODO" / "sampling.json", "r") as sampling_fileobject: 

242 assert response_get.json()["sampling"] == json.load(sampling_fileobject)["1"] 

243 

244 

245# ============================================================================== 

246# test_ok_default_2 

247# ============================================================================== 

248 

249 

250@pytest.mark.asyncio() 

251async def test_ok_default_2(async_client, tmp_path): 

252 """ 

253 Test the `GET /api/projects/{project_id}/sampling` route with some projects. 

254 

255 Arguments: 

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

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

258 """ 

259 # Assert HTTP client is created. 

260 assert async_client 

261 

262 # Create dummy projects. 

263 create_dummy_projects( 

264 tmp_path=tmp_path, 

265 list_of_dummy_project_ids=[ 

266 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

267 ], 

268 ) 

269 

270 # Assert route `GET /api/projects/{project_id}/sampling` works. 

271 response_get = await async_client.get(url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/sampling") 

272 assert response_get.status_code == 200 

273 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "sampling"] 

274 assert response_get.json() == { 

275 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

276 "iteration_id": 2, 

277 "sampling": [ 

278 "(10,14)", 

279 "(2,7)", 

280 "(11,9)", 

281 "(18,20)", 

282 "(13,15)", 

283 "(17,23)", 

284 "(10,12)", 

285 "(17,19)", 

286 "(12,14)", 

287 "(0,7)", 

288 "(16,21)", 

289 "(12,13)", 

290 "(16,6)", 

291 "(19,20)", 

292 "(17,21)", 

293 "(21,23)", 

294 "(19,3)", 

295 "(11,5)", 

296 "(16,23)", 

297 "(19,21)", 

298 "(2,3)", 

299 "(10,15)", 

300 "(0,2)", 

301 "(16,18)", 

302 "(10,8)", 

303 "(20,6)", 

304 "(1,5)", 

305 "(18,21)", 

306 "(12,8)", 

307 "(14,8)", 

308 "(20,21)", 

309 "(15,8)", 

310 "(18,19)", 

311 "(19,23)", 

312 "(3,6)", 

313 "(17,18)", 

314 "(12,15)", 

315 "(17,20)", 

316 "(4,5)", 

317 "(22,5)", 

318 "(18,6)", 

319 "(17,3)", 

320 "(10,13)", 

321 "(5,9)", 

322 "(2,6)", 

323 "(11,4)", 

324 "(11,22)", 

325 "(14,15)", 

326 "(13,14)", 

327 "(18,3)", 

328 "(21,3)", 

329 "(20,3)", 

330 "(22,9)", 

331 "(16,3)", 

332 "(13,8)", 

333 "(19,7)", 

334 "(23,3)", 

335 "(19,2)", 

336 "(16,17)", 

337 "(21,6)", 

338 "(21,7)", 

339 "(16,19)", 

340 "(16,7)", 

341 "(18,7)", 

342 "(2,21)", 

343 "(20,7)", 

344 "(16,2)", 

345 "(18,2)", 

346 "(3,7)", 

347 "(23,6)", 

348 "(2,20)", 

349 "(23,7)", 

350 "(1,11)", 

351 "(1,22)", 

352 "(2,23)", 

353 "(16,20)", 

354 "(17,6)", 

355 "(17,7)", 

356 "(1,9)", 

357 "(19,6)", 

358 "(18,23)", 

359 "(17,2)", 

360 "(6,7)", 

361 "(20,23)", 

362 "(0,19)", 

363 "(1,4)", 

364 "(22,4)", 

365 "(0,3)", 

366 "(0,6)", 

367 "(0,16)", 

368 "(0,17)", 

369 "(0,18)", 

370 "(0,20)", 

371 "(0,21)", 

372 "(0,23)", 

373 "(4,9)", 

374 "(3,5)", 

375 "(0,13)", 

376 "(13,5)", 

377 "(12,9)", 

378 "(11,20)", 

379 "(1,23)", 

380 "(2,5)", 

381 "(11,17)", 

382 "(12,3)", 

383 "(21,9)", 

384 "(0,14)", 

385 "(11,18)", 

386 "(13,7)", 

387 "(16,8)", 

388 "(17,5)", 

389 "(1,19)", 

390 "(11,14)", 

391 "(23,4)", 

392 "(1,18)", 

393 "(16,9)", 

394 "(1,2)", 

395 "(10,23)", 

396 "(1,3)", 

397 "(10,17)", 

398 "(13,23)", 

399 "(5,6)", 

400 "(20,22)", 

401 "(0,12)", 

402 "(15,22)", 

403 "(11,23)", 

404 "(13,2)", 

405 "(1,10)", 

406 "(10,22)", 

407 "(13,6)", 

408 "(7,9)", 

409 "(14,7)", 

410 "(14,5)", 

411 "(10,18)", 

412 "(13,16)", 

413 "(15,16)", 

414 "(14,21)", 

415 "(7,8)", 

416 "(18,9)", 

417 "(14,9)", 

418 "(10,19)", 

419 "(1,20)", 

420 "(10,20)", 

421 "(10,16)", 

422 "(14,23)", 

423 "(5,7)", 

424 "(15,9)", 

425 "(15,4)", 

426 "(14,17)", 

427 "(14,2)", 

428 "(12,23)", 

429 "(12,2)", 

430 "(17,4)", 

431 "(5,8)", 

432 "(15,6)", 

433 "(3,4)", 

434 "(15,5)", 

435 "(0,15)", 

436 "(0,9)", 

437 "(10,5)", 

438 "(11,6)", 

439 "(22,8)", 

440 "(2,22)", 

441 "(14,19)", 

442 "(21,22)", 

443 "(18,22)", 

444 "(1,15)", 

445 "(19,4)", 

446 "(1,14)", 

447 "(4,6)", 

448 "(14,6)", 

449 "(13,18)", 

450 "(11,15)", 

451 "(13,22)", 

452 "(14,22)", 

453 "(15,7)", 

454 "(18,4)", 

455 "(12,17)", 

456 "(23,8)", 

457 "(1,7)", 

458 "(20,5)", 

459 "(20,8)", 

460 "(14,20)", 

461 "(11,2)", 

462 "(15,18)", 

463 "(4,7)", 

464 "(1,8)", 

465 "(20,9)", 

466 "(21,4)", 

467 "(15,3)", 

468 "(0,1)", 

469 "(12,6)", 

470 "(13,20)", 

471 "(21,8)", 

472 "(12,7)", 

473 "(1,12)", 

474 "(12,21)", 

475 "(15,23)", 

476 "(15,19)", 

477 "(17,22)", 

478 "(1,17)", 

479 "(11,13)", 

480 "(10,6)", 

481 "(11,21)", 

482 "(10,2)", 

483 "(22,6)", 

484 "(14,18)", 

485 "(15,17)", 

486 "(11,3)", 

487 "(19,5)", 

488 "(8,9)", 

489 "(15,20)", 

490 "(15,21)", 

491 "(12,20)", 

492 "(2,4)", 

493 "(18,8)", 

494 "(0,8)", 

495 "(6,8)", 

496 "(0,11)", 

497 "(10,21)", 

498 "(15,2)", 

499 "(12,18)", 

500 "(13,3)", 

501 "(13,21)", 

502 "(3,8)", 

503 "(0,22)", 

504 "(12,22)", 

505 "(14,16)", 

506 "(1,6)", 

507 "(11,12)", 

508 "(19,8)", 

509 "(2,9)", 

510 "(11,8)", 

511 "(13,17)", 

512 "(23,9)", 

513 "(22,7)", 

514 "(16,22)", 

515 "(0,5)", 

516 "(11,16)", 

517 "(1,16)", 

518 "(10,9)", 

519 "(16,4)", 

520 "(3,9)", 

521 "(0,4)", 

522 "(17,8)", 

523 "(10,4)", 

524 "(10,7)", 

525 "(13,4)", 

526 "(22,3)", 

527 "(14,4)", 

528 "(23,5)", 

529 "(1,13)", 

530 "(18,5)", 

531 "(1,21)", 

532 "(4,8)", 

533 "(19,22)", 

534 "(14,3)", 

535 "(11,7)", 

536 "(10,3)", 

537 "(6,9)", 

538 "(21,5)", 

539 "(12,16)", 

540 "(13,19)", 

541 "(12,19)", 

542 "(2,8)", 

543 "(10,11)", 

544 "(11,19)", 

545 "(13,9)", 

546 "(19,9)", 

547 "(20,4)", 

548 "(12,4)", 

549 "(0,10)", 

550 "(12,5)", 

551 "(16,5)", 

552 "(17,9)", 

553 "(22,23)", 

554 ], 

555 } 

556 

557 # Assert file content is the same. 

558 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "sampling.json", "r") as sampling_fileobject: 

559 assert response_get.json()["sampling"] == json.load(sampling_fileobject)["2"] 

560 

561 

562# ============================================================================== 

563# test_ok_with_iteration_id 

564# ============================================================================== 

565 

566 

567@pytest.mark.asyncio() 

568async def test_ok_with_iteration_id(async_client, tmp_path): 

569 """ 

570 Test the `GET /api/projects/{project_id}/sampling` route with some projects. 

571 

572 Arguments: 

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

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

575 """ 

576 # Assert HTTP client is created. 

577 assert async_client 

578 

579 # Create dummy projects. 

580 create_dummy_projects( 

581 tmp_path=tmp_path, 

582 list_of_dummy_project_ids=[ 

583 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

584 ], 

585 ) 

586 

587 # Assert route `GET /api/projects/{project_id}/sampling` works. 

588 response_get = await async_client.get( 

589 url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/sampling?iteration_id=1" 

590 ) 

591 assert response_get.status_code == 200 

592 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "sampling"] 

593 assert response_get.json() == { 

594 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

595 "iteration_id": 1, 

596 "sampling": [ 

597 "(0,4)", 

598 "(12,9)", 

599 "(10,9)", 

600 "(1,2)", 

601 "(14,9)", 

602 "(22,6)", 

603 "(20,22)", 

604 "(1,3)", 

605 "(4,7)", 

606 "(21,22)", 

607 "(11,12)", 

608 "(10,11)", 

609 "(1,6)", 

610 "(2,4)", 

611 "(8,9)", 

612 "(17,22)", 

613 "(19,22)", 

614 "(1,7)", 

615 "(22,8)", 

616 "(18,22)", 

617 "(11,14)", 

618 "(15,16)", 

619 "(13,16)", 

620 "(4,6)", 

621 "(18,8)", 

622 ], 

623 } 

624 

625 # Assert file content is the same. 

626 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "sampling.json", "r") as sampling_fileobject: 

627 assert response_get.json()["sampling"] == json.load(sampling_fileobject)["1"]