Coverage for tests\test_put_api_projects_constraints_review.py: 100.00%

76 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_put_api_projects_constraints_review.py 

5* Description: Unittests for `app` module on the `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` 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 pytest 

16 

17from tests.dummies_utils import create_dummy_projects 

18 

19# ============================================================================== 

20# test_ko_not_found_1 

21# ============================================================================== 

22 

23 

24@pytest.mark.asyncio() 

25async def test_ko_not_found_1(async_client): 

26 """ 

27 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` route with not existing project. 

28 

29 Arguments: 

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

31 """ 

32 # Assert HTTP client is created. 

33 assert async_client 

34 

35 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` works. 

36 response_put = await async_client.put(url="/api/projects/UNKNOWN_PROJECT/constraints/UNKNOWN_CONSTRAINT/review") 

37 assert response_put.status_code == 404 

38 assert response_put.json() == { 

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

40 } 

41 

42 

43# ============================================================================== 

44# test_ko_not_found_2 

45# ============================================================================== 

46 

47 

48@pytest.mark.asyncio() 

49async def test_ko_not_found_2(async_client, tmp_path): 

50 """ 

51 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` route with not existing project. 

52 

53 Arguments: 

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

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

56 """ 

57 # Assert HTTP client is created. 

58 assert async_client 

59 

60 # Create dummy projects. 

61 create_dummy_projects( 

62 tmp_path=tmp_path, 

63 list_of_dummy_project_ids=[ 

64 "1a_SAMPLING_TODO", 

65 ], 

66 ) 

67 

68 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` works. 

69 response_put = await async_client.put(url="/api/projects/1a_SAMPLING_TODO/constraints/UNKNOWN_CONSTRAINT/review") 

70 assert response_put.status_code == 404 

71 assert response_put.json() == { 

72 "detail": "In project with id '1a_SAMPLING_TODO', the constraint with id 'UNKNOWN_CONSTRAINT' to annotate doesn't exist.", 

73 } 

74 

75 

76# ============================================================================== 

77# test_ok_good_state_default 

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

79 

80 

81@pytest.mark.asyncio() 

82async def test_ok_good_state_default(async_client, tmp_path): 

83 """ 

84 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` route with good state. 

85 

86 Arguments: 

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

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

89 """ 

90 # Assert HTTP client is created. 

91 assert async_client 

92 

93 # Create dummy projects. 

94 create_dummy_projects( 

95 tmp_path=tmp_path, 

96 list_of_dummy_project_ids=[ 

97 "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

98 ], 

99 ) 

100 

101 # Get texts before test. 

102 response_get_constraints_before = await async_client.get( 

103 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints?without_hidden_constraints=false" 

104 ) 

105 assert response_get_constraints_before.status_code == 200 

106 assert response_get_constraints_before.json()["constraints"]["(0,4)"]["to_review"] is False 

107 

108 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` works. 

109 response_put = await async_client.put( 

110 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints/(0,4)/review" 

111 ) 

112 assert response_put.status_code == 202 

113 assert response_put.json() == { 

114 "project_id": "1d_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

115 "constraint_id": "(0,4)", 

116 "detail": "In project with id '1d_ANNOTATION_WITH_UPTODATE_MODELIZATION', the constraint with id '(0,4)' need a review.", 

117 } 

118 

119 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

120 response_get_constraints_after = await async_client.get( 

121 url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints?without_hidden_constraints=false" 

122 ) 

123 assert response_get_constraints_after.status_code == 200 

124 for constraint_id in response_get_constraints_before.json()["constraints"].keys(): 

125 if constraint_id != "(0,4)": 

126 assert ( 

127 response_get_constraints_after.json()["constraints"][constraint_id] 

128 == response_get_constraints_before.json()["constraints"][constraint_id] 

129 ) 

130 assert response_get_constraints_after.json()["constraints"]["(0,4)"]["to_review"] is True 

131 

132 # Assert route `GET /api/projects/{project_id}/status` is updated. 

133 response_get_status = await async_client.get(url="/api/projects/1d_ANNOTATION_WITH_UPTODATE_MODELIZATION/status") 

134 assert response_get_status.status_code == 200 

135 assert response_get_status.json()["status"]["iteration_id"] == 1 

136 assert response_get_status.json()["status"]["state"] == "ANNOTATION_WITH_UPTODATE_MODELIZATION" 

137 

138 

139# ============================================================================== 

140# test_ok_good_state_review 

141# ============================================================================== 

142 

143 

144@pytest.mark.asyncio() 

145async def test_ok_good_state_review(async_client, tmp_path): 

146 """ 

147 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` route with good state. 

148 

149 Arguments: 

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

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

152 """ 

153 # Assert HTTP client is created. 

154 assert async_client 

155 

156 # Create dummy projects. 

157 create_dummy_projects( 

158 tmp_path=tmp_path, 

159 list_of_dummy_project_ids=[ 

160 "1m_CLUSTERING_TODO", 

161 ], 

162 ) 

163 

164 # Get texts before test. 

165 response_get_constraints_before = await async_client.get( 

166 url="/api/projects/1m_CLUSTERING_TODO/constraints?without_hidden_constraints=false" 

167 ) 

168 assert response_get_constraints_before.status_code == 200 

169 assert response_get_constraints_before.json()["constraints"]["(0,4)"]["to_review"] is False 

170 

171 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` works. 

172 response_put = await async_client.put( 

173 url="/api/projects/1m_CLUSTERING_TODO/constraints/(0,4)/review?to_review=true" 

174 ) 

175 assert response_put.status_code == 202 

176 assert response_put.json() == { 

177 "project_id": "1m_CLUSTERING_TODO", 

178 "constraint_id": "(0,4)", 

179 "detail": "In project with id '1m_CLUSTERING_TODO', the constraint with id '(0,4)' need a review.", 

180 } 

181 

182 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

183 response_get_constraints_after = await async_client.get( 

184 url="/api/projects/1m_CLUSTERING_TODO/constraints?without_hidden_constraints=false" 

185 ) 

186 assert response_get_constraints_after.status_code == 200 

187 for constraint_id in response_get_constraints_before.json()["constraints"].keys(): 

188 if constraint_id != "(0,4)": 

189 assert ( 

190 response_get_constraints_after.json()["constraints"][constraint_id] 

191 == response_get_constraints_before.json()["constraints"][constraint_id] 

192 ) 

193 assert response_get_constraints_after.json()["constraints"]["(0,4)"]["to_review"] is True 

194 

195 # Assert route `GET /api/projects/{project_id}/status` is updated. 

196 response_get_status = await async_client.get(url="/api/projects/1m_CLUSTERING_TODO/status") 

197 assert response_get_status.status_code == 200 

198 assert response_get_status.json()["status"]["iteration_id"] == 1 

199 assert response_get_status.json()["status"]["state"] == "CLUSTERING_TODO" 

200 

201 

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

203# test_ok_good_state_not_review 

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

205 

206 

207@pytest.mark.asyncio() 

208async def test_ok_good_state_not_review(async_client, tmp_path): 

209 """ 

210 Test the `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` route with good state. 

211 

212 Arguments: 

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

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

215 """ 

216 # Assert HTTP client is created. 

217 assert async_client 

218 

219 # Create dummy projects. 

220 create_dummy_projects( 

221 tmp_path=tmp_path, 

222 list_of_dummy_project_ids=[ 

223 "1p_ITERATION_END", 

224 ], 

225 ) 

226 

227 # Get texts before test. 

228 response_get_constraints_before = await async_client.get( 

229 url="/api/projects/1p_ITERATION_END/constraints?without_hidden_constraints=false" 

230 ) 

231 assert response_get_constraints_before.status_code == 200 

232 assert response_get_constraints_before.json()["constraints"]["(4,6)"]["to_review"] is True 

233 

234 # Assert route `PUT /api/projects/{project_id}/constraints/{constraint_id}/review` works. 

235 response_put = await async_client.put(url="/api/projects/1p_ITERATION_END/constraints/(4,6)/review?to_review=false") 

236 assert response_put.status_code == 202 

237 assert response_put.json() == { 

238 "project_id": "1p_ITERATION_END", 

239 "constraint_id": "(4,6)", 

240 "detail": "In project with id '1p_ITERATION_END', the constraint with id '(4,6)' has been reviewed.", 

241 } 

242 

243 # Assert route `GET /api/projects/{project_id}/constraints` is updated. 

244 response_get_constraints_after = await async_client.get( 

245 url="/api/projects/1p_ITERATION_END/constraints?without_hidden_constraints=false" 

246 ) 

247 assert response_get_constraints_after.status_code == 200 

248 for constraint_id in response_get_constraints_before.json()["constraints"].keys(): 

249 if constraint_id != "(4,6)": 

250 assert ( 

251 response_get_constraints_after.json()["constraints"][constraint_id] 

252 == response_get_constraints_before.json()["constraints"][constraint_id] 

253 ) 

254 assert response_get_constraints_after.json()["constraints"]["(4,6)"]["to_review"] is False 

255 

256 # Assert route `GET /api/projects/{project_id}/status` is updated. 

257 response_get_status = await async_client.get(url="/api/projects/1p_ITERATION_END/status") 

258 assert response_get_status.status_code == 200 

259 assert response_get_status.json()["status"]["iteration_id"] == 1 

260 assert response_get_status.json()["status"]["state"] == "ITERATION_END"