-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_plugin_controller.py
478 lines (355 loc) · 23.4 KB
/
test_plugin_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# _ __ _ ___ _ ___ _ _
# | |/ /_ _ __ _| |_ ___ __/ __| __ _| |___ _ __ ___| _ \ |_ _ __ _(_)_ _
# | ' <| '_/ _` | _/ _ (_-<__ \/ _` | / _ \ ' \/ -_) _/ | || / _` | | ' \
# |_|\_\_| \__,_|\__\___/__/___/\__,_|_\___/_|_|_\___|_| |_|\_,_\__, |_|_||_|
# |___/
# License: BSD License ; see LICENSE
#
# Main authors: Philipp Bucher (https://github.com/philbucher)
#
# set up testing environment (before anything else)
import initialize_testing_environment
# python imports
from pathlib import Path
from os import listdir
import unittest
from unittest.mock import patch
# plugin imports
from kratos_salome_plugin.gui.plugin_controller import PluginController
import kratos_salome_plugin.gui.active_window as active_window
# tests imports
from testing_utilities import QtTestCase, CreateHDFStudyFile, DeleteDirectoryIfExisting, SalomeTestCaseWithBox, skipUnlessPythonVersionIsAtLeast
# qt imports
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest
class TestPluginControllerGUIConnection(QtTestCase):
"""This test checks if the correct functions of the backend are called
Due to the connection of the functions to the gui the methods have to be mocked
before the object is created
"""
def test_file_new(self):
with patch.object(PluginController, '_New') as patch_fct:
controller = PluginController()
controller._main_window.actionNew.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_file_open(self):
with patch.object(PluginController, '_Open') as patch_fct:
controller = PluginController()
controller._main_window.actionOpen.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_file_open_button(self):
with patch.object(PluginController, '_Open') as patch_fct:
controller = PluginController()
QTest.mouseClick(controller._main_window.pushButton_Open, Qt.LeftButton)
self.assertEqual(patch_fct.call_count, 1)
def test_file_save(self):
with patch.object(PluginController, '_Save') as patch_fct:
controller = PluginController()
controller._main_window.actionSave.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_file_save_as(self):
with patch.object(PluginController, '_SaveAs') as patch_fct:
controller = PluginController()
controller._main_window.actionSave_As.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_file_settings(self):
with patch.object(PluginController, '_Settings') as patch_fct:
controller = PluginController()
controller._main_window.actionSettings.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_file_close(self):
with patch.object(PluginController, '_Close') as patch_fct:
controller = PluginController()
controller._main_window.actionClose.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_kratos_groups(self):
with patch.object(PluginController, '_Groups') as patch_fct:
controller = PluginController()
controller._main_window.actionGroups.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_kratos_launch_flowgraph(self):
with patch.object(PluginController, '_LaunchFlowgraph') as patch_fct:
controller = PluginController()
controller._main_window.actionLaunch_Flowgraph.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_kratos_import_mdpa(self):
with patch.object(PluginController, '_ImportMdpa') as patch_fct:
controller = PluginController()
controller._main_window.actionImport_MDPA.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_help_about(self):
with patch('kratos_salome_plugin.gui.plugin_controller.ShowAbout') as patch_fct:
controller = PluginController()
controller._main_window.actionAbout.trigger()
self.assertEqual(patch_fct.call_count, 1)
def test_help_website(self):
with patch('kratos_salome_plugin.gui.plugin_controller.webbrowser') as patch_fct:
controller = PluginController()
controller._main_window.actionWebsite_Plugin.trigger()
self.assertEqual(patch_fct.open.call_count, 1)
controller = PluginController()
controller._main_window.actionWebsite_Kratos.trigger()
self.assertEqual(patch_fct.open.call_count, 2)
controller = PluginController()
controller._main_window.actionWebsite_Flowgraph.trigger()
self.assertEqual(patch_fct.open.call_count, 3)
class TestPluginControllerMainWindowCloseReopen(QtTestCase):
"""This test makes sure if the MainWindow is closed, it is not destroyed"""
def test_main_window_reopen(self):
controller = PluginController()
orig_obj = controller._main_window
controller._main_window.close()
controller._main_window.ShowOnTop()
self.assertIs(orig_obj, controller._main_window)
class TestPluginControllerMainWindow_ActiveWindow(QtTestCase):
def test_main_window_active_window(self):
# setting initial state
active_window.ACTIVE_WINDOW = None
controller = PluginController()
self.assertIs(active_window.ACTIVE_WINDOW, controller._main_window)
# using a module local patch due to import of QFileDialog in project_path_handler
# see https://realpython.com/python-mock-library/#where-to-patch
_QFileDialog_patch = 'kratos_salome_plugin.gui.project_path_handler.QFileDialog.'
@skipUnlessPythonVersionIsAtLeast((3,6), 'pathlib.Path does not work with some fcts before 3.6 (e.g. "with open" or "os.makedirs")')
class TestPluginControllerProject(QtTestCase):
def test_New(self):
controller = PluginController()
controller._previous_save_path = Path("some/path")
initial_project_manager = controller._project_manager
initial_project_path_handler = controller._project_path_handler
controller._New()
# make sure things were cleaned properly
self.assertIsNone(controller._previous_save_path)
self.assertIsNot(initial_project_manager, controller._project_manager)
self.assertIsNot(initial_project_path_handler, controller._project_path_handler)
def test_Close(self):
controller = PluginController()
controller._main_window.ShowOnTop()
self.assertFalse(controller._main_window.isMinimized())
self.assertTrue(controller._main_window.isVisible())
self.assertFalse(controller._main_window.isHidden())
self.assertEqual(controller._main_window.windowState(), Qt.WindowNoState)
controller._Close()
self.assertFalse(controller._main_window.isMinimized())
self.assertFalse(controller._main_window.isVisible())
self.assertTrue(controller._main_window.isHidden())
self.assertEqual(controller._main_window.windowState(), Qt.WindowNoState)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_SaveAs(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_as.ksp")
self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
DeleteDirectoryIfExisting(project_dir) # remove potential leftovers
controller = PluginController()
self.assertIsNone(controller._previous_save_path)
with patch.object(controller._main_window, 'StatusBarInfo') as patch_fct_status_bar:
with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._SaveAs()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saved project under "{}"'.format(project_dir))
self.assertEqual(patch_fct_status_bar.call_count, 1)
self.assertEqual(patch_fct.call_count, 1)
self.assertTrue(project_dir.is_dir())
num_files_after_first_save = len(listdir(project_dir))
self.assertGreater(num_files_after_first_save, 0)
self.assertEqual(controller._previous_save_path, project_dir)
# calling it a second time should ask again for the save-path
controller._SaveAs()
self.assertEqual(patch_fct_status_bar.call_count, 2)
self.assertEqual(patch_fct.call_count, 2)
self.assertTrue(project_dir.is_dir())
self.assertEqual(num_files_after_first_save, len(listdir(project_dir))) # make sure not more files are created
self.assertEqual(controller._previous_save_path, project_dir)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_SaveAs_aborted(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_as_aborted.ksp")
controller = PluginController()
self.assertIsNone(controller._previous_save_path)
with patch.object(controller._project_manager, 'SaveProject') as patch_fct_save_project:
with patch.object(controller._main_window, 'StatusBarWarning') as patch_fct_status_bar:
with patch(_QFileDialog_patch+'getSaveFileName', return_value=("",0)) as patch_fct:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._SaveAs()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving was aborted')
self.assertEqual(patch_fct_save_project.call_count, 0)
self.assertEqual(patch_fct_status_bar.call_count, 1)
self.assertEqual(patch_fct.call_count, 1)
self.assertFalse(project_dir.is_dir())
self.assertIsNone(controller._previous_save_path)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_SaveAs_failed(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_as_failed.ksp")
controller = PluginController()
self.assertIsNone(controller._previous_save_path)
with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct_get_save_path:
with patch.object(controller._project_manager, 'SaveProject', return_value=False) as patch_fct_save_proj:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._SaveAs()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
self.assertEqual(cm.output[1], 'CRITICAL:kratos_salome_plugin.gui.plugin_controller:Failed to save project under "{}"!'.format(project_dir))
self.assertEqual(patch_fct_get_save_path.call_count, 1)
self.assertEqual(patch_fct_save_proj.call_count, 1)
self.assertFalse(project_dir.is_dir())
self.assertIsNone(controller._previous_save_path)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_Save_first_save(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_first.ksp")
self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
DeleteDirectoryIfExisting(project_dir) # remove potential leftovers
controller = PluginController()
self.assertIsNone(controller._previous_save_path)
with patch.object(controller._main_window, 'StatusBarInfo') as patch_fct_status_bar:
with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Save()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saved project under "{}"'.format(project_dir))
self.assertEqual(patch_fct_status_bar.call_count, 1)
self.assertEqual(patch_fct.call_count, 1)
self.assertTrue(project_dir.is_dir())
num_files_after_first_save = len(listdir(project_dir))
self.assertGreater(num_files_after_first_save, 0)
self.assertEqual(controller._previous_save_path, project_dir)
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Save()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project with previous save path ...')
self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saved project under "{}"'.format(project_dir))
# calling Save a second time should not ask again for the save-path
self.assertEqual(patch_fct_status_bar.call_count, 2)
self.assertEqual(patch_fct.call_count, 1)
self.assertTrue(project_dir.is_dir())
self.assertEqual(num_files_after_first_save, len(listdir(project_dir))) # make sure not more files are created
self.assertEqual(controller._previous_save_path, project_dir)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_Save_second_save(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_second.ksp")
self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
DeleteDirectoryIfExisting(project_dir) # remove potential leftovers
controller = PluginController()
controller._previous_save_path = project_dir
with patch.object(controller._main_window, 'StatusBarInfo') as patch_fct_status_bar:
with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Save()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project with previous save path ...')
self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saved project under "{}"'.format(project_dir))
self.assertEqual(patch_fct_status_bar.call_count, 1)
self.assertEqual(patch_fct.call_count, 0) # should not be called as previous save path is used
self.assertTrue(project_dir.is_dir())
self.assertEqual(controller._previous_save_path, project_dir)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_Save_aborted(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_aborted.ksp")
controller = PluginController()
self.assertIsNone(controller._previous_save_path)
with patch.object(controller._project_manager, 'SaveProject') as patch_fct_save_project:
with patch.object(controller._main_window, 'StatusBarWarning') as patch_fct_status_bar:
with patch(_QFileDialog_patch+'getSaveFileName', return_value=("",0)) as patch_fct:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Save()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving was aborted')
self.assertEqual(patch_fct_save_project.call_count, 0)
self.assertEqual(patch_fct_status_bar.call_count, 1)
self.assertEqual(patch_fct.call_count, 1)
self.assertFalse(project_dir.is_dir())
self.assertIsNone(controller._previous_save_path)
@patch('salome_version.getVersions', return_value=[1,2,3])
@patch('salome.myStudy.SaveAs', side_effect=CreateHDFStudyFile)
def test_Save_failed(self, mock_save_study, mock_version):
project_dir = Path("controller_save_project_failed.ksp")
controller = PluginController()
controller._previous_save_path = project_dir
with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct_get_save_path:
with patch.object(controller._project_manager, 'SaveProject', return_value=False) as patch_fct_save_proj:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Save()
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project with previous save path ...')
self.assertEqual(cm.output[1], 'CRITICAL:kratos_salome_plugin.gui.plugin_controller:Failed to save project under "{}"!'.format(project_dir))
self.assertEqual(patch_fct_get_save_path.call_count, 0)
self.assertEqual(patch_fct_save_proj.call_count, 1)
self.assertFalse(project_dir.is_dir())
self.assertEqual(controller._previous_save_path, project_dir)
def test_Open(self):
controller = PluginController()
project_dir = Path("controller_open_project.ksp")
with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=project_dir) as patch_fct_get_open_path:
with patch.object(controller._project_manager, 'OpenProject', return_value=True) as patch_fct_open_proj:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Open()
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Successfully opened project from "{}"'.format(project_dir))
self.assertEqual(patch_fct_get_open_path.call_count, 1)
self.assertEqual(patch_fct_open_proj.call_count, 1)
def test_Open_invalid_folder(self):
controller = PluginController()
with patch(_QFileDialog_patch+'getExistingDirectory', return_value=str("random_folder")) as patch_fct:
with patch.object(controller._project_manager, 'OpenProject', return_value=True) as patch_fct_open_proj:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Open()
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'WARNING:kratos_salome_plugin.gui.plugin_controller:User input error while opening project: Invalid project folder selected, must end with ".ksp"!')
self.assertEqual(patch_fct.call_count, 1)
self.assertEqual(patch_fct_open_proj.call_count, 0)
def test_Open_aborted(self):
controller = PluginController()
with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=Path(".")) as patch_fct_get_open_path:
with patch.object(controller._project_manager, 'OpenProject', return_value=True) as patch_fct_open_proj:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Open()
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Opening was aborted')
self.assertEqual(patch_fct_get_open_path.call_count, 1)
self.assertEqual(patch_fct_open_proj.call_count, 0)
def test_Open_failed(self):
controller = PluginController()
project_dir = Path("controller_open_project_failed.ksp")
with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=project_dir) as patch_fct_get_open_path:
with patch.object(controller._project_manager, 'OpenProject', return_value=False) as patch_fct_open_proj:
with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
controller._Open()
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'CRITICAL:kratos_salome_plugin.gui.plugin_controller:Failed to open project from "{}"!'.format(project_dir))
self.assertEqual(patch_fct_get_open_path.call_count, 1)
self.assertEqual(patch_fct_open_proj.call_count, 1)
class PluginControllerIntegationTests(SalomeTestCaseWithBox):
"""these tests make sure the complete workflow is working"""
def test_Save(self):
project_dir = Path("controller_save_project_salome.ksp")
self.__execute_test_save(project_dir)
def test_SaveAndReOpen(self):
# imported here due to patching issues
from kratos_salome_plugin.salome_study_utilities import GetNumberOfObjectsInStudy, ResetStudy
project_dir = Path("controller_save_open_project_salome.ksp")
initial_num_objs = GetNumberOfObjectsInStudy()
self.__execute_test_save(project_dir)
ResetStudy()
controller = PluginController()
with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=project_dir) as patch_fct:
controller._Open()
self.assertEqual(GetNumberOfObjectsInStudy(), initial_num_objs)
def __execute_test_save(self, project_dir):
self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
DeleteDirectoryIfExisting(project_dir) # remove potential leftovers
controller = PluginController()
with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
controller._Save()
self.assertTrue(project_dir.is_dir())
self.assertGreater(len(listdir(project_dir)), 0) # make sure sth was created
if __name__ == '__main__':
unittest.main()