Coverage for tests\test_run_constrained_clustering_task.py: 100.00%

23 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_run_constrained_clustering_task.py 

5* Description: Unittests for the `run_constrained_clustering_task` backgroundtask. 

6* Author: Erwan Schild 

7* Created: 13/12/2021 

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

9""" 

10 

11# ============================================================================== 

12# IMPORT PYTHON DEPENDENCIES 

13# ============================================================================== 

14 

15 

16import json 

17 

18from tests.dummies_utils import create_dummy_projects 

19 

20# ============================================================================== 

21# test_ko_not_found 

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

23 

24 

25def test_ko_not_found(fake_backgroundtasks): 

26 """ 

27 Test the `constrained clustering` task with project not existing. 

28 

29 Args: 

30 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

31 """ 

32 

33 # Run the task. 

34 fake_backgroundtasks.run_constrained_clustering_task( 

35 project_id="UNKNOWN_PROJECT", 

36 ) 

37 

38 

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

40# test_ko_bad_state 

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

42 

43 

44def test_ko_bad_state(fake_backgroundtasks, tmp_path): 

45 """ 

46 Test the `constrained clustering` task with bad state. 

47 

48 Args: 

49 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

50 tmp_path: Pytest fixture: points to a temporary directory. 

51 """ 

52 

53 # Create dummy projects. 

54 create_dummy_projects( 

55 tmp_path=tmp_path, 

56 list_of_dummy_project_ids=[ 

57 "1m_CLUSTERING_TODO", 

58 ], 

59 ) 

60 

61 # Run the task. 

62 fake_backgroundtasks.run_constrained_clustering_task( 

63 project_id="1m_CLUSTERING_TODO", 

64 ) 

65 

66 # Check clustering file content. 

67 with open(tmp_path / "1m_CLUSTERING_TODO" / "clustering.json", "r") as clustering_before_fileobject: 

68 assert json.load(clustering_before_fileobject) == { 

69 "0": { 

70 "0": 0, 

71 "1": 1, 

72 "10": 2, 

73 "11": 1, 

74 "12": 2, 

75 "13": 2, 

76 "14": 2, 

77 "15": 2, 

78 "16": 0, 

79 "17": 0, 

80 "18": 0, 

81 "19": 0, 

82 "2": 0, 

83 "20": 0, 

84 "21": 0, 

85 "22": 1, 

86 "23": 0, 

87 "3": 0, 

88 "4": 1, 

89 "5": 1, 

90 "6": 0, 

91 "7": 0, 

92 "8": 2, 

93 "9": 1, 

94 } 

95 } 

96 

97 # Assert status is still the same. 

98 with open(tmp_path / "1m_CLUSTERING_TODO" / "status.json", "r") as status_after_fileobject: 

99 assert json.load(status_after_fileobject) == {"iteration_id": 1, "state": "CLUSTERING_TODO", "task": None} 

100 

101 # Assert clustering file content is still the same. 

102 with open(tmp_path / "1m_CLUSTERING_TODO" / "clustering.json", "r") as clustering_after_fileobject: 

103 assert json.load(clustering_after_fileobject) == { 

104 "0": { 

105 "0": 0, 

106 "1": 1, 

107 "10": 2, 

108 "11": 1, 

109 "12": 2, 

110 "13": 2, 

111 "14": 2, 

112 "15": 2, 

113 "16": 0, 

114 "17": 0, 

115 "18": 0, 

116 "19": 0, 

117 "2": 0, 

118 "20": 0, 

119 "21": 0, 

120 "22": 1, 

121 "23": 0, 

122 "3": 0, 

123 "4": 1, 

124 "5": 1, 

125 "6": 0, 

126 "7": 0, 

127 "8": 2, 

128 "9": 1, 

129 } 

130 } 

131 

132 

133# ============================================================================== 

134# test_ok 

135# ============================================================================== 

136 

137 

138def test_ok(fake_backgroundtasks, tmp_path): 

139 """ 

140 Test the `constrained clustering` task works. 

141 

142 Args: 

143 fake_backgroundtasks: Fixture providing a backgroundtasks module, declared in `conftest.py`. 

144 tmp_path: Pytest fixture: points to a temporary directory. 

145 """ 

146 

147 # Create dummy projects. 

148 create_dummy_projects( 

149 tmp_path=tmp_path, 

150 list_of_dummy_project_ids=[ 

151 "1n_CLUSTERING_PENDING", 

152 ], 

153 ) 

154 

155 # Check clustering file content. 

156 with open(tmp_path / "1n_CLUSTERING_PENDING" / "clustering.json", "r") as clustering_before_fileobject: 

157 assert json.load(clustering_before_fileobject) == { 

158 "0": { 

159 "0": 0, 

160 "1": 1, 

161 "10": 2, 

162 "11": 1, 

163 "12": 2, 

164 "13": 2, 

165 "14": 2, 

166 "15": 2, 

167 "16": 0, 

168 "17": 0, 

169 "18": 0, 

170 "19": 0, 

171 "2": 0, 

172 "20": 0, 

173 "21": 0, 

174 "22": 1, 

175 "23": 0, 

176 "3": 0, 

177 "4": 1, 

178 "5": 1, 

179 "6": 0, 

180 "7": 0, 

181 "8": 2, 

182 "9": 1, 

183 } 

184 } 

185 

186 # Run the task. 

187 fake_backgroundtasks.run_constrained_clustering_task(project_id="1n_CLUSTERING_PENDING") 

188 

189 # Assert status is updated. 

190 with open(tmp_path / "1n_CLUSTERING_PENDING" / "status.json", "r") as status_after_fileobject: 

191 assert json.load(status_after_fileobject) == {"iteration_id": 1, "state": "ITERATION_END", "task": None} 

192 

193 # Assert clustering file content is updated. 

194 with open(tmp_path / "1n_CLUSTERING_PENDING" / "clustering.json", "r") as clustering_after_fileobject: 

195 assert json.load(clustering_after_fileobject) == { 

196 "0": { 

197 "0": 0, 

198 "1": 1, 

199 "10": 2, 

200 "11": 1, 

201 "12": 2, 

202 "13": 2, 

203 "14": 2, 

204 "15": 2, 

205 "16": 0, 

206 "17": 0, 

207 "18": 0, 

208 "19": 0, 

209 "2": 0, 

210 "20": 0, 

211 "21": 0, 

212 "22": 1, 

213 "23": 0, 

214 "3": 0, 

215 "4": 1, 

216 "5": 1, 

217 "6": 0, 

218 "7": 0, 

219 "8": 2, 

220 "9": 1, 

221 }, 

222 "1": { 

223 "0": 0, 

224 "1": 0, 

225 "10": 0, 

226 "11": 0, 

227 "12": 0, 

228 "13": 0, 

229 "15": 0, 

230 "16": 1, 

231 "17": 2, 

232 "18": 2, 

233 "19": 2, 

234 "2": 0, 

235 "20": 2, 

236 "21": 2, 

237 "22": 2, 

238 "23": 2, 

239 "3": 0, 

240 "4": 0, 

241 "5": 0, 

242 "6": 0, 

243 "7": 0, 

244 "8": 0, 

245 "9": 0, 

246 }, 

247 }