Coverage for tests\test_post_api_projects_clustering.py: 100.00%

64 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_clustering.py 

5* Description: Unittests for `app` module on the `POST /api/projects/{project_id}/clustering` route. 

6* Author: Erwan Schild 

7* Created: 22/02/2022 

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

9""" 

10 

11import pytest 

12 

13from tests.dummies_utils import create_dummy_projects 

14 

15# ============================================================================== 

16# test_ko_not_found 

17# ============================================================================== 

18 

19 

20@pytest.mark.asyncio() 

21async def test_ko_not_found(async_client): 

22 """ 

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

24 

25 Arguments: 

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

27 """ 

28 # Assert HTTP client is created. 

29 assert async_client 

30 

31 # Assert route `POST /api/projects/{project_id}/clustering` works. 

32 response_post = await async_client.post(url="/api/projects/UNKNOWN_PROJECT/clustering") 

33 assert response_post.status_code == 404 

34 assert response_post.json() == { 

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

36 } 

37 

38 

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

40# test_ko_bad_state_1 

41# ============================================================================== 

42 

43 

44@pytest.mark.asyncio() 

45async def test_ko_bad_state_1(async_client, tmp_path): 

46 """ 

47 Test the `POST /api/projects/{project_id}/clustering` route with bad state. 

48 

49 Arguments: 

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

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

52 """ 

53 # Assert HTTP client is created. 

54 assert async_client 

55 

56 # Create dummy projects. 

57 create_dummy_projects( 

58 tmp_path=tmp_path, 

59 list_of_dummy_project_ids=[ 

60 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

61 ], 

62 ) 

63 

64 # Assert route `POST /api/projects/{project_id}/clustering` works. 

65 response_post = await async_client.post(url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/clustering") 

66 assert response_post.status_code == 403 

67 assert response_post.json() == { 

68 "detail": "The project with id '0a_INITIALIZATION_WITHOUT_MODELIZATION' doesn't allow the preparation of constrained clustering task during this state (state='INITIALIZATION_WITHOUT_MODELIZATION')." 

69 } 

70 

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

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

73 assert response_get.status_code == 200 

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

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

76 

77 

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

79# test_ko_bad_state_2 

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

81 

82 

83@pytest.mark.asyncio() 

84async def test_ko_bad_state_2(async_client, tmp_path): 

85 """ 

86 Test the `POST /api/projects/{project_id}/clustering` route with bad state. 

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 "1b_SAMPLING_PENDING", 

100 ], 

101 ) 

102 

103 # Assert route `POST /api/projects/{project_id}/clustering` works. 

104 response_post = await async_client.post(url="/api/projects/1b_SAMPLING_PENDING/clustering") 

105 assert response_post.status_code == 403 

106 assert response_post.json() == { 

107 "detail": "The project with id '1b_SAMPLING_PENDING' doesn't allow the preparation of constrained clustering task during this state (state='SAMPLING_PENDING')." 

108 } 

109 

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

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

112 assert response_get.status_code == 200 

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

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

115 

116 

117# ============================================================================== 

118# test_ko_bad_state_3 

119# ============================================================================== 

120 

121 

122@pytest.mark.asyncio() 

123async def test_ko_bad_state_3(async_client, tmp_path): 

124 """ 

125 Test the `POST /api/projects/{project_id}/clustering` route with bad state. 

126 

127 Arguments: 

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

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

130 """ 

131 # Assert HTTP client is created. 

132 assert async_client 

133 

134 # Create dummy projects. 

135 create_dummy_projects( 

136 tmp_path=tmp_path, 

137 list_of_dummy_project_ids=[ 

138 "1o_CLUSTERING_WORKING", 

139 ], 

140 ) 

141 

142 # Assert route `POST /api/projects/{project_id}/clustering` works. 

143 response_post = await async_client.post(url="/api/projects/1o_CLUSTERING_WORKING/clustering") 

144 assert response_post.status_code == 403 

145 assert response_post.json() == { 

146 "detail": "The project with id '1o_CLUSTERING_WORKING' doesn't allow the preparation of constrained clustering task during this state (state='CLUSTERING_WORKING')." 

147 } 

148 

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

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

151 assert response_get.status_code == 200 

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

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

154 

155 

156# ============================================================================== 

157# test_ok_1_200 

158# ============================================================================== 

159 

160 

161@pytest.mark.asyncio() 

162async def test_ok_1_200(async_client, tmp_path): 

163 """ 

164 Test the `POST /api/projects/{project_id}/clustering` route with good state. 

165 

166 Arguments: 

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

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

169 """ 

170 # Assert HTTP client is created. 

171 assert async_client 

172 

173 # Create dummy projects. 

174 create_dummy_projects( 

175 tmp_path=tmp_path, 

176 list_of_dummy_project_ids=[ 

177 "0d_CLUSTERING_TODO", 

178 ], 

179 ) 

180 

181 # Assert route `POST /api/projects/{project_id}/clustering` works. 

182 response_post = await async_client.post(url="/api/projects/0d_CLUSTERING_TODO/clustering") 

183 assert response_post.status_code == 202 

184 assert response_post.json() == { 

185 "project_id": "0d_CLUSTERING_TODO", 

186 "detail": "In project with id '0d_CLUSTERING_TODO', the constrained clustering task has been requested and is waiting for a background task.", 

187 } 

188 

189 # Assert route `GET /api/projects/{project_id}/status` is update. 

190 response_get = await async_client.get(url="/api/projects/0d_CLUSTERING_TODO/status") 

191 assert response_get.status_code == 200 

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

193 assert response_get.json()["status"]["state"] in {"CLUSTERING_PENDING", "CLUSTERING_WORKING", "ITERATION_END"} 

194 if response_get.json()["status"]["state"] in {"CLUSTERING_PENDING", "CLUSTERING_WORKING"}: # pragma: nocover 

195 assert response_get.json()["status"]["task"] is not None 

196 else: # pragma: nocover 

197 assert response_get.json()["status"]["task"] is None 

198 

199 

200# ============================================================================== 

201# test_ok_2_200 

202# ============================================================================== 

203 

204 

205@pytest.mark.asyncio() 

206async def test_ok_2_200(async_client, tmp_path): 

207 """ 

208 Test the `POST /api/projects/{project_id}/clustering` route with good state. 

209 

210 Arguments: 

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

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

213 """ 

214 # Assert HTTP client is created. 

215 assert async_client 

216 

217 # Create dummy projects. 

218 create_dummy_projects( 

219 tmp_path=tmp_path, 

220 list_of_dummy_project_ids=[ 

221 "1m_CLUSTERING_TODO", 

222 ], 

223 ) 

224 

225 # Assert route `POST /api/projects/{project_id}/clustering` works. 

226 response_post = await async_client.post(url="/api/projects/1m_CLUSTERING_TODO/clustering") 

227 assert response_post.status_code == 202 

228 assert response_post.json() == { 

229 "project_id": "1m_CLUSTERING_TODO", 

230 "detail": "In project with id '1m_CLUSTERING_TODO', the constrained clustering task has been requested and is waiting for a background task.", 

231 } 

232 

233 # Assert route `GET /api/projects/{project_id}/status` is update. 

234 response_get = await async_client.get(url="/api/projects/1m_CLUSTERING_TODO/status") 

235 assert response_get.status_code == 200 

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

237 assert response_get.json()["status"]["state"] in {"CLUSTERING_PENDING", "CLUSTERING_WORKING", "ITERATION_END"} 

238 if response_get.json()["status"]["state"] in {"CLUSTERING_PENDING", "CLUSTERING_WORKING"}: # pragma: nocover 

239 assert response_get.json()["status"]["task"] is not None 

240 else: # pragma: nocover 

241 assert response_get.json()["status"]["task"] is None