1 import copy

2 import os

3 from typing import Dict, Tuple

4

5 import pytest

6 from pytestqt.qtbot import QtBot

7 from qgis.PyQt.QtCore import Qt

8 from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox

9

10 from grm.dialogs.Climate_data import ClimateDataDialog

11 from grm.lib.Util import MsTitle

12

13# Set environment variables (PyQt 실행용)

14 if os.environ.get("QT_QPA_PLATFORM_PLUGIN_PATH") is None:

15 os.environ[

16 "QT_QPA_PLATFORM_PLUGIN_PATH"

17 ] = r"C:\Program Files\QGIS 3.10\apps\Qt5\plugins"

18 if os.environ.get("PROJ_LIB") is None:

19 os.environ["PROJ_LIB"] = "C:\\PROGRA~1\\QGIS3~1.10\\share\\proj"

20

21

22 class TestClimateDataDialog:

23 """

24 ClimateDataDialog 테스트

25 """

26

27 xmltodict = {

28 "GRMProject": {

29 "ProjectSettings": {

30 "PrecipitationDataFile": "",

31 "PrecipitationInterval_min": "",

32 "TemperatureMaxDataFile": "",

33 "TemperatureMaxInterval_min": "",

34 "TemperatureMinDataFile": "",

35 "TemperatureMinInterval_min": "",

36 "DaytimeLengthDataFile": "",

37 "DaytimeLengthInterval_min": "",

38 "DaytimeHoursRatioDataFile": "",

39 "SolarRadiationDataFile": "",

40 "SolarRadiationInterval_min": "",

41 "SnowPackTemperatureDataFile": "",

42 "SnowPackTemperatureInterval_min": "",

43 }

44 }

45 }

46

47 @classmethod

48 def setup_class(self):

49 MsTitle("ClimateDataDialog")

50

51 @pytest.fixture(scope="class")

52 def monkeyclass(self) -> pytest.MonkeyPatch:

53 """

54 Mocking QMessageBox

55

56 """

57 from _pytest.monkeypatch import MonkeyPatch

58

59 mpatch = MonkeyPatch()

60 mpatch.setattr(QMessageBox, "information", lambda *args: "information")

61 mpatch.setattr(QMessageBox, "warning", lambda *args: "warning")

62 mpatch.setattr(

63 QFileDialog,

64 "getOpenFileName",

65 lambda *args: ("/path/mock/file/test.txt", "txt"),

66 )

67 yield mpatch

68 mpatch.undo()

69

70 def test_default(self, qtbot: QtBot):

71 assert ClimateDataDialog()

72

73 def test_xml_input(self, qtbot: QtBot):

74 with pytest.raises(KeyError):

75 ClimateDataDialog({})

76 with pytest.raises(AttributeError):

77 ClimateDataDialog({"GRMProject": {"ProjectSettings": ""}})

78

79 def test_insert_precipitation(self, qtbot: QtBot, monkeyclass: pytest.MonkeyPatch):

80 """

81 Precipitation 정확한 데이터를 입력했을 시

82 """

83 xmltodict = copy.deepcopy(self.xmltodict)

84 dialog = ClimateDataDialog(xmltodict)

85 qtbot.addWidget(dialog)

86

87 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

88 dialog.ui.spb_PrecipitationInterval_min.setValue(5)

89 dialog.ui.spb_PrecipitationInterval_min.setValue(1)

90 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

91

92 assert (

93 xmltodict["GRMProject"]["ProjectSettings"]["PrecipitationDataFile"]

94 == "/path/mock/file/test.txt"

95 )

96 assert (

97 xmltodict["GRMProject"]["ProjectSettings"]["PrecipitationInterval_min"] == 1

98 )

99

100 def test_not_insert_precipitation_datafile(

101 self, qtbot: QtBot, monkeyclass: pytest.MonkeyPatch

102 ):

103 """

104 Precipitation 데이터 파일을 입력하지 않았을 시

105

106 - 경고창이 떳는지 확인

107 - xmltodict가 변하지 않았는지 확인

108 """

109 xmltodict = copy.deepcopy(self.xmltodict)

110

111 is_warning = False

112

113 def set_warning(*args):

114 nonlocal is_warning

115 is_warning = True

116

117 monkeyclass.setattr(QMessageBox, "warning", set_warning)

118

119 dialog = ClimateDataDialog(xmltodict)

120 qtbot.addWidget(dialog)

121 dialog.ui.spb_PrecipitationInterval_min.setValue(5)

122 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

123

124 assert is_warning

125 assert xmltodict == self.xmltodict

126

127 def test_not_insert_precipitation_interval_min(

128 self, qtbot: QtBot, monkeyclass: pytest.MonkeyPatch

129 ):

130 """

131 Precipitation interval을 입력하지 않았을 시

132

133 - 경고창이 떳는지 확인

134 - xmltodict가 변하지 않았는지 확인

135 """

136 xmltodict = copy.deepcopy(self.xmltodict)

137

138 is_warning = False

139

140 def set_warning(*args):

141 nonlocal is_warning

142 is_warning = True

143

144 monkeyclass.setattr(QMessageBox, "warning", set_warning)

145

146 dialog = ClimateDataDialog(xmltodict)

147 qtbot.addWidget(dialog)

148

149 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

150 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

151

152 assert is_warning

153 assert xmltodict == self.xmltodict

154

155 def test_insert_temperature_max(self, qtbot: QtBot):

156 """

157 TemperatureMax 정확한 데이터를 입력했을 시

158 """

159 xmltodict = copy.deepcopy(self.xmltodict)

160 dialog = ClimateDataDialog(xmltodict)

161 qtbot.addWidget(dialog)

162

163 # 필수 값 입력

164 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

165 dialog.ui.spb_PrecipitationInterval_min.setValue(1)

166

167 qtbot.mouseClick(dialog.ui.load_btn_TemperatureMaxDataFile, Qt.LeftButton)

168 dialog.ui.spb_TemperatureMaxInterval_min.setValue(55)

169 dialog.ui.spb_TemperatureMaxInterval_min.setValue(13)

170 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

171

172 assert (

173 xmltodict["GRMProject"]["ProjectSettings"]["TemperatureMaxDataFile"]

174 == "/path/mock/file/test.txt"

175 )

176 assert (

177 xmltodict["GRMProject"]["ProjectSettings"]["TemperatureMaxInterval_min"]

178 == 13

179 )

180

181 def test_insert_temperature_min(self, qtbot: QtBot):

182 """

183 TemperatureMin 정확한 데이터를 입력했을 시

184 """

185 xmltodict = copy.deepcopy(self.xmltodict)

186 dialog = ClimateDataDialog(xmltodict)

187 qtbot.addWidget(dialog)

188

189 # 필수 값 입력

190 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

191 dialog.ui.spb_PrecipitationInterval_min.setValue(1)

192

193 # TemperatureMin 값 입력

194 qtbot.mouseClick(dialog.ui.load_btn_TemperatureMinDataFile, Qt.LeftButton)

195 dialog.ui.spb_TemperatureMinInterval_min.setValue(55)

196 dialog.ui.spb_TemperatureMinInterval_min.setValue(65)

197 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

198

199 assert (

200 xmltodict["GRMProject"]["ProjectSettings"]["TemperatureMinDataFile"]

201 == "/path/mock/file/test.txt"

202 )

203 assert (

204 xmltodict["GRMProject"]["ProjectSettings"]["TemperatureMinInterval_min"]

205 == 65

206 )

207

208 def test_insert_daytime(self, qtbot: QtBot):

209 """

210 Daytime 정확한 데이터를 입력했을 시

211 """

212

213 xmltodict = copy.deepcopy(self.xmltodict)

214 dialog = ClimateDataDialog(xmltodict)

215 qtbot.addWidget(dialog)

216

217 # 필수 값 입력

218 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

219 dialog.ui.spb_PrecipitationInterval_min.setValue(1)

220

221 # Daytime 값 입력

222 qtbot.mouseClick(dialog.ui.load_btn_DaytimeHoursRatioDataFile, Qt.LeftButton)

223 qtbot.mouseClick(dialog.ui.load_btn_DaytimeLengthDataFile, Qt.LeftButton)

224 dialog.ui.spb_DaytimeLengthInterval_min.setValue(55)

225 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

226

227 assert (

228 xmltodict["GRMProject"]["ProjectSettings"]["DaytimeHoursRatioDataFile"]

229 == "/path/mock/file/test.txt"

230 )

231 assert (

232 xmltodict["GRMProject"]["ProjectSettings"]["DaytimeLengthDataFile"]

233 == "/path/mock/file/test.txt"

234 )

235 assert xmltodict["GRMProject"]["ProjectSettings"]["DaytimeLengthInterval_min"] == 55

236

237 def test_insert_solar_radiation(self, qtbot: QtBot):

238 """

239 SolarRadiation 정확한 데이터를 입력했을 시

240 """

241

242 xmltodict = copy.deepcopy(self.xmltodict)

243 dialog = ClimateDataDialog(xmltodict)

244 qtbot.addWidget(dialog)

245

246 # 필수 값 입력

247 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

248 dialog.ui.spb_PrecipitationInterval_min.setValue(1)

249

250 # SolarRadiation 값 입력

251 qtbot.mouseClick(dialog.ui.load_btn_SolarRadiationDataFile, Qt.LeftButton)

252 dialog.ui.spb_SolarRadiationInterval_min.setValue(55)

253 dialog.ui.spb_SolarRadiationInterval_min.setValue(13)

254 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

255

256 assert (

257 xmltodict["GRMProject"]["ProjectSettings"]["SolarRadiationDataFile"]

258 == "/path/mock/file/test.txt"

259 )

260 assert (

261 xmltodict["GRMProject"]["ProjectSettings"]["SolarRadiationInterval_min"]

262 == 13

263 )

264

265 def test_insert_snow_pack_temperature(self, qtbot: QtBot):

266 """

267 SnowPackTemperature 정확한 데이터를 입력했을 시

268 """

269

270 xmltodict = copy.deepcopy(self.xmltodict)

271 dialog = ClimateDataDialog(xmltodict)

272 qtbot.addWidget(dialog)

273

274 # 필수 값 입력

275 qtbot.mouseClick(dialog.ui.load_btn_PrecipitationDataFile, Qt.LeftButton)

276 dialog.ui.spb_PrecipitationInterval_min.setValue(1)

277

278 # SnowPackTemperature 값 입력

279 qtbot.mouseClick(dialog.ui.load_btn_SnowPackTemperatureDataFile, Qt.LeftButton)

280 dialog.ui.spb_SnowPackTemperatureInterval_min.setValue(55)

281 dialog.ui.spb_SnowPackTemperatureInterval_min.setValue(13)

282 qtbot.mouseClick(dialog.ui.btnOK, Qt.LeftButton)

283

284 assert (

285 xmltodict["GRMProject"]["ProjectSettings"]["SnowPackTemperatureDataFile"]

286 == "/path/mock/file/test.txt"

287 )

288 assert (

289 xmltodict["GRMProject"]["ProjectSettings"][

290 "SnowPackTemperatureInterval_min"

291 ]

292 == 13

293 )