Coverage for tests\test_post_api_projects_iterations.py: 100.00%

78 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_post_api_projects_iterations.py 

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

16from typing import Any, Dict 

17 

18import pytest 

19 

20from tests.dummies_utils import create_dummy_projects 

21 

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

23# test_ko_not_found 

24# ============================================================================== 

25 

26 

27@pytest.mark.asyncio() 

28async def test_ko_not_found(async_client): 

29 """ 

30 Test the `POST /api/projects/{project_id}/iterations` route with not existing project. 

31 

32 Arguments: 

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

34 """ 

35 # Assert HTTP client is created. 

36 assert async_client 

37 

38 # Assert route `POST /api/projects/{project_id}/iterations` works. 

39 response_post = await async_client.post(url="/api/projects/UNKNOWN_PROJECT/iterations") 

40 assert response_post.status_code == 404 

41 assert response_post.json() == { 

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

43 } 

44 

45 

46# ============================================================================== 

47# test_ko_bad_state_1 

48# ============================================================================== 

49 

50 

51@pytest.mark.asyncio() 

52async def test_ko_bad_state_1(async_client, tmp_path): 

53 """ 

54 Test the `POST /api/projects/{project_id}/iterations` route with bad state. 

55 

56 Arguments: 

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

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

59 """ 

60 # Assert HTTP client is created. 

61 assert async_client 

62 

63 # Create dummy projects. 

64 create_dummy_projects( 

65 tmp_path=tmp_path, 

66 list_of_dummy_project_ids=[ 

67 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

68 ], 

69 ) 

70 

71 # Assert route `POST /api/projects/{project_id}/iterations` works. 

72 response_post = await async_client.post(url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/iterations") 

73 assert response_post.status_code == 403 

74 assert response_post.json() == { 

75 "detail": "The project with id '0a_INITIALIZATION_WITHOUT_MODELIZATION' hasn't completed its clustering step on iteration '0'.", 

76 } 

77 

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

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

80 assert response_get.status_code == 200 

81 assert response_get.json()["status"]["iteration_id"] == 0 

82 assert response_get.json()["status"]["state"] == "INITIALIZATION_WITHOUT_MODELIZATION" 

83 

84 

85# ============================================================================== 

86# test_ko_bad_state_2 

87# ============================================================================== 

88 

89 

90@pytest.mark.asyncio() 

91async def test_ko_bad_state_2(async_client, tmp_path): 

92 """ 

93 Test the `POST /api/projects/{project_id}/iterations` route with bad state. 

94 

95 Arguments: 

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

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

98 """ 

99 # Assert HTTP client is created. 

100 assert async_client 

101 

102 # Create dummy projects. 

103 create_dummy_projects( 

104 tmp_path=tmp_path, 

105 list_of_dummy_project_ids=[ 

106 "1b_SAMPLING_PENDING", 

107 ], 

108 ) 

109 

110 # Assert route `POST /api/projects/{project_id}/iterations` works. 

111 response_post = await async_client.post(url="/api/projects/1b_SAMPLING_PENDING/iterations") 

112 assert response_post.status_code == 403 

113 assert response_post.json() == { 

114 "detail": "The project with id '1b_SAMPLING_PENDING' hasn't completed its clustering step on iteration '1'.", 

115 } 

116 

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

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

119 assert response_get.status_code == 200 

120 assert response_get.json()["status"]["iteration_id"] == 1 

121 assert response_get.json()["status"]["state"] == "SAMPLING_PENDING" 

122 

123 

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

125# test_ko_bad_state_3 

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

127 

128 

129@pytest.mark.asyncio() 

130async def test_ko_bad_state_3(async_client, tmp_path): 

131 """ 

132 Test the `POST /api/projects/{project_id}/iterations` 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 "1o_CLUSTERING_WORKING", 

146 ], 

147 ) 

148 

149 # Assert route `POST /api/projects/{project_id}/iterations` works. 

150 response_post = await async_client.post(url="/api/projects/1o_CLUSTERING_WORKING/iterations") 

151 assert response_post.status_code == 403 

152 assert response_post.json() == { 

153 "detail": "The project with id '1o_CLUSTERING_WORKING' hasn't completed its clustering step on iteration '1'.", 

154 } 

155 

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

157 response_get = await async_client.get(url="/api/projects/1o_CLUSTERING_WORKING/status") 

158 assert response_get.status_code == 200 

159 assert response_get.json()["status"]["iteration_id"] == 1 

160 assert response_get.json()["status"]["state"] == "CLUSTERING_WORKING" 

161 

162 

163# ============================================================================== 

164# test_ok_1 

165# ============================================================================== 

166 

167 

168@pytest.mark.asyncio() 

169async def test_ok_1(async_client, tmp_path): 

170 """ 

171 Test the `POST /api/projects/{project_id}/iterations` route with good state. 

172 

173 Arguments: 

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

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

176 """ 

177 # Assert HTTP client is created. 

178 assert async_client 

179 

180 # Create dummy projects. 

181 create_dummy_projects( 

182 tmp_path=tmp_path, 

183 list_of_dummy_project_ids=[ 

184 "0g_ITERATION_END", 

185 ], 

186 ) 

187 

188 # Assert route `POST /api/projects/{project_id}/iterations` works. 

189 response_post = await async_client.post(url="/api/projects/0g_ITERATION_END/iterations") 

190 assert response_post.status_code == 201 

191 assert response_post.json() == { 

192 "project_id": "0g_ITERATION_END", 

193 "iteration_id": 1, 

194 "detail": "The project with id '0g_ITERATION_END' is now on iteration with id '1'.", 

195 } 

196 

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

198 response_get = await async_client.get(url="/api/projects/0g_ITERATION_END/status") 

199 assert response_get.status_code == 200 

200 assert response_get.json()["status"]["iteration_id"] == 1 

201 assert response_get.json()["status"]["state"] == "SAMPLING_TODO" 

202 

203 # Case of `settings.json`. 

204 with open(tmp_path / "0g_ITERATION_END" / "settings.json", "r") as settings_fileobject: 

205 project_settings: Dict[str, Any] = json.load(settings_fileobject) 

206 assert list(project_settings.keys()) == [ 

207 "0", 

208 "1", 

209 ] 

210 assert list(project_settings["1"].keys()) == [ 

211 "sampling", 

212 "preprocessing", 

213 "vectorization", 

214 "clustering", 

215 ] 

216 assert project_settings["1"]["preprocessing"] == project_settings["0"]["preprocessing"] 

217 assert project_settings["1"]["vectorization"] == project_settings["0"]["vectorization"] 

218 assert project_settings["1"]["clustering"] == project_settings["0"]["clustering"] 

219 

220 

221# ============================================================================== 

222# test_ok_2 

223# ============================================================================== 

224 

225 

226@pytest.mark.asyncio() 

227async def test_ok_2(async_client, tmp_path): 

228 """ 

229 Test the `POST /api/projects/{project_id}/iterations` route with good state. 

230 

231 Arguments: 

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

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

234 """ 

235 # Assert HTTP client is created. 

236 assert async_client 

237 

238 # Create dummy projects. 

239 create_dummy_projects( 

240 tmp_path=tmp_path, 

241 list_of_dummy_project_ids=[ 

242 "1p_ITERATION_END", 

243 ], 

244 ) 

245 

246 # Assert route `POST /api/projects/{project_id}/iterations` works. 

247 response_post = await async_client.post(url="/api/projects/1p_ITERATION_END/iterations") 

248 assert response_post.status_code == 201 

249 assert response_post.json() == { 

250 "project_id": "1p_ITERATION_END", 

251 "iteration_id": 2, 

252 "detail": "The project with id '1p_ITERATION_END' is now on iteration with id '2'.", 

253 } 

254 

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

256 response_get = await async_client.get(url="/api/projects/1p_ITERATION_END/status") 

257 assert response_get.status_code == 200 

258 assert response_get.json()["status"]["iteration_id"] == 2 

259 assert response_get.json()["status"]["state"] == "SAMPLING_TODO" 

260 

261 # Case of `settings.json`. 

262 with open(tmp_path / "1p_ITERATION_END" / "settings.json", "r") as settings_fileobject: 

263 project_settings: Dict[str, Any] = json.load(settings_fileobject) 

264 assert list(project_settings.keys()) == [ 

265 "0", 

266 "1", 

267 "2", 

268 ] 

269 assert list(project_settings["2"].keys()) == [ 

270 "sampling", 

271 "preprocessing", 

272 "vectorization", 

273 "clustering", 

274 ] 

275 assert project_settings["2"] == project_settings["1"]