Coverage for tests\test_run_constraints_sampling_task.py: 100.00%

61 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_run_constraints_sampling_task.py 

5* Description: Unittests for the `run_constraints_sampling_task` background task. 

6* Author: Erwan Schild 

7* Created: 13/12/2021 

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

9""" 

10 

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

12# IMPORT PYTHON DEPENDENCIES 

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

14 

15 

16import json 

17 

18from tests.dummies_utils import create_dummy_projects 

19 

20# ============================================================================== 

21# test_ko_not_found 

22# ============================================================================== 

23 

24 

25def test_ko_not_found(fake_backgroundtasks): 

26 """ 

27 Test the `constraints sampling` task with project not existing. 

28 

29 Args: 

30 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

31 """ 

32 

33 # Run the task. 

34 fake_backgroundtasks.run_constraints_sampling_task(project_id="UNKNOWN_PROJECT") 

35 

36 

37# ============================================================================== 

38# test_ko_bad_state 

39# ============================================================================== 

40 

41 

42def test_ko_bad_state(fake_backgroundtasks, tmp_path): 

43 """ 

44 Test the `constraints sampling` task with bad state. 

45 

46 Args: 

47 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

48 tmp_path: Pytest fixture: points to a temporary directory. 

49 """ 

50 

51 # Create dummy projects. 

52 create_dummy_projects( 

53 tmp_path=tmp_path, 

54 list_of_dummy_project_ids=[ 

55 "1a_SAMPLING_TODO", 

56 ], 

57 ) 

58 

59 # Check sampling file content. 

60 with open(tmp_path / "1a_SAMPLING_TODO" / "sampling.json", "r") as sampling_before_fileobject: 

61 assert json.load(sampling_before_fileobject) == {} # noqa: WPS520 

62 

63 # Run the task. 

64 fake_backgroundtasks.run_constraints_sampling_task(project_id="1a_SAMPLING_TODO") 

65 

66 # Assert status is still the same. 

67 with open(tmp_path / "1a_SAMPLING_TODO" / "status.json", "r") as status_after_fileobject: 

68 assert json.load(status_after_fileobject) == {"iteration_id": 1, "state": "SAMPLING_TODO", "task": None} 

69 

70 # Assert sampling file content is still the same. 

71 with open(tmp_path / "1a_SAMPLING_TODO" / "sampling.json", "r") as sampling_after_fileobject: 

72 assert json.load(sampling_after_fileobject) == {} # noqa: WPS520 

73 

74 

75# ============================================================================== 

76# test_ok_1 

77# ============================================================================== 

78 

79 

80def test_ok_1(fake_backgroundtasks, tmp_path): 

81 """ 

82 Test the `constraints sampling` task works. 

83 

84 Args: 

85 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

86 tmp_path: Pytest fixture: points to a temporary directory. 

87 """ 

88 

89 # Create dummy projects. 

90 create_dummy_projects( 

91 tmp_path=tmp_path, 

92 list_of_dummy_project_ids=[ 

93 "1b_SAMPLING_PENDING", 

94 ], 

95 ) 

96 

97 # Check sampling file content. 

98 with open(tmp_path / "1b_SAMPLING_PENDING" / "sampling.json", "r") as sampling_before_fileobject: 

99 assert json.load(sampling_before_fileobject) == {} # noqa: WPS520 

100 

101 # Run the task. 

102 fake_backgroundtasks.run_constraints_sampling_task(project_id="1b_SAMPLING_PENDING") 

103 

104 # Assert status is updated. 

105 with open(tmp_path / "1b_SAMPLING_PENDING" / "status.json", "r") as status_after_fileobject: 

106 assert json.load(status_after_fileobject) == { 

107 "iteration_id": 1, 

108 "state": "ANNOTATION_WITH_UPTODATE_MODELIZATION", 

109 "task": None, 

110 } 

111 

112 # Assert sampling file content is updated. 

113 with open(tmp_path / "1b_SAMPLING_PENDING" / "sampling.json", "r") as sampling_after_fileobject: 

114 sampling_results = json.load(sampling_after_fileobject) 

115 assert sampling_results == { 

116 "1": [ 

117 "(0,4)", 

118 "(12,9)", 

119 "(10,9)", 

120 "(1,2)", 

121 "(14,9)", 

122 "(22,6)", 

123 "(20,22)", 

124 "(1,3)", 

125 "(4,7)", 

126 "(21,22)", 

127 "(11,12)", 

128 "(10,11)", 

129 "(1,6)", 

130 "(2,4)", 

131 "(8,9)", 

132 "(17,22)", 

133 "(19,22)", 

134 "(1,7)", 

135 "(22,8)", 

136 "(18,22)", 

137 "(11,14)", 

138 "(15,16)", 

139 "(13,16)", 

140 "(4,6)", 

141 "(18,8)", 

142 ] 

143 } 

144 

145 # Assert constraints file content is updated. 

146 with open(tmp_path / "1b_SAMPLING_PENDING" / "constraints.json", "r") as constraints_after_fileobject: 

147 constraints_results = json.load(constraints_after_fileobject) 

148 assert sorted(constraints_results.keys()) == sorted(sampling_results["1"]) 

149 

150 

151# ============================================================================== 

152# test_ok_2 

153# ============================================================================== 

154 

155 

156def test_ok_2(fake_backgroundtasks, tmp_path): 

157 """ 

158 Test the `constraints sampling` task works. 

159 

160 Args: 

161 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

162 tmp_path: Pytest fixture: points to a temporary directory. 

163 """ 

164 

165 # Create dummy projects. 

166 create_dummy_projects( 

167 tmp_path=tmp_path, 

168 list_of_dummy_project_ids=[ 

169 "2b_SAMPLING_PENDING", 

170 ], 

171 ) 

172 

173 # Check sampling file content. 

174 

175 # Assert sampling file content is updated. 

176 with open(tmp_path / "2b_SAMPLING_PENDING" / "sampling.json", "r") as sampling_before_fileobject: 

177 sampling_results_before = json.load(sampling_before_fileobject) 

178 assert sampling_results_before == { 

179 "1": [ 

180 "(0,4)", 

181 "(12,9)", 

182 "(10,9)", 

183 "(1,2)", 

184 "(14,9)", 

185 "(22,6)", 

186 "(20,22)", 

187 "(1,3)", 

188 "(4,7)", 

189 "(21,22)", 

190 "(11,12)", 

191 "(10,11)", 

192 "(1,6)", 

193 "(2,4)", 

194 "(8,9)", 

195 "(17,22)", 

196 "(19,22)", 

197 "(1,7)", 

198 "(22,8)", 

199 "(18,22)", 

200 "(11,14)", 

201 "(15,16)", 

202 "(13,16)", 

203 "(4,6)", 

204 "(18,8)", 

205 ] 

206 } 

207 

208 # Assert constraints file content is updated. 

209 with open(tmp_path / "2b_SAMPLING_PENDING" / "constraints.json", "r") as constraints_before_fileobject: 

210 constraints_results_before = json.load(constraints_before_fileobject) 

211 assert sorted(constraints_results_before.keys()) == sorted(sampling_results_before["1"]) 

212 

213 # Run the task. 

214 fake_backgroundtasks.run_constraints_sampling_task(project_id="2b_SAMPLING_PENDING") 

215 

216 # Assert status is updated. 

217 with open(tmp_path / "2b_SAMPLING_PENDING" / "status.json", "r") as status_after_fileobject: 

218 assert json.load(status_after_fileobject) == { 

219 "iteration_id": 2, 

220 "state": "ANNOTATION_WITH_UPTODATE_MODELIZATION", 

221 "task": None, 

222 } 

223 

224 # Assert sampling file content is updated. 

225 with open(tmp_path / "2b_SAMPLING_PENDING" / "sampling.json", "r") as sampling_after_fileobject: 

226 sampling_results_after = json.load(sampling_after_fileobject) 

227 assert sampling_results_after == { 

228 "1": [ 

229 "(0,4)", 

230 "(12,9)", 

231 "(10,9)", 

232 "(1,2)", 

233 "(14,9)", 

234 "(22,6)", 

235 "(20,22)", 

236 "(1,3)", 

237 "(4,7)", 

238 "(21,22)", 

239 "(11,12)", 

240 "(10,11)", 

241 "(1,6)", 

242 "(2,4)", 

243 "(8,9)", 

244 "(17,22)", 

245 "(19,22)", 

246 "(1,7)", 

247 "(22,8)", 

248 "(18,22)", 

249 "(11,14)", 

250 "(15,16)", 

251 "(13,16)", 

252 "(4,6)", 

253 "(18,8)", 

254 ], 

255 "2": [ 

256 "(13,15)", 

257 "(17,23)", 

258 "(12,13)", 

259 "(10,15)", 

260 "(21,23)", 

261 "(11,5)", 

262 "(1,5)", 

263 "(15,8)", 

264 "(19,23)", 

265 "(12,15)", 

266 "(4,5)", 

267 "(10,13)", 

268 "(5,9)", 

269 "(10,3)", 

270 "(15,9)", 

271 "(13,9)", 

272 "(11,4)", 

273 "(10,4)", 

274 "(13,8)", 

275 "(15,6)", 

276 "(13,6)", 

277 "(11,3)", 

278 "(11,15)", 

279 "(11,13)", 

280 "(12,3)", 

281 "(3,9)", 

282 "(22,23)", 

283 "(1,11)", 

284 "(11,7)", 

285 "(1,12)", 

286 "(1,9)", 

287 "(11,2)", 

288 "(1,10)", 

289 "(12,7)", 

290 "(7,9)", 

291 "(18,23)", 

292 "(10,7)", 

293 "(12,2)", 

294 "(20,23)", 

295 "(2,9)", 

296 "(10,2)", 

297 "(0,11)", 

298 "(0,15)", 

299 "(1,15)", 

300 "(6,8)", 

301 "(6,9)", 

302 "(11,6)", 

303 "(12,6)", 

304 "(15,3)", 

305 "(15,7)", 

306 "(0,5)", 

307 "(0,8)", 

308 "(0,9)", 

309 "(0,10)", 

310 "(0,12)", 

311 "(0,13)", 

312 "(1,8)", 

313 "(1,13)", 

314 "(2,5)", 

315 "(2,8)", 

316 "(3,5)", 

317 "(3,8)", 

318 "(4,8)", 

319 "(4,9)", 

320 "(5,6)", 

321 "(5,7)", 

322 "(5,8)", 

323 "(7,8)", 

324 "(10,6)", 

325 "(12,4)", 

326 "(12,5)", 

327 "(13,2)", 

328 "(13,3)", 

329 "(13,4)", 

330 "(13,5)", 

331 "(13,7)", 

332 "(15,2)", 

333 "(15,4)", 

334 "(15,5)", 

335 "(10,5)", 

336 "(11,23)", 

337 "(15,19)", 

338 "(23,3)", 

339 "(17,5)", 

340 "(20,5)", 

341 "(13,17)", 

342 "(18,5)", 

343 "(13,23)", 

344 "(23,9)", 

345 "(23,5)", 

346 "(23,4)", 

347 "(15,22)", 

348 "(16,8)", 

349 "(19,5)", 

350 "(16,2)", 

351 "(1,23)", 

352 "(21,5)", 

353 "(15,18)", 

354 "(16,21)", 

355 "(22,5)", 

356 "(23,7)", 

357 "(10,16)", 

358 "(0,16)", 

359 "(13,20)", 

360 "(16,23)", 

361 "(16,3)", 

362 "(13,18)", 

363 "(16,6)", 

364 "(2,23)", 

365 "(12,16)", 

366 "(13,19)", 

367 "(23,6)", 

368 "(16,9)", 

369 "(15,20)", 

370 "(16,4)", 

371 "(13,22)", 

372 "(16,19)", 

373 "(15,17)", 

374 "(15,21)", 

375 "(16,20)", 

376 "(12,23)", 

377 "(16,18)", 

378 "(13,21)", 

379 "(23,8)", 

380 "(1,16)", 

381 "(16,5)", 

382 "(16,22)", 

383 "(11,16)", 

384 "(0,23)", 

385 "(16,7)", 

386 "(16,17)", 

387 "(10,23)", 

388 "(15,23)", 

389 ], 

390 } 

391 

392 # Assert constraints file content is updated. 

393 with open(tmp_path / "2b_SAMPLING_PENDING" / "constraints.json", "r") as constraints_after_fileobject: 

394 constraints_results_after = json.load(constraints_after_fileobject) 

395 assert sorted(constraints_results_after.keys()) == sorted( 

396 set(sampling_results_after["1"] + sampling_results_after["2"]) 

397 ) 

398 

399 

400# ============================================================================== 

401# test_ok_3 

402# ============================================================================== 

403 

404 

405def test_ok_3(fake_backgroundtasks, tmp_path): 

406 """ 

407 Test the `constraints sampling` task works. 

408 

409 Args: 

410 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

411 tmp_path: Pytest fixture: points to a temporary directory. 

412 """ 

413 

414 # Create dummy projects. 

415 create_dummy_projects( 

416 tmp_path=tmp_path, 

417 list_of_dummy_project_ids=[ 

418 "2b2_SAMPLING_PENDING", 

419 ], 

420 ) 

421 

422 # Check sampling file content. 

423 with open(tmp_path / "2b2_SAMPLING_PENDING" / "sampling.json", "r") as sampling_before_fileobject: 

424 sampling_results_before = json.load(sampling_before_fileobject) 

425 assert sampling_results_before == { 

426 "1": [ 

427 "(0,4)", 

428 "(12,9)", 

429 "(10,9)", 

430 "(1,2)", 

431 "(14,9)", 

432 "(22,6)", 

433 "(20,22)", 

434 "(1,3)", 

435 "(4,7)", 

436 "(21,22)", 

437 "(11,12)", 

438 "(10,11)", 

439 "(1,6)", 

440 "(2,4)", 

441 "(8,9)", 

442 "(17,22)", 

443 "(19,22)", 

444 "(1,7)", 

445 "(22,8)", 

446 "(18,22)", 

447 "(11,14)", 

448 "(15,16)", 

449 "(13,16)", 

450 "(4,6)", 

451 "(18,8)", 

452 ] 

453 } 

454 

455 # Assert constraints file content is updated. 

456 with open(tmp_path / "2b2_SAMPLING_PENDING" / "constraints.json", "r") as constraints_before_fileobject: 

457 constraints_results_before = json.load(constraints_before_fileobject) 

458 assert sorted(constraints_results_before.keys()) == sorted(sampling_results_before["1"]) 

459 

460 # Run the task. 

461 fake_backgroundtasks.run_constraints_sampling_task(project_id="2b2_SAMPLING_PENDING") 

462 

463 # Assert status is updated. 

464 with open(tmp_path / "2b2_SAMPLING_PENDING" / "status.json", "r") as status_after_fileobject: 

465 assert json.load(status_after_fileobject) == { 

466 "iteration_id": 2, 

467 "state": "ANNOTATION_WITH_UPTODATE_MODELIZATION", 

468 "task": None, 

469 } 

470 

471 # Assert sampling file content is updated. 

472 with open(tmp_path / "2b2_SAMPLING_PENDING" / "sampling.json", "r") as sampling_after_fileobject: 

473 sampling_results_after = json.load(sampling_after_fileobject) 

474 assert sampling_results_after == { 

475 "1": [ 

476 "(0,4)", 

477 "(12,9)", 

478 "(10,9)", 

479 "(1,2)", 

480 "(14,9)", 

481 "(22,6)", 

482 "(20,22)", 

483 "(1,3)", 

484 "(4,7)", 

485 "(21,22)", 

486 "(11,12)", 

487 "(10,11)", 

488 "(1,6)", 

489 "(2,4)", 

490 "(8,9)", 

491 "(17,22)", 

492 "(19,22)", 

493 "(1,7)", 

494 "(22,8)", 

495 "(18,22)", 

496 "(11,14)", 

497 "(15,16)", 

498 "(13,16)", 

499 "(4,6)", 

500 "(18,8)", 

501 ], 

502 "2": [ 

503 "(10,14)", 

504 "(2,7)", 

505 "(11,9)", 

506 "(18,20)", 

507 "(13,15)", 

508 "(17,23)", 

509 "(10,12)", 

510 "(17,19)", 

511 "(12,14)", 

512 "(0,7)", 

513 "(16,21)", 

514 "(12,13)", 

515 "(16,6)", 

516 "(19,20)", 

517 "(17,21)", 

518 "(21,23)", 

519 "(19,3)", 

520 "(11,5)", 

521 "(16,23)", 

522 "(19,21)", 

523 "(2,3)", 

524 "(10,15)", 

525 "(0,2)", 

526 "(16,18)", 

527 "(10,8)", 

528 "(20,6)", 

529 "(1,5)", 

530 "(18,21)", 

531 "(12,8)", 

532 "(14,8)", 

533 "(20,21)", 

534 "(15,8)", 

535 "(18,19)", 

536 "(19,23)", 

537 "(3,6)", 

538 "(17,18)", 

539 "(12,15)", 

540 "(17,20)", 

541 "(4,5)", 

542 "(22,5)", 

543 "(18,6)", 

544 "(17,3)", 

545 "(10,13)", 

546 "(5,9)", 

547 "(2,6)", 

548 "(11,4)", 

549 "(11,22)", 

550 "(14,15)", 

551 "(13,14)", 

552 "(18,3)", 

553 "(21,3)", 

554 "(20,3)", 

555 "(22,9)", 

556 "(16,3)", 

557 "(13,8)", 

558 "(19,7)", 

559 "(23,3)", 

560 "(19,2)", 

561 "(16,17)", 

562 "(21,6)", 

563 "(21,7)", 

564 "(16,19)", 

565 "(16,7)", 

566 "(18,7)", 

567 "(2,21)", 

568 "(20,7)", 

569 "(16,2)", 

570 "(18,2)", 

571 "(3,7)", 

572 "(23,6)", 

573 "(2,20)", 

574 "(23,7)", 

575 "(1,11)", 

576 "(1,22)", 

577 "(2,23)", 

578 "(16,20)", 

579 "(17,6)", 

580 "(17,7)", 

581 "(1,9)", 

582 "(19,6)", 

583 "(18,23)", 

584 "(17,2)", 

585 "(6,7)", 

586 "(20,23)", 

587 "(0,19)", 

588 "(1,4)", 

589 "(22,4)", 

590 "(0,3)", 

591 "(0,6)", 

592 "(0,16)", 

593 "(0,17)", 

594 "(0,18)", 

595 "(0,20)", 

596 "(0,21)", 

597 "(0,23)", 

598 "(4,9)", 

599 "(3,5)", 

600 "(0,13)", 

601 "(13,5)", 

602 "(12,9)", 

603 "(11,20)", 

604 "(1,23)", 

605 "(2,5)", 

606 "(11,17)", 

607 "(12,3)", 

608 "(21,9)", 

609 "(0,14)", 

610 "(11,18)", 

611 "(13,7)", 

612 "(16,8)", 

613 "(17,5)", 

614 "(1,19)", 

615 "(11,14)", 

616 "(23,4)", 

617 "(1,18)", 

618 "(16,9)", 

619 "(1,2)", 

620 "(10,23)", 

621 "(1,3)", 

622 "(10,17)", 

623 "(13,23)", 

624 "(5,6)", 

625 "(20,22)", 

626 "(0,12)", 

627 "(15,22)", 

628 "(11,23)", 

629 "(13,2)", 

630 "(1,10)", 

631 "(10,22)", 

632 "(13,6)", 

633 "(7,9)", 

634 "(14,7)", 

635 "(14,5)", 

636 "(10,18)", 

637 "(13,16)", 

638 "(15,16)", 

639 "(14,21)", 

640 "(7,8)", 

641 "(18,9)", 

642 "(14,9)", 

643 "(10,19)", 

644 "(1,20)", 

645 "(10,20)", 

646 "(10,16)", 

647 "(14,23)", 

648 "(5,7)", 

649 "(15,9)", 

650 "(15,4)", 

651 "(14,17)", 

652 "(14,2)", 

653 "(12,23)", 

654 "(12,2)", 

655 "(17,4)", 

656 "(5,8)", 

657 "(15,6)", 

658 "(3,4)", 

659 "(15,5)", 

660 "(0,15)", 

661 "(0,9)", 

662 "(10,5)", 

663 "(11,6)", 

664 "(22,8)", 

665 "(2,22)", 

666 "(14,19)", 

667 "(21,22)", 

668 "(18,22)", 

669 "(1,15)", 

670 "(19,4)", 

671 "(1,14)", 

672 "(4,6)", 

673 "(14,6)", 

674 "(13,18)", 

675 "(11,15)", 

676 "(13,22)", 

677 "(14,22)", 

678 "(15,7)", 

679 "(18,4)", 

680 "(12,17)", 

681 "(23,8)", 

682 "(1,7)", 

683 "(20,5)", 

684 "(20,8)", 

685 "(14,20)", 

686 "(11,2)", 

687 "(15,18)", 

688 "(4,7)", 

689 "(1,8)", 

690 "(20,9)", 

691 "(21,4)", 

692 "(15,3)", 

693 "(0,1)", 

694 "(12,6)", 

695 "(13,20)", 

696 "(21,8)", 

697 "(12,7)", 

698 "(1,12)", 

699 "(12,21)", 

700 "(15,23)", 

701 "(15,19)", 

702 "(17,22)", 

703 "(1,17)", 

704 "(11,13)", 

705 "(10,6)", 

706 "(11,21)", 

707 "(10,2)", 

708 "(22,6)", 

709 "(14,18)", 

710 "(15,17)", 

711 "(11,3)", 

712 "(19,5)", 

713 "(8,9)", 

714 "(15,20)", 

715 "(15,21)", 

716 "(12,20)", 

717 "(2,4)", 

718 "(18,8)", 

719 "(0,8)", 

720 "(6,8)", 

721 "(0,11)", 

722 "(10,21)", 

723 "(15,2)", 

724 "(12,18)", 

725 "(13,3)", 

726 "(13,21)", 

727 "(3,8)", 

728 "(0,22)", 

729 "(12,22)", 

730 "(14,16)", 

731 "(1,6)", 

732 "(11,12)", 

733 "(19,8)", 

734 "(2,9)", 

735 "(11,8)", 

736 "(13,17)", 

737 "(23,9)", 

738 "(22,7)", 

739 "(16,22)", 

740 "(0,5)", 

741 "(11,16)", 

742 "(1,16)", 

743 "(10,9)", 

744 "(16,4)", 

745 "(3,9)", 

746 "(0,4)", 

747 "(17,8)", 

748 "(10,4)", 

749 "(10,7)", 

750 "(13,4)", 

751 "(22,3)", 

752 "(14,4)", 

753 "(23,5)", 

754 "(1,13)", 

755 "(18,5)", 

756 "(1,21)", 

757 "(4,8)", 

758 "(19,22)", 

759 "(14,3)", 

760 "(11,7)", 

761 "(10,3)", 

762 "(6,9)", 

763 "(21,5)", 

764 "(12,16)", 

765 "(13,19)", 

766 "(12,19)", 

767 "(2,8)", 

768 "(10,11)", 

769 "(11,19)", 

770 "(13,9)", 

771 "(19,9)", 

772 "(20,4)", 

773 "(12,4)", 

774 "(0,10)", 

775 "(12,5)", 

776 "(16,5)", 

777 "(17,9)", 

778 "(22,23)", 

779 ], 

780 } 

781 

782 # Assert constraints file content is updated. 

783 with open(tmp_path / "2b2_SAMPLING_PENDING" / "constraints.json", "r") as constraints_after_fileobject: 

784 constraints_results_after = json.load(constraints_after_fileobject) 

785 assert sorted(constraints_results_after.keys()) == sorted( 

786 set(sampling_results_after["1"] + sampling_results_after["2"]) 

787 )