Skip to content

Commit

Permalink
Connected MSliceADSObserver and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SilkeSchomann committed Dec 5, 2024
1 parent 8926101 commit 101deef
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
20 changes: 1 addition & 19 deletions src/mslice/models/mslice_ads_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
4 changes: 4 additions & 0 deletions src/mslice/models/workspacemanager/workspace_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 20 additions & 1 deletion src/mslice/presenters/workspace_manager_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
47 changes: 45 additions & 2 deletions tests/workspacemanager_presenter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
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
from mslice.views.interfaces.workspace_view import WorkspaceView
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):
Expand Down Expand Up @@ -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()

0 comments on commit 101deef

Please sign in to comment.