-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_salome_study_utilities.py
410 lines (309 loc) · 19.5 KB
/
test_salome_study_utilities.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
# _ __ _ ___ _ ___ _ _
# | |/ /_ _ __ _| |_ ___ __/ __| __ _| |___ _ __ ___| _ \ |_ _ __ _(_)_ _
# | ' <| '_/ _` | _/ _ (_-<__ \/ _` | / _ \ ' \/ -_) _/ | || / _` | | ' \
# |_|\_\_| \__,_|\__\___/__/___/\__,_|_\___/_|_|_\___|_| |_|\_,_\__, |_|_||_|
# |___/
# 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 makedirs, listdir
import unittest
from unittest.mock import patch
# plugin imports
from kratos_salome_plugin import salome_study_utilities
# tests imports
from testing_utilities import SalomeTestCase, SalomeTestCaseWithBox, GetTestsPath, DeleteDirectoryIfExisting, DeleteFileIfExisting
# salome imports
import salome
class TestSalomeTestCaseStudyCleaning(SalomeTestCase):
"""test to make sure that the cleaning of studies between tests works correctly"""
# the order of execution is not deterministic, hence we need a flag
already_executed = False
num_objs_in_study = None
def setUp(self):
super().setUp()
# create geometry
O = self.geompy.MakeVertex(0, 0, 0)
OX = self.geompy.MakeVectorDXDYDZ(1, 0, 0)
OY = self.geompy.MakeVectorDXDYDZ(0, 1, 0)
OZ = self.geompy.MakeVectorDXDYDZ(0, 0, 1)
Box_1 = self.geompy.MakeBoxDXDYDZ(200, 200, 200)
self.geompy.addToStudy( O, 'O' )
self.geompy.addToStudy( OX, 'OX' )
self.geompy.addToStudy( OY, 'OY' )
self.geompy.addToStudy( OZ, 'OZ' )
self.geompy.addToStudy( Box_1, 'Box_1' )
# create mesh
from salome.smesh import smeshBuilder
Mesh_1 = self.smesh.Mesh(Box_1)
Regular_1D = Mesh_1.Segment()
Max_Size_1 = Regular_1D.MaxSize(34.641)
MEFISTO_2D = Mesh_1.Triangle(algo=smeshBuilder.MEFISTO)
NETGEN_3D = Mesh_1.Tetrahedron()
isDone = Mesh_1.Compute()
## Set names of Mesh objects
self.smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D')
self.smesh.SetName(NETGEN_3D.GetAlgorithm(), 'NETGEN 3D')
self.smesh.SetName(MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D')
self.smesh.SetName(Max_Size_1, 'Max Size_1')
self.smesh.SetName(Mesh_1.GetMesh(), 'Mesh_1')
def test_1(self):
self.__CheckStudy()
def test_2(self):
self.__CheckStudy()
def __CheckStudy(self):
if TestSalomeTestCaseStudyCleaning.already_executed:
# make sure the number of components is the same!
current_num_objs_in_study = salome_study_utilities.GetNumberOfObjectsInStudy()
# if this check fails it means that the study was not cleaned, leftover objects exist!
self.assertEqual(current_num_objs_in_study, TestSalomeTestCaseStudyCleaning.num_objs_in_study)
else:
TestSalomeTestCaseStudyCleaning.already_executed = True
# if executed for the first time then count the components
TestSalomeTestCaseStudyCleaning.num_objs_in_study = salome_study_utilities.GetNumberOfObjectsInStudy()
class TestSalomeStudyUtilities(SalomeTestCaseWithBox):
def test_GetNumberOfObjectsInComponent(self):
num_components = 0
num_objs_in_comp = []
itcomp = salome.myStudy.NewComponentIterator()
while itcomp.More(): # loop components (e.g. GEOM, SMESH)
num_components += 1
component = itcomp.Value()
num_objs_in_comp.append(salome_study_utilities.GetNumberOfObjectsInComponent(component))
itcomp.Next()
self.assertEqual(num_components, 2)
self.assertListEqual(num_objs_in_comp, [13,67])
def test_GetNumberOfObjectsInStudy(self):
self.assertEqual(salome_study_utilities.GetNumberOfObjectsInStudy(), 80)
salome_study_utilities.ResetStudy()
self.assertEqual(salome_study_utilities.GetNumberOfObjectsInStudy(), 0)
def test_SaveStudy(self):
file_path = Path("my_study_saved.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
DeleteFileIfExisting(file_path) # remove potential leftovers
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='DEBUG') as cm:
save_successful = salome_study_utilities.SaveStudy(file_path)
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'DEBUG:kratos_salome_plugin.salome_study_utilities:Study was saved with path: "{}"'.format(file_path))
self.assertTrue(save_successful)
self.assertTrue(file_path.is_file())
def test_SaveStudy_empty_input(self):
with self.assertRaisesRegex(NameError, 'Path cannot be empty!'):
salome_study_utilities.SaveStudy(Path())
def test_SaveStudy_string(self):
with self.assertRaisesRegex(TypeError, 'Path must be a "pathlib.Path" object!'):
salome_study_utilities.SaveStudy("save_study_name")
def test_SaveStudy_without_suffix(self):
file_path = Path("my_study_saved_without_suffix")
file_path_with_suffix = file_path.with_suffix(".hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path_with_suffix))
DeleteFileIfExisting(file_path_with_suffix) # remove potential leftovers
save_successful = salome_study_utilities.SaveStudy(file_path)
self.assertTrue(save_successful)
self.assertTrue(file_path_with_suffix.is_file())
def test_SaveStudy_fake_failure(self):
file_path = Path("my_study_saved_faked_failure.hdf")
with patch('salome.myStudy.SaveAs', return_value=False):
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='DEBUG') as cm:
salome_study_utilities.SaveStudy(file_path)
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'CRITICAL:kratos_salome_plugin.salome_study_utilities:Study could not be saved with path: "{}"'.format(file_path))
def test_SaveStudy_overwrite_info(self):
file_path = Path("my_study_saved_overwrite.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
DeleteFileIfExisting(file_path) # remove potential leftovers
file_path.touch() # this creates the file that is overwritten
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='DEBUG') as cm:
save_successful = salome_study_utilities.SaveStudy(file_path)
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'DEBUG:kratos_salome_plugin.salome_study_utilities:File "{}" exists already and will be overwritten'.format(file_path))
self.assertEqual(cm.output[1], 'DEBUG:kratos_salome_plugin.salome_study_utilities:Study was saved with path: "{}"'.format(file_path))
self.assertTrue(save_successful)
self.assertTrue(file_path.is_file())
def test_SaveStudy_file_not_created(self):
# make sure that salome actually creates the file. If not log the problem
def SaveAs_do_nothing(*args):
return True
file_path = Path("non_existing_study_file.hdf")
with patch('salome.myStudy.SaveAs', side_effect=SaveAs_do_nothing):
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='CRITICAL') as cm:
salome_study_utilities.SaveStudy(file_path)
self.assertEqual(len(cm.output), 2)
self.assertEqual(cm.output[0], 'CRITICAL:kratos_salome_plugin.salome_study_utilities:Salome sucessfully saved study but study file was not created: "{}"!'.format(file_path))
self.assertEqual(cm.output[1], 'CRITICAL:kratos_salome_plugin.salome_study_utilities:Study could not be saved with path: "{}"'.format(file_path))
def test_SaveStudy_exception(self):
def SaveAs_raising(*args):
raise Exception("random error")
file_path = Path("my_empty_invaid_study_file.hdf")
with patch('salome.myStudy.SaveAs', side_effect=SaveAs_raising):
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='ERROR') as cm:
salome_study_utilities.SaveStudy(file_path)
self.assertEqual(len(cm.output), 2)
self.assertIn('ERROR:kratos_salome_plugin.salome_study_utilities:Exception when saving study:', cm.output[0])
self.assertEqual(cm.output[1], 'CRITICAL:kratos_salome_plugin.salome_study_utilities:Study could not be saved with path: "{}"'.format(file_path))
def test_SaveStudy_in_folder(self):
self.__execute_test_save_study_in_folder()
def test_SaveStudy_in_sub_folder(self):
parent_save_folder_path = GetTestsPath() / "test_SaveStudy_sub_folder"
save_folder_path = parent_save_folder_path / "some_subfolder" / "actual_save_folder"
self.addCleanup(lambda: DeleteDirectoryIfExisting(parent_save_folder_path))
# cleaning potential leftovers
DeleteDirectoryIfExisting(parent_save_folder_path)
# Note: ".hdf" extension is added automatically and folder to be saved in is created
file_name_full_path = save_folder_path / "my_study_test_save"
save_successful = salome_study_utilities.SaveStudy(file_name_full_path)
self.assertTrue(save_successful)
self.assertTrue(save_folder_path.is_dir()) # make sure folder was created
self.assertFalse(file_name_full_path.is_file())
self.assertTrue(file_name_full_path.with_suffix(".hdf").is_file())
self.assertEqual(len(listdir(save_folder_path)), 1) # make sure only one file was created
def test_SaveStudy_in_existing_folder(self):
save_folder_path = GetTestsPath() / "test_SaveStudy_existing_folder"
self.addCleanup(lambda: DeleteDirectoryIfExisting(save_folder_path))
# cleaning potential leftovers
DeleteDirectoryIfExisting(save_folder_path)
makedirs(save_folder_path)
# Note: ".hdf" extension is added automatically and folder to be saved in is created
file_name_full_path = save_folder_path / "my_study_test_save"
save_successful = salome_study_utilities.SaveStudy(file_name_full_path)
self.assertTrue(save_successful)
self.assertTrue(save_folder_path.is_dir()) # make sure folder was created
self.assertFalse(file_name_full_path.is_file())
self.assertTrue(file_name_full_path.with_suffix(".hdf").is_file())
self.assertEqual(len(listdir(save_folder_path)), 1) # make sure only one file was created
def test_SaveStudy_in_existing_sub_folder(self):
parent_save_folder_path = GetTestsPath() / "test_SaveStudy_existing_sub_folder"
save_folder_path = parent_save_folder_path / "some_subfolder" / "actual_save_folder"
self.addCleanup(lambda: DeleteDirectoryIfExisting(parent_save_folder_path))
# cleaning potential leftovers
DeleteDirectoryIfExisting(parent_save_folder_path)
makedirs(save_folder_path)
# Note: ".hdf" extension is added automatically and folder to be saved in is created
file_name_full_path = save_folder_path / "my_study_test_save"
save_successful = salome_study_utilities.SaveStudy(file_name_full_path)
self.assertTrue(save_successful)
self.assertTrue(save_folder_path.is_dir()) # make sure folder was created
self.assertFalse(file_name_full_path.is_file())
self.assertTrue(file_name_full_path.with_suffix(".hdf").is_file())
self.assertEqual(len(listdir(save_folder_path)), 1) # make sure only one file was created
def test_SaveStudy_in_partially_existing_sub_folder(self):
parent_save_folder_path = GetTestsPath() / "test_SaveStudy_partially_existing_sub_folder"
partial_folder_path = parent_save_folder_path / "some_subfolder"
save_folder_path = partial_folder_path / "actual_save_folder"
self.addCleanup(lambda: DeleteDirectoryIfExisting(parent_save_folder_path))
# cleaning potential leftovers
DeleteDirectoryIfExisting(parent_save_folder_path)
makedirs(partial_folder_path)
# Note: ".hdf" extension is added automatically and folder to be saved in is created
file_name_full_path = save_folder_path / "my_study_test_save"
save_successful = salome_study_utilities.SaveStudy(file_name_full_path)
self.assertTrue(save_successful)
self.assertTrue(save_folder_path.is_dir()) # make sure folder was created
self.assertFalse(file_name_full_path.is_file())
self.assertTrue(file_name_full_path.with_suffix(".hdf").is_file())
self.assertEqual(len(listdir(save_folder_path)), 1) # make sure only one file was created
def test_OpenStudy_empty_input(self):
with self.assertRaisesRegex(NameError, 'Path cannot be empty!'):
salome_study_utilities.OpenStudy(Path())
def test_OpenStudy_string(self):
with self.assertRaisesRegex(TypeError, 'Path must be a "pathlib.Path" object!'):
salome_study_utilities.OpenStudy("open_study_name")
def test_OpenStudy_non_existing(self):
with self.assertRaisesRegex(FileNotFoundError, 'File "some_completely_random_non_existin_path" does not exist!'):
salome_study_utilities.OpenStudy(Path("some_completely_random_non_existin_path"))
@patch('salome.myStudy.Open', return_value=True)
@patch('kratos_salome_plugin.salome_study_utilities.IsStudyModified', return_value=False)
@patch('kratos_salome_plugin.salome_study_utilities.GetNumberOfObjectsInStudy', return_value=0)
def test_OpenStudy_warning_logs_wrong_suffix(self, mock_num_objs_study, mock_is_modified, mock_open_study):
file_path = Path("without_suffix")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
file_path.touch()
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='WARNING') as cm:
salome_study_utilities.OpenStudy(file_path)
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'WARNING:kratos_salome_plugin.salome_study_utilities:Opening study from file without ".hdf" extension: "{}"'.format(file_path))
@patch('salome.myStudy.Open', return_value=True)
@patch('kratos_salome_plugin.salome_study_utilities.IsStudyModified', return_value=True)
@patch('kratos_salome_plugin.salome_study_utilities.GetNumberOfObjectsInStudy', return_value=3)
def test_OpenStudy_warning_logs_modified_study(self, mock_num_objs_study, mock_is_modified, mock_open_study):
file_path = Path("my_study_file.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
file_path.touch()
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='WARNING') as cm:
salome_study_utilities.OpenStudy(file_path)
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'WARNING:kratos_salome_plugin.salome_study_utilities:Opening study when current study has unsaved changes')
@patch('salome.myStudy.Open', return_value=True)
@patch('kratos_salome_plugin.salome_study_utilities.IsStudyModified', return_value=True)
@patch('kratos_salome_plugin.salome_study_utilities.GetNumberOfObjectsInStudy', return_value=0)
def test_OpenStudy_warning_logs_modified_but_empty_study(self, mock_num_objs_study, mock_is_modified, mock_open_study):
# if the study is modified but empty it should not give the warning
file_path = Path("my_empty_study_file.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
file_path.touch()
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='DEBUG') as cm:
salome_study_utilities.OpenStudy(file_path)
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'DEBUG:kratos_salome_plugin.salome_study_utilities:Study was openend from path: "{}"'.format(file_path))
def test_OpenStudy_fake_failure(self):
file_path = Path("my_study_open_faked_failure.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
file_path.touch()
with patch('salome.myStudy.Open', return_value=False):
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='CRITICAL') as cm:
salome_study_utilities.OpenStudy(file_path)
self.assertEqual(len(cm.output), 1)
self.assertEqual(cm.output[0], 'CRITICAL:kratos_salome_plugin.salome_study_utilities:Study could not be opened from path: "{}"'.format(file_path))
def test_OpenStudy(self):
num_objs_in_study = salome_study_utilities.GetNumberOfObjectsInStudy()
# this creates the study file
study_file_name = self.__execute_test_save_study_in_folder()
salome_study_utilities.ResetStudy()
self.assertTrue(salome_study_utilities.OpenStudy(study_file_name))
self.assertEqual(num_objs_in_study, salome_study_utilities.GetNumberOfObjectsInStudy(), msg="Number of objects in study has changed!")
def test_OpenStudy_exception(self):
file_path = Path("my_empty_invaid_study_file.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
file_path.touch() # this is of course no vaild study_file
with self.assertLogs('kratos_salome_plugin.salome_study_utilities', level='ERROR') as cm:
salome_study_utilities.OpenStudy(file_path)
self.assertEqual(len(cm.output), 2)
self.assertIn('ERROR:kratos_salome_plugin.salome_study_utilities:Exception when opening study:', cm.output[0])
self.assertEqual(cm.output[1], 'CRITICAL:kratos_salome_plugin.salome_study_utilities:Study could not be opened from path: "{}"'.format(file_path))
def test_ResetStudy(self):
self.assertGreater(salome_study_utilities.GetNumberOfObjectsInStudy(), 0)
salome_study_utilities.ResetStudy()
self.assertEqual(salome_study_utilities.GetNumberOfObjectsInStudy(), 0)
def test_IsStudyModified(self):
# the test-study was never saved hence it should be modified
prop = self.study.GetProperties()
self.assertTrue(prop.IsModified())
self.assertTrue(salome_study_utilities.IsStudyModified())
# now save the study
file_path = Path("my_study_saved_is_modified.hdf")
self.addCleanup(lambda: DeleteFileIfExisting(file_path))
save_successful = salome_study_utilities.SaveStudy(file_path)
self.assertTrue(save_successful)
self.assertFalse(prop.IsModified()) # after saving this should return false
self.assertFalse(salome_study_utilities.IsStudyModified()) # after saving this should return false
def __execute_test_save_study_in_folder(self):
save_folder_path = GetTestsPath() / "test_SaveStudy_folder"
self.addCleanup(lambda: DeleteDirectoryIfExisting(save_folder_path))
# cleaning potential leftovers
DeleteDirectoryIfExisting(save_folder_path)
# Note: ".hdf" extension is added automatically and folder to be saved in is created
file_name_full_path = save_folder_path / "my_study_test_save"
save_successful = salome_study_utilities.SaveStudy(file_name_full_path)
self.assertTrue(save_successful)
self.assertTrue(save_folder_path.is_dir()) # make sure folder was created
self.assertFalse(file_name_full_path.is_file())
self.assertTrue(file_name_full_path.with_suffix(".hdf").is_file())
self.assertEqual(len(listdir(save_folder_path)), 1) # make sure only one file was created
return file_name_full_path.with_suffix(".hdf")
if __name__ == '__main__':
unittest.main()