Coverage for tests\test_post_api_projects_sampling.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}/sampling` 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}/sampling` 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}/sampling` works. 

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

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}/sampling` 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}/sampling` works. 

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

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 constraints sampling 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}/sampling` 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}/sampling` works. 

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

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 constraints sampling 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}/sampling` 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}/sampling` works. 

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

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 constraints sampling 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_202 

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

159 

160 

161@pytest.mark.asyncio() 

162async def test_ok_1_202(async_client, tmp_path): 

163 """ 

164 Test the `POST /api/projects/{project_id}/sampling` 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 "1a_SAMPLING_TODO", 

178 ], 

179 ) 

180 

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

182 response_post = await async_client.post(url="/api/projects/1a_SAMPLING_TODO/sampling") 

183 assert response_post.status_code == 202 

184 assert response_post.json() == { 

185 "project_id": "1a_SAMPLING_TODO", 

186 "detail": "In project with id '1a_SAMPLING_TODO', the constraints sampling 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/1a_SAMPLING_TODO/status") 

191 assert response_get.status_code == 200 

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

193 assert response_get.json()["status"]["state"] in { 

194 "SAMPLING_PENDING", 

195 "SAMPLING_WORKING", 

196 "ANNOTATION_WITH_UPTODATE_MODELIZATION", 

197 } 

198 if response_get.json()["status"]["state"] in {"SAMPLING_PENDING", "SAMPLING_WORKING"}: # pragma: nocover 

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

200 else: # pragma: nocover 

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

202 

203 

204# ============================================================================== 

205# test_ok_2_202 

206# ============================================================================== 

207 

208 

209@pytest.mark.asyncio() 

210async def test_ok_2_202(async_client, tmp_path): 

211 """ 

212 Test the `POST /api/projects/{project_id}/sampling` route with good state. 

213 

214 Arguments: 

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

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

217 """ 

218 # Assert HTTP client is created. 

219 assert async_client 

220 

221 # Create dummy projects. 

222 create_dummy_projects( 

223 tmp_path=tmp_path, 

224 list_of_dummy_project_ids=[ 

225 "2a_SAMPLING_TODO", 

226 ], 

227 ) 

228 

229 # Assert route `POST /api/projects/{project_id}/sampling` works. 

230 response_post = await async_client.post(url="/api/projects/2a_SAMPLING_TODO/sampling") 

231 assert response_post.status_code == 202 

232 assert response_post.json() == { 

233 "project_id": "2a_SAMPLING_TODO", 

234 "detail": "In project with id '2a_SAMPLING_TODO', the constraints sampling task has been requested and is waiting for a background task.", 

235 } 

236 

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

238 response_get = await async_client.get(url="/api/projects/2a_SAMPLING_TODO/status") 

239 assert response_get.status_code == 200 

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

241 assert response_get.json()["status"]["state"] in { 

242 "SAMPLING_PENDING", 

243 "SAMPLING_WORKING", 

244 "ANNOTATION_WITH_UPTODATE_MODELIZATION", 

245 } 

246 if response_get.json()["status"]["state"] in {"SAMPLING_PENDING", "SAMPLING_WORKING"}: # pragma: nocover 

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

248 else: # pragma: nocover 

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