Coverage for tests\test_get_api_projects_clustering.py: 100.00%

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

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

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

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_ko_bad_iteration_id_1 

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

48 

49 

50@pytest.mark.asyncio() 

51async def test_ko_bad_iteration_id_1(async_client, tmp_path): 

52 """ 

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

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

71 response_get = await async_client.get( 

72 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/clustering?iteration_id=99" 

73 ) 

74 assert response_get.status_code == 404 

75 assert response_get.json() == { 

76 "detail": "The project with id '0a_INITIALIZATION_WITHOUT_MODELIZATION' has no iteration with id '99'.", 

77 } 

78 

79 

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

81# test_ko_bad_iteration_id_2 

82# ============================================================================== 

83 

84 

85@pytest.mark.asyncio() 

86async def test_ko_bad_iteration_id_2(async_client, tmp_path): 

87 """ 

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

89 

90 Arguments: 

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

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

93 """ 

94 # Assert HTTP client is created. 

95 assert async_client 

96 

97 # Create dummy projects. 

98 create_dummy_projects( 

99 tmp_path=tmp_path, 

100 list_of_dummy_project_ids=[ 

101 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

102 ], 

103 ) 

104 

105 # Assert route `GET /api/projects/{project_id}/clustering` works. 

106 response_get = await async_client.get( 

107 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/clustering?iteration_id=0" 

108 ) 

109 assert response_get.status_code == 403 

110 assert response_get.json() == { 

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

112 } 

113 

114 

115# ============================================================================== 

116# test_ok_default_1 

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

118 

119 

120@pytest.mark.asyncio() 

121async def test_ok_default_1(async_client, tmp_path): 

122 """ 

123 Test the `GET /api/projects/{project_id}/clustering` route with some projects. 

124 

125 Arguments: 

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

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

128 """ 

129 # Assert HTTP client is created. 

130 assert async_client 

131 

132 # Create dummy projects. 

133 create_dummy_projects( 

134 tmp_path=tmp_path, 

135 list_of_dummy_project_ids=[ 

136 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

137 ], 

138 ) 

139 

140 # Assert route `GET /api/projects/{project_id}/clustering` works. 

141 response_get = await async_client.get(url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/clustering") 

142 assert response_get.status_code == 200 

143 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "clustering"] 

144 assert response_get.json() == { 

145 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

146 "iteration_id": 1, 

147 "clustering": { 

148 "0": 0, 

149 "1": 0, 

150 "10": 0, 

151 "11": 0, 

152 "12": 0, 

153 "13": 0, 

154 "15": 0, 

155 "16": 1, 

156 "17": 2, 

157 "18": 2, 

158 "19": 2, 

159 "2": 0, 

160 "20": 2, 

161 "21": 2, 

162 "22": 2, 

163 "23": 2, 

164 "3": 0, 

165 "4": 0, 

166 "5": 0, 

167 "6": 0, 

168 "7": 0, 

169 "8": 0, 

170 "9": 0, 

171 }, 

172 } 

173 

174 # Assert file content is the same. 

175 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "clustering.json", "r") as clustering_fileobject: 

176 assert response_get.json()["clustering"] == json.load(clustering_fileobject)["1"] 

177 

178 

179# ============================================================================== 

180# test_ok_default_2 

181# ============================================================================== 

182 

183 

184@pytest.mark.asyncio() 

185async def test_ok_default_2(async_client, tmp_path): 

186 """ 

187 Test the `GET /api/projects/{project_id}/clustering` route with some projects. 

188 

189 Arguments: 

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

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

192 """ 

193 # Assert HTTP client is created. 

194 assert async_client 

195 

196 # Create dummy projects. 

197 create_dummy_projects( 

198 tmp_path=tmp_path, 

199 list_of_dummy_project_ids=[ 

200 "1p_ITERATION_END", 

201 ], 

202 ) 

203 

204 # Assert route `GET /api/projects/{project_id}/clustering` works. 

205 response_get = await async_client.get(url="/api/projects/1p_ITERATION_END/clustering") 

206 assert response_get.status_code == 200 

207 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "clustering"] 

208 assert response_get.json() == { 

209 "project_id": "1p_ITERATION_END", 

210 "iteration_id": 1, 

211 "clustering": { 

212 "0": 0, 

213 "1": 0, 

214 "10": 0, 

215 "11": 0, 

216 "12": 0, 

217 "13": 0, 

218 "15": 0, 

219 "16": 1, 

220 "17": 2, 

221 "18": 2, 

222 "19": 2, 

223 "2": 0, 

224 "20": 2, 

225 "21": 2, 

226 "22": 2, 

227 "23": 2, 

228 "3": 0, 

229 "4": 0, 

230 "5": 0, 

231 "6": 0, 

232 "7": 0, 

233 "8": 0, 

234 "9": 0, 

235 }, 

236 } 

237 

238 # Assert file content is the same. 

239 with open(tmp_path / "1p_ITERATION_END" / "clustering.json", "r") as clustering_fileobject: 

240 assert response_get.json()["clustering"] == json.load(clustering_fileobject)["1"] 

241 

242 

243# ============================================================================== 

244# test_ok_with_iteration_id 

245# ============================================================================== 

246 

247 

248@pytest.mark.asyncio() 

249async def test_ok_with_iteration_id(async_client, tmp_path): 

250 """ 

251 Test the `GET /api/projects/{project_id}/clustering` route with some projects. 

252 

253 Arguments: 

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

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

256 """ 

257 # Assert HTTP client is created. 

258 assert async_client 

259 

260 # Create dummy projects. 

261 create_dummy_projects( 

262 tmp_path=tmp_path, 

263 list_of_dummy_project_ids=[ 

264 "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

265 ], 

266 ) 

267 

268 # Assert route `GET /api/projects/{project_id}/clustering` works. 

269 response_get = await async_client.get( 

270 url="/api/projects/2d_ANNOTATION_WITH_UPTODATE_MODELIZATION/clustering?iteration_id=0" 

271 ) 

272 assert response_get.status_code == 200 

273 assert list(response_get.json().keys()) == ["project_id", "iteration_id", "clustering"] 

274 assert response_get.json() == { 

275 "project_id": "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

276 "iteration_id": 0, 

277 "clustering": { 

278 "0": 0, 

279 "1": 1, 

280 "10": 2, 

281 "11": 1, 

282 "12": 2, 

283 "13": 2, 

284 "14": 2, 

285 "15": 2, 

286 "16": 0, 

287 "17": 0, 

288 "18": 0, 

289 "19": 0, 

290 "2": 0, 

291 "20": 0, 

292 "21": 0, 

293 "22": 1, 

294 "23": 0, 

295 "3": 0, 

296 "4": 1, 

297 "5": 1, 

298 "6": 0, 

299 "7": 0, 

300 "8": 2, 

301 "9": 1, 

302 }, 

303 } 

304 

305 # Assert file content is the same. 

306 with open(tmp_path / "2d_ANNOTATION_WITH_UPTODATE_MODELIZATION" / "clustering.json", "r") as clustering_fileobject: 

307 assert response_get.json()["clustering"] == json.load(clustering_fileobject)["0"]