From 101deefc642fd4059f7a39c86c964eee359eef07 Mon Sep 17 00:00:00 2001 From: Silke Schomann Date: Thu, 5 Dec 2024 10:12:52 +0000 Subject: [PATCH] Connected MSliceADSObserver and added tests --- src/mslice/models/mslice_ads_observer.py | 20 +------- .../workspacemanager/workspace_provider.py | 4 ++ .../presenters/workspace_manager_presenter.py | 21 ++++++++- tests/workspacemanager_presenter_test.py | 47 ++++++++++++++++++- 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/mslice/models/mslice_ads_observer.py b/src/mslice/models/mslice_ads_observer.py index 91183c46c..649800127 100644 --- a/src/mslice/models/mslice_ads_observer.py +++ b/src/mslice/models/mslice_ads_observer.py @@ -23,16 +23,14 @@ def wrapper(*args, **kwargs): class MSliceADSObserver(AnalysisDataServiceObserver): - def __init__(self, delete_callback, clear_callback, replace_callback, rename_callback): + def __init__(self, delete_callback, clear_callback, rename_callback): super(MSliceADSObserver, self).__init__() self.delete_callback = delete_callback self.clear_callback = clear_callback - self.replace_callback = replace_callback self.rename_callback = rename_callback self.observeDelete(True) self.observeRename(True) - self.observeReplace(True) self.observeClear(True) @_catch_exceptions @@ -59,19 +57,3 @@ def clearHandle(self): Called when the ADS has been cleared, removes all data. """ self.clear_callback() - - @_catch_exceptions - def replaceHandle(self, name, workspace): - """ - Called when the ADS has replaced a workspace with one of the same name. - Updates the workspace stored in the dict. - :param name: The name of the workspace. - :param workspace: A reference to the new workspace - """ - self.replace_callback(name, workspace) - - def unsubscribe(self): - self.observeDelete(False) - self.observeRename(False) - self.observeClear(False) - self.observeReplace(False) diff --git a/src/mslice/models/workspacemanager/workspace_provider.py b/src/mslice/models/workspacemanager/workspace_provider.py index dc16e31e4..e03348812 100644 --- a/src/mslice/models/workspacemanager/workspace_provider.py +++ b/src/mslice/models/workspacemanager/workspace_provider.py @@ -36,6 +36,10 @@ def get_visible_workspace_names(): return [key for key in iterkeys(_loaded_workspaces) if key[:2] != '__'] +def get_workspace_names(): + return [key for key in iterkeys(_loaded_workspaces)] + + def get_workspace_name(workspace): """Returns the name of a workspace given the workspace handle""" if isinstance(workspace, string_types): diff --git a/src/mslice/presenters/workspace_manager_presenter.py b/src/mslice/presenters/workspace_manager_presenter.py index daa06f003..edea692ec 100644 --- a/src/mslice/presenters/workspace_manager_presenter.py +++ b/src/mslice/presenters/workspace_manager_presenter.py @@ -4,13 +4,15 @@ from .busy import show_busy from mslice.widgets.workspacemanager.command import Command from mslice.widgets.workspacemanager import TAB_2D, TAB_NONPSD +from mslice.models.mslice_ads_observer import MSliceADSObserver from mslice.models.workspacemanager.file_io import get_save_directory from mslice.models.workspacemanager.workspace_algorithms import (save_workspaces, export_workspace_to_ads, subtract, is_pixel_workspace, combine_workspace, add_workspace_runs, scale_workspaces, remove_workspace_from_ads) from mslice.models.workspacemanager.workspace_provider import (get_workspace_handle, get_visible_workspace_names, - get_workspace_name, delete_workspace, rename_workspace) + get_workspace_names, get_workspace_name, + delete_workspace, rename_workspace) from .interfaces.workspace_manager_presenter import WorkspaceManagerPresenterInterface from .interfaces.main_presenter import MainPresenterInterface from .validation_decorators import require_main_presenter @@ -38,6 +40,7 @@ def __init__(self, workspace_view): lambda: self._workspace_manager_view._display_error('Please select a Compose action from the dropdown menu'), Command.Scale: self._scale_workspace, Command.Bose: lambda: self._scale_workspace(is_bose=True)} + self._ads_observer = MSliceADSObserver(self.delete_handle, self.clear_handle, self.rename_handle) def register_master(self, main_presenter): assert (isinstance(main_presenter, MainPresenterInterface)) @@ -228,3 +231,19 @@ def update_displayed_workspaces(self): def _clear_displayed_error(self): self._workspace_manager_view.clear_displayed_error() + + def delete_handle(self, workspace): + delete_workspace(workspace) + self.update_displayed_workspaces() + + def clear_handle(self): + for workspace in get_workspace_names(): + delete_workspace(workspace) + self.update_displayed_workspaces() + + def rename_handle(self, workspace, new_name): + if new_name is None: + return + if workspace in get_visible_workspace_names(): + rename_workspace(workspace, new_name) + self.update_displayed_workspaces() diff --git a/tests/workspacemanager_presenter_test.py b/tests/workspacemanager_presenter_test.py index 3030db562..6b127a6f4 100644 --- a/tests/workspacemanager_presenter_test.py +++ b/tests/workspacemanager_presenter_test.py @@ -2,9 +2,13 @@ import unittest import mock -from mock import call, patch +from mock import call, patch, MagicMock import numpy as np +from mantid.api import AnalysisDataService +from mantid.simpleapi import RenameWorkspace + +from mslice.models.mslice_ads_observer import MSliceADSObserver from mslice.presenters.interfaces.main_presenter import MainPresenterInterface from mslice.presenters.workspace_manager_presenter import WorkspaceManagerPresenter from mslice.views.interfaces.mainview import MainView @@ -12,7 +16,7 @@ from mslice.widgets.workspacemanager.command import Command from mslice.widgets.workspacemanager import TAB_2D, TAB_NONPSD from mslice.util.mantid.mantid_algorithms import (AddSampleLog, CreateWorkspace, CreateSimulationWorkspace, - ConvertToMD, CloneWorkspace) + ConvertToMD, CloneWorkspace, CreateSampleWorkspace) class WorkspaceManagerPresenterTest(unittest.TestCase): @@ -432,6 +436,45 @@ def test_compose_error(self): self.presenter.notify(Command.ComposeWorkspace) self.view._display_error.assert_called() + def test_ensure_that_the_ads_observer_calls_delete_handle(self, _): + presenter = WorkspaceManagerPresenter(self.view) + presenter.delete_handle = MagicMock() + self.assertTrue(isinstance(presenter._ads_observer, MSliceADSObserver)) + presenter._ads_observer = MSliceADSObserver( + presenter.delete_handle, presenter.clear_handle, presenter.rename_handle + ) + + CreateSampleWorkspace(OutputWorkspace="ws", StoreInADS=True) + AnalysisDataService.remove("ws") + + presenter.delete_handle.assert_called_once_with("ws") + + def test_ensure_that_the_ads_observer_calls_clear_handle(self, _): + presenter = WorkspaceManagerPresenter(self.view) + presenter.clear_handle = MagicMock() + self.assertTrue(isinstance(presenter._ads_observer, MSliceADSObserver)) + presenter._ads_observer = MSliceADSObserver( + presenter.delete_handle, presenter.clear_handle, presenter.rename_handle + ) + + CreateSampleWorkspace(OutputWorkspace="ws", StoreInADS=True) + AnalysisDataService.clear() + + presenter.clear_handle.assert_called_once() + + def test_ensure_that_the_ads_observer_calls_rename_handle(self, _): + presenter = WorkspaceManagerPresenter(self.view) + presenter.rename_handle = MagicMock() + self.assertTrue(isinstance(presenter._ads_observer, MSliceADSObserver)) + presenter._ads_observer = MSliceADSObserver( + presenter.delete_handle, presenter.clear_handle, presenter.rename_handle + ) + + CreateSampleWorkspace(OutputWorkspace="ws", StoreInADS=True) + RenameWorkspace(InputWorkspace="ws", OutputWorkspace="ws1") + + presenter.rename_handle.assert_called_once_with("ws", "ws1") + if __name__ == '__main__': unittest.main()