示例#1
0
    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_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)
    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
示例#4
0
    def test_OpenProject_non_existing_study_file(self):
        project_dir = Path(self._testMethodName).with_suffix(".ksp")

        self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))

        DeleteDirectoryIfExisting(project_dir)
        makedirs(project_dir)

        with self.assertRaisesRegex(
                FileNotFoundError,
                'Salome study does not exist in project "test_OpenProject_non_existing_study_file.ksp"'
        ):
            ProjectManager().OpenProject(project_dir)
示例#5
0
    def test_OpenProject_non_existing_plugin_data(self):
        project_dir = Path(self._testMethodName).with_suffix(".ksp")

        self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))

        DeleteDirectoryIfExisting(project_dir)
        makedirs(project_dir)

        salome_study_path = project_dir / "salome_study.hdf"
        salome_study_path.touch()

        with self.assertRaisesRegex(
                FileNotFoundError,
                'Plugin data file does not exist in project "test_OpenProject_non_existing_plugin_data.ksp"'
        ):
            ProjectManager().OpenProject(project_dir)
示例#6
0
    def test_SaveProject_existing_folder(self, mock_save_study, mock_version):
        """this test makes sure that no unrelated files are changed/overwritten"""
        project_name = Path("project_with_kratos")
        project_dir = project_name.with_suffix(".ksp")

        DeleteDirectoryIfExisting(project_dir)
        makedirs(project_dir)

        main_kratos_py_path = project_dir / "MainKratos.py"
        proj_params_json_path = project_dir / "ProjectParameters.json"

        plugin_data_path = project_dir / "plugin_data.json"
        salome_study_path = project_dir / "salome_study.hdf"

        main_kratos_py_path.touch()
        proj_params_json_path.touch()

        _ExecuteTestSaveProject(self, project_name)

        self.assertTrue(mock_version.called)
        self.assertEqual(mock_version.call_count, 1)

        self.assertTrue(mock_save_study.called)
        self.assertEqual(mock_save_study.call_count, 1)

        # make sure the previously existing file wasn't deleted aka the folder wasn't removed and recreated
        self.assertTrue(main_kratos_py_path.is_file())
        self.assertTrue(proj_params_json_path.is_file())
        self.assertTrue(plugin_data_path.is_file())
        self.assertTrue(salome_study_path.is_file())
    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)
示例#8
0
def _ExecuteTestSaveProject(test_case, project_name=None):
    if not project_name:
        project_name = Path(test_case._testMethodName)

    project_dir = project_name.with_suffix(".ksp")

    test_case.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))

    manager = ProjectManager()

    test_case.assertTrue(manager.SaveProject(project_name))

    plugin_data_path = project_dir / "plugin_data.json"

    # check the files were created
    test_case.assertTrue(project_dir.is_dir())
    test_case.assertTrue(plugin_data_path.is_file())
    test_case.assertTrue(project_dir.joinpath("salome_study.hdf").is_file())

    # check content of "plugin_data.json"
    with open(plugin_data_path, 'r') as plugin_data_file:
        plugin_data = json.load(plugin_data_file)

    test_case.assertIn("general", plugin_data)
    test_case.assertIn("groups", plugin_data)
    test_case.assertNotIn(
        "application",
        plugin_data)  # no app was loaded hence this shouldn't exist

    general = plugin_data["general"]
    test_case.assertIn("version_plugin", general)
    test_case.assertIn("version_salome", general)
    test_case.assertIn("creation_time", general)
    test_case.assertIn("operating_system", general)

    return project_name