Coverage for tests\test_get_api_projects_status.py: 100.00%

36 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_get_api_projects_status.py 

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

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

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_ok_1 

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

48 

49 

50@pytest.mark.asyncio() 

51async def test_ok_1(async_client, tmp_path): 

52 """ 

53 Test the `GET /api/projects/{project_id}/status` route. 

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 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

67 ], 

68 ) 

69 

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

71 response_get = await async_client.get(url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/status") 

72 assert response_get.status_code == 200 

73 assert list(response_get.json().keys()) == ["project_id", "status"] 

74 assert response_get.json() == { 

75 "project_id": "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

76 "status": { 

77 "iteration_id": 0, 

78 "state": "INITIALIZATION_WITHOUT_MODELIZATION", 

79 "task": None, 

80 "state_details": { 

81 "step": "CLUSTERING", 

82 "step_status": "LOCKED", 

83 "modelization_status": "TODO", 

84 "conflict_status": "UNKNOWN", 

85 }, 

86 }, 

87 } 

88 

89 # Assert file content is the same. 

90 with open(tmp_path / "0a_INITIALIZATION_WITHOUT_MODELIZATION" / "status.json", "r") as status_fileobject: 

91 project_status = json.load(status_fileobject) 

92 assert response_get.json()["status"]["iteration_id"] == project_status["iteration_id"] 

93 assert response_get.json()["status"]["state"] == project_status["state"] 

94 assert response_get.json()["status"]["task"] == project_status["task"] 

95 

96 

97# ============================================================================== 

98# test_ok_2 

99# ============================================================================== 

100 

101 

102@pytest.mark.asyncio() 

103async def test_ok_2(async_client, tmp_path): 

104 """ 

105 Test the `GET /api/projects/{project_id}/status` route. 

106 

107 Arguments: 

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

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

110 """ 

111 # Assert HTTP client is created. 

112 assert async_client 

113 

114 # Create dummy projects. 

115 create_dummy_projects( 

116 tmp_path=tmp_path, 

117 list_of_dummy_project_ids=[ 

118 "1b_SAMPLING_PENDING", 

119 ], 

120 ) 

121 

122 # Assert route `GET /api/projects/{project_id}/status` works. 

123 response_get = await async_client.get(url="/api/projects/1b_SAMPLING_PENDING/status") 

124 assert response_get.status_code == 200 

125 assert list(response_get.json().keys()) == ["project_id", "status"] 

126 assert response_get.json() == { 

127 "project_id": "1b_SAMPLING_PENDING", 

128 "status": { 

129 "iteration_id": 1, 

130 "state": "SAMPLING_PENDING", 

131 "task": { 

132 "progression": 1, 

133 "detail": "Waiting for background task allocation...", 

134 }, 

135 "state_details": { 

136 "step": "SAMPLING", 

137 "step_status": "PENDING", 

138 "modelization_status": "UPTODATE", 

139 "conflict_status": "FALSE", 

140 }, 

141 }, 

142 } 

143 

144 # Assert file content is the same. 

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

146 project_status = json.load(status_fileobject) 

147 assert response_get.json()["status"]["iteration_id"] == project_status["iteration_id"] 

148 assert response_get.json()["status"]["state"] == project_status["state"] 

149 assert response_get.json()["status"]["task"] == project_status["task"]