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_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_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(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_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 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_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_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_string(self): with self.assertRaisesRegex(TypeError, 'Path must be a "pathlib.Path" object!'): salome_study_utilities.SaveStudy("save_study_name")
def test_SaveStudy_empty_input(self): with self.assertRaisesRegex(NameError, 'Path cannot be empty!'): salome_study_utilities.SaveStudy(Path())