def test_set_selected_workspace_handle(self, get_ws_name_mock): self.presenter = WorkspaceManagerPresenter(self.view) self.view.get_workspace_index = mock.Mock(return_value=0) get_ws_name_mock.return_value = 'ws' self.presenter.set_selected_workspaces([mock.Mock()]) get_ws_name_mock.called_once_with(mock.Mock()) self.view.get_workspace_index.assert_called_once_with('ws') self.view.set_workspace_selected.assert_called_once_with([0])
def test_rename_workspace_non_selected_prompt_user(self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports that no workspaces are selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called()
def test_remove_workspace_non_selected_prompt_user(self, delete_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports no workspace selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.RemoveSelectedWorkspaces) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_or_more_workspaces.assert_called_once_with() delete_ws_mock.assert_not_called() assert (not self.view.display_loaded_workspaces.called)
def test_save_workspace_non_selected_prompt_user(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports no workspaces arw selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_workspace.assert_called_once_with() save_dir_mock.assert_not_called() save_ws_mock.assert_not_called()
def test_combine_workspace_wrong_type(self, is_pixel_ws_mock): # Checks that it will fail if one of the workspace is not a MDEventWorkspace self.presenter = WorkspaceManagerPresenter(self.view) selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) is_pixel_ws_mock.side_effect = [True, False] self.presenter.notify(Command.CombineWorkspace) self.view.get_workspace_selected.assert_called_once_with() check_calls = [call('ws1'), call('ws2')] is_pixel_ws_mock.assert_has_calls(check_calls, any_order=True) assert (self.view.error_select_more_than_one_workspaces.called)
def test_rename_workspace_multiple_workspace_selected_prompt_user( self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports multiple selected workspaces on calls to get_workspace_selected selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_only_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called()
def test_combine_workspace_single_ws(self, is_pixel_ws_mock, combine_ws_mock): # Checks that it will fail if only one workspace is selected. is_pixel_ws_mock.return_value = True self.presenter = WorkspaceManagerPresenter(self.view) selected_workspaces = ['ws1'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) self.view.add_workspace_dialog = mock.Mock(return_value='ws2') self.presenter.notify(Command.CombineWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.add_workspace_dialog.assert_called_once() combine_ws_mock.assert_called_once_with(['ws1', 'ws2'], 'ws1_combined')
def test_combine_workspace(self, is_pixel_ws_mock, combine_ws_mock): # Now checks it succeeds otherwise is_pixel_ws_mock.return_value = True self.presenter = WorkspaceManagerPresenter(self.view) selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) self.presenter.notify(Command.CombineWorkspace) self.view.get_workspace_selected.assert_called() assert (not self.view.error_select_more_than_one_workspaces.called) combine_ws_mock.assert_called_once_with( selected_workspaces, selected_workspaces[0] + '_combined')
def test_remove_workspace(self, delete_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a workspace that reports a single selected workspace on calls to get_workspace_selected workspace_to_be_removed = wrap_workspace( CloneWorkspace(self.m_workspace.raw_ws, OutputWorkspace='file1'), 'file1') self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_be_removed]) self.view.display_loaded_workspaces = mock.Mock() self.presenter.notify(Command.RemoveSelectedWorkspaces) self.view.get_workspace_selected.assert_called_once_with() delete_ws_mock.assert_called_once_with(workspace_to_be_removed) self.view.display_loaded_workspaces.assert_called_once()
def test_remove_multiple_workspaces(self, delete_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports 3 selected workspaces on calls to get_workspace_selected workspace1 = wrap_workspace( CloneWorkspace(self.m_workspace.raw_ws, OutputWorkspace='file1'), 'file1') workspace2 = wrap_workspace( CloneWorkspace(self.m_workspace.raw_ws, OutputWorkspace='file2'), 'file2') self.view.get_workspace_selected = mock.Mock( return_value=[workspace1, workspace2]) self.presenter.notify(Command.RemoveSelectedWorkspaces) self.view.get_workspace_selected.assert_called_once_with() delete_calls = [call(workspace1), call(workspace2)] delete_ws_mock.assert_has_calls(delete_calls, any_order=True) assert (self.view.display_loaded_workspaces.called)
def test_save_workspace_multiple_selected(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports multiple workspaces are selected on calls to get_workspace_selected path_to_save_to = r'A:\file\path' self.view.get_workspace_selected = mock.Mock( return_value=['file1', 'file2']) save_dir_mock.return_value = (path_to_save_to, None, '.nxs') save_ws_mock.side_effect = [True, RuntimeError] self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() save_dir_mock.assert_called_once_with(multiple_files=True, save_as_image=False, default_ext='.nxs') save_ws_mock.assert_called_with(['file1', 'file2'], path_to_save_to, None, '.nxs')
def test_save_ascii_workspace(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that report a single selected workspace on calls to get_workspace_selected and supplies a path # to save to on calls to get_workspace_to_save_filepath path_to_save_to = r'A:\file\path' workspace_to_save = 'file1' self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_save]) save_dir_mock.return_value = (path_to_save_to, workspace_to_save, '.txt') self.presenter.notify(Command.SaveSelectedWorkspaceAscii) save_dir_mock.assert_called_once_with(multiple_files=False, save_as_image=False, default_ext='.txt') self.view.get_workspace_selected.assert_called_once_with() save_ws_mock.assert_called_once_with([workspace_to_save], path_to_save_to, 'file1', '.txt')
def test_that_rename_workspace_works_as_expected(self, get_ws_names_mock, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that will return a single selected workspace on call to get_workspace_selected and supply a # name on call to get_workspace_new_name old_workspace_name = 'file1' new_workspace_name = 'new_name' self.view.get_workspace_selected = mock.Mock( return_value=[old_workspace_name]) self.view.get_workspace_new_name = mock.Mock( return_value=new_workspace_name) self.view.display_loaded_workspaces = mock.Mock() get_ws_names_mock.return_value = ['file1', 'file2', 'file3'] self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.get_workspace_new_name.assert_called_once_with() rename_ws_mock.assert_called_once_with('file1', 'new_name') self.view.display_loaded_workspaces.assert_called_once()
def test_rename_workspace_multiple_workspace_selected_prompt_user(self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports multiple selected workspaces on calls to get_workspace_selected selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock(return_value=selected_workspaces) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_only_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called()
def __init__(self, parent=None): QWidget.__init__(self, parent) load_ui(__file__, 'workspacemanager.ui', self) self.button_mappings = {} self._main_window = None self.onscreen_workspaces = [] self.tab = None self.tab_to_list = { TAB_2D: self.listWorkspaces2D, TAB_EVENT: self.listWorkspacesEvent, TAB_HISTO: self.listWorkspacesHisto } self.tabWidget.currentChanged.connect(self.tab_changed_method) self.listWorkspaces2D.itemSelectionChanged.connect( self.list_item_changed) self.listWorkspacesEvent.itemSelectionChanged.connect( self.list_item_changed) self.listWorkspacesHisto.itemSelectionChanged.connect( self.list_item_changed) self._presenter = WorkspaceManagerPresenter(self)
def test_save_workspace_cancelled(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that report a single selected workspace on calls to get_workspace_selected and supplies a path # to save to on calls to get_workspace_to_save_filepath path_to_save_to = "" # view returns empty string to indicate operation cancelled workspace_to_save = 'file1' self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_save]) self.view.error_invalid_save_path = mock.Mock() save_dir_mock.return_value = (path_to_save_to, workspace_to_save, '.nxs') self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() self.view.get_workspace_selected.assert_called_once_with() save_dir_mock.assert_called_once_with(multiple_files=False, save_as_image=False, default_ext='.nxs') self.view.error_invalid_save_path.assert_called_once() save_ws_mock.assert_not_called()
def test_that_rename_workspace_works_as_expected(self, get_ws_names_mock, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that will return a single selected workspace on call to get_workspace_selected and supply a # name on call to get_workspace_new_name old_workspace_name = 'file1' new_workspace_name = 'new_name' self.view.get_workspace_selected = mock.Mock(return_value=[old_workspace_name]) self.view.get_workspace_new_name = mock.Mock(return_value=new_workspace_name) self.view.display_loaded_workspaces = mock.Mock() get_ws_names_mock.return_value = ['file1', 'file2', 'file3'] self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.get_workspace_new_name.assert_called_once_with() rename_ws_mock.assert_called_once_with('file1', 'new_name') self.view.display_loaded_workspaces.assert_called_once()
def test_notify_presenter_clears_error(self): presenter = WorkspaceManagerPresenter(self.view) presenter.register_master(self.main_presenter) # This unit test will verify that notifying cut presenter will cause the error to be cleared on the view. # The actual subsequent procedure will fail, however this irrelevant to this. Hence the try, except blocks for command in [x for x in dir(Command) if x[0] != "_"]: try: presenter.notify(command) except ValueError: pass assert (self.view.clear_displayed_error.called) self.view.reset_mock()
class WorkspaceManagerWidget(WorkspaceView, QWidget): """A Widget that allows user to perform basic workspace save/load/rename/delete operations on workspaces""" error_occurred = Signal('QString') tab_changed = Signal(int) busy = Signal(bool) def __init__(self, parent=None): QWidget.__init__(self, parent) load_ui(__file__, 'workspacemanager.ui', self) self.button_mappings = {} self._main_window = None self.onscreen_workspaces = [] self.tab = None self.tab_to_list = { TAB_2D: self.listWorkspaces2D, TAB_EVENT: self.listWorkspacesEvent, TAB_HISTO: self.listWorkspacesHisto } self.tabWidget.currentChanged.connect(self.tab_changed_method) self.listWorkspaces2D.itemSelectionChanged.connect( self.list_item_changed) self.listWorkspacesEvent.itemSelectionChanged.connect( self.list_item_changed) self.listWorkspacesHisto.itemSelectionChanged.connect( self.list_item_changed) self._presenter = WorkspaceManagerPresenter(self) def _display_error(self, error_string): self.error_occurred.emit(error_string) def tab_changed_method(self, tab_index): self.clear_selection() self.tab = tab_index if self.tabWidget.tabText(tab_index)[-1:] == "*": self.tabWidget.setTabText(tab_index, self.tabWidget.tabText(tab_index)[:-1]) self.tab_changed.emit(tab_index) def clear_selection(self): for ws_list in [ self.listWorkspaces2D, self.listWorkspacesEvent, self.listWorkspacesHisto ]: ws_list.clearSelection() def current_list(self): return self.tab_to_list[self.tabWidget.currentIndex()] def current_tab(self): return self.tab if self.tab is not None else self.tabWidget.currentIndex( ) def change_tab(self, tab): self.tabWidget.setCurrentIndex(tab) def highlight_tab(self, tab): tab_text = self.tabWidget.tabText(tab) if not tab_text.endswith('*'): self.tabWidget.setTabText(tab, tab_text + '*') def _btn_clicked(self): sender = self.sender() try: command = self.button_mappings[sender] except KeyError: raise Exception('Invalid sender') self._presenter.notify(command) def add_workspace(self, workspace): item = QListWidgetItem(workspace) self.onscreen_workspaces.append(workspace) workspace = get_workspace_handle(workspace) if isinstance(workspace, PixelWorkspace): self.listWorkspacesEvent.addItem(item) elif isinstance(workspace, HistogramWorkspace): self.listWorkspacesHisto.addItem(item) elif isinstance(workspace, Workspace): self.listWorkspaces2D.addItem(item) else: raise TypeError("Loaded file is not a valid workspace") def display_loaded_workspaces(self, workspaces): for workspace in workspaces: if workspace not in self.onscreen_workspaces: self.add_workspace(workspace) for workspace in self.onscreen_workspaces: if workspace not in workspaces: self.remove_workspace(workspace) def remove_workspace(self, workspace): """Remove workspace from list. Must be done in seperate function because items are removed by index and removing an items may alter the indexes of other items""" self.onscreen_workspaces.remove(workspace) for ws_list in [ self.listWorkspaces2D, self.listWorkspacesEvent, self.listWorkspacesHisto ]: for index in range(ws_list.count()): if ws_list.item(index).text() == workspace: ws_list.takeItem(index) return def add_workspace_dialog(self): items = [] current_list = self.current_list() for i in range(current_list.count()): item = current_list.item(i).text() items.append(item) dialog = QInputDialog() dialog.setWindowTitle("Add Workspace") dialog.setLabelText("Choose a workspace to add:") dialog.setOptions(QInputDialog.UseListViewForComboBoxItems) dialog.setComboBoxItems(items) dialog.exec_() return dialog.textValue() def subtraction_input(self): sub_input = SubtractInputBox(self.listWorkspaces2D, self) if sub_input.exec_(): return sub_input.user_input() else: raise RuntimeError('dialog cancelled') def get_workspace_selected(self): selected_workspaces = [ str(x.text()) for x in self.current_list().selectedItems() ] return selected_workspaces def set_workspace_selected(self, index): current_list = self.current_list() if QT_VERSION.startswith('5'): for item_index in range(current_list.count()): current_list.item(item_index).setSelected(False) for this_index in (index if hasattr(index, "__iter__") else [index]): current_list.item(this_index).setSelected(True) else: for item_index in range(current_list.count()): current_list.setItemSelected(current_list.item(item_index), False) for this_index in (index if hasattr(index, "__iter__") else [index]): current_list.setItemSelected(current_list.item(this_index), True) def get_workspace_index(self, ws_name): current_list = self.current_list() for index in range(current_list.count()): if str(current_list.item(index).text()) == ws_name: return index return -1 def get_workspace_to_load_path(self): paths = QFileDialog.getOpenFileNames() return paths[0] if isinstance( paths, tuple) else [str(filename) for filename in paths] def get_workspace_new_name(self): name, success = QInputDialog.getText( self, "Workspace New Name", "Enter the new name for the workspace : ") # The message above was padded with spaces to allow the whole title to show up if not success: raise ValueError('No Valid Name supplied') return str(name) def error_select_only_one_workspace(self): self._display_error( 'Please select only one workspace and then try again') def error_select_one_or_more_workspaces(self): self._display_error( 'Please select one or more workspaces the try again') def error_select_one_workspace(self): self._display_error('Please select a workspace then try again') def error_select_more_than_one_workspaces(self): self._display_error( 'Please select more than one projected workspaces then try again') def error_invalid_save_path(self): self._display_error('No files were saved') def get_presenter(self): return self._presenter def list_item_changed(self): self._presenter.notify(Command.SelectionChanged) def error_unable_to_save(self): self._display_error("Something went wrong while trying to save") def clear_displayed_error(self): self._display_error("")
def test_register_master_invalid_master_fail(self): workspace_presenter = WorkspaceManagerPresenter(self.view) self.assertRaises(AssertionError, workspace_presenter.register_master, 3)
def test_register_master_success(self): workspace_presenter = WorkspaceManagerPresenter(self.view) workspace_presenter.register_master(self.main_presenter) self.main_presenter.register_workspace_selector.assert_called_once_with( workspace_presenter)
class MantidWorkspaceProviderTest(unittest.TestCase): def setUp(self): self.view = mock.create_autospec(spec=WorkspaceView) self.test_ws_2d = CreateSimulationWorkspace( OutputWorkspace='test_ws_2d', Instrument='MAR', BinParams=[-10, 1, 10], UnitX='DeltaE') AddSampleLog(Workspace=self.test_ws_2d.raw_ws, LogName='Ei', LogText='50.', LogType='Number', StoreInADS=False) self.test_ws_md = ConvertToMD(OutputWorkspace='test_ws_md', InputWorkspace=self.test_ws_2d, QDimensions='|Q|', dEAnalysisMode='Direct', MinValues='-10,0,0', MaxValues='10,6,500', SplitInto='50,50') self.test_ws_2d.e_mode = "Direct" self.test_ws_md.ef_defined = False self.test_ws_md.is_PSD = True self.test_ws_md.e_mode = "Direct" self.test_ws_md.limits = {'DeltaE': [0, 2, 1]} def test_delete_workspace(self): delete_workspace('test_ws_md') self.assertFalse('test_ws_md' in get_visible_workspace_names()) def test_subtract_workspace(self): subtract(['test_ws_2d'], 'test_ws_2d', 0.95) result = get_workspace_handle('test_ws_2d_subtracted') np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [0.05] * 20) np.testing.assert_array_almost_equal(self.test_ws_2d.raw_ws.dataY(0), [1] * 20) self.assertFalse('scaled_bg_ws' in get_visible_workspace_names()) def test_add_workspace(self): original_data = self.test_ws_2d.raw_ws.dataY(0) add_workspace_runs(['test_ws_2d', 'test_ws_2d']) result = get_workspace_handle('test_ws_2d_sum') np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [2.0] * 20) np.testing.assert_array_almost_equal(original_data, [1] * 20) @patch( 'mslice.models.workspacemanager.workspace_algorithms._original_step_size' ) def test_combine_workspace(self, step_mock): ws_2 = CloneWorkspace(OutputWorkspace='ws_2', InputWorkspace=self.test_ws_md) ws_2.e_mode = "Direct" step_mock.return_value = 1 combined = combine_workspace([self.test_ws_md, ws_2], 'combined') np.testing.assert_array_almost_equal(combined.limits['DeltaE'], [-10, 10, 1], 4) np.testing.assert_array_almost_equal(combined.limits['|Q|'], [0.2939, 9.4817, 0.0919], 4) self.assertTrue(combined.is_PSD) def test_process_EFixed(self): processEfixed(self.test_ws_2d) self.assertTrue(self.test_ws_2d.ef_defined) def test_rename_workspace(self): rename_workspace('test_ws_md', 'newname') self.assertTrue('newname' in get_visible_workspace_names()) self.assertFalse('test_ws_md' in get_visible_workspace_names()) new_ws = get_workspace_handle('newname') self.assertFalse(new_ws.ef_defined) self.assertEqual(new_ws.limits['DeltaE'], [0, 2, 1]) @patch('mslice.presenters.workspace_manager_presenter.rename_workspace') @patch( 'mslice.presenters.workspace_manager_presenter.get_visible_workspace_names' ) def test_that_rename_workspace_works_as_expected(self, get_ws_names_mock, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that will return a single selected workspace on call to get_workspace_selected and supply a # name on call to get_workspace_new_name old_workspace_name = 'file1' new_workspace_name = 'new_name' self.view.get_workspace_selected = mock.Mock( return_value=[old_workspace_name]) self.view.get_workspace_new_name = mock.Mock( return_value=new_workspace_name) self.view.display_loaded_workspaces = mock.Mock() get_ws_names_mock.return_value = ['file1', 'file2', 'file3'] self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.get_workspace_new_name.assert_called_once_with() rename_ws_mock.assert_called_once_with('file1', 'new_name') self.view.display_loaded_workspaces.assert_called_once() @patch('mslice.presenters.workspace_manager_presenter.rename_workspace') def test_rename_workspace_multiple_workspace_selected_prompt_user( self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports multiple selected workspaces on calls to get_workspace_selected selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_only_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called() @patch('mslice.presenters.workspace_manager_presenter.rename_workspace') def test_rename_workspace_non_selected_prompt_user(self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports that no workspaces are selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called() def test_propagate_properties(self): ws_2 = CreateSimulationWorkspace(OutputWorkspace='test_ws_2', Instrument='MAR', BinParams=[-1, 1, 20], UnitX='DeltaE') propagate_properties(self.test_ws_md, ws_2) delete_workspace('test_ws_md') self.assertFalse(ws_2.ef_defined) self.assertEqual({'DeltaE': [0, 2, 1]}, ws_2.limits) def test_get_limits(self): limits = get_limits('test_ws_md', 'DeltaE') self.assertEqual(limits[0], 0) self.assertEqual(limits[1], 2) self.assertEqual(limits[2], 1) @patch( 'mslice.models.workspacemanager.workspace_algorithms._original_step_size' ) def test_get_limits_100_steps(self, step_mock): self.test_ws_md.limits = {} step_mock.return_value = 1 limits = get_limits('test_ws_md', 'DeltaE') np.testing.assert_array_almost_equal(limits, [-10, 10, 1], 4) limits = get_limits('test_ws_md', '|Q|') np.testing.assert_allclose(limits, [0.545576, 8.615743, 0.042867], rtol=0, atol=1e-3) def test_get_limits_saved(self): self.test_ws_2d.limits = {} get_limits('test_ws_2d', 'DeltaE') np.testing.assert_array_equal(self.test_ws_2d.limits['DeltaE'], [-10, 10, 1]) np.testing.assert_array_equal( self.test_ws_2d.limits['|Q|'], self.test_ws_2d.limits['MomentumTransfer']) np.testing.assert_almost_equal(self.test_ws_2d.limits['|Q|'], [0.25116, 9.52454, 0.04287], 5) np.testing.assert_almost_equal(self.test_ws_2d.limits['2Theta'], [3.43, 134.14, 0.57296], 5)
class WorkspaceManagerPresenterTest(unittest.TestCase): @classmethod def setUpClass(cls): x = np.linspace(0, 99, 100) y = x * 1 e = y * 0 + 2 cls.m_workspace = wrap_workspace( CreateWorkspace(x, y, e, OutputWorkspace="m_ws"), 'm_ws') sim_workspace = CreateSimulationWorkspace(Instrument='MAR', BinParams=[-10, 1, 10], UnitX='DeltaE', OutputWorkspace='ws1') AddSampleLog(sim_workspace, LogName='Ei', LogText='3.', LogType='Number') cls.px_workspace = ConvertToMD(InputWorkspace=sim_workspace, OutputWorkspace="ws1", QDimensions='|Q|', dEAnalysisMode='Direct', MinValues='-10,0,0', MaxValues='10,6,500', SplitInto='50,50') cls.px_workspace_clone = CloneWorkspace( InputWorkspace=cls.px_workspace, OutputWorkspace='ws2', StoreInADS=False) cls.px_workspace = wrap_workspace(cls.px_workspace, 'ws1') cls.px_workspace_clone = wrap_workspace(cls.px_workspace_clone, 'ws2') def setUp(self): self.view = mock.create_autospec(spec=WorkspaceView) self.mainview = mock.create_autospec(MainView) self.main_presenter = mock.create_autospec(MainPresenterInterface) self.mainview.get_presenter = mock.Mock( return_value=self.main_presenter) def test_register_master_success(self): workspace_presenter = WorkspaceManagerPresenter(self.view) workspace_presenter.register_master(self.main_presenter) self.main_presenter.register_workspace_selector.assert_called_once_with( workspace_presenter) def test_register_master_invalid_master_fail(self): workspace_presenter = WorkspaceManagerPresenter(self.view) self.assertRaises(AssertionError, workspace_presenter.register_master, 3) @patch('mslice.presenters.workspace_manager_presenter.get_save_directory') @patch('mslice.presenters.workspace_manager_presenter.save_workspaces') def test_save_workspace(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that report a single selected workspace on calls to get_workspace_selected and supplies a path # to save to on calls to get_workspace_to_save_filepath path_to_save_to = r'A:\file\path' workspace_to_save = 'file1' self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_save]) save_dir_mock.return_value = (path_to_save_to, workspace_to_save, '.nxs') self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() save_dir_mock.assert_called_once_with(multiple_files=False, save_as_image=False, default_ext='.nxs') save_ws_mock.assert_called_once_with([workspace_to_save], path_to_save_to, 'file1', '.nxs') @patch('mslice.presenters.workspace_manager_presenter.get_save_directory') @patch('mslice.presenters.workspace_manager_presenter.save_workspaces') def test_save_ascii_workspace(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that report a single selected workspace on calls to get_workspace_selected and supplies a path # to save to on calls to get_workspace_to_save_filepath path_to_save_to = r'A:\file\path' workspace_to_save = 'file1' self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_save]) save_dir_mock.return_value = (path_to_save_to, workspace_to_save, '.txt') self.presenter.notify(Command.SaveSelectedWorkspaceAscii) save_dir_mock.assert_called_once_with(multiple_files=False, save_as_image=False, default_ext='.txt') self.view.get_workspace_selected.assert_called_once_with() save_ws_mock.assert_called_once_with([workspace_to_save], path_to_save_to, 'file1', '.txt') @patch('mslice.presenters.workspace_manager_presenter.get_save_directory') @patch('mslice.presenters.workspace_manager_presenter.save_workspaces') def test_save_matlab_workspace(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that report a single selected workspace on calls to get_workspace_selected and supplies a path # to save to on calls to get_workspace_to_save_filepath path_to_save_to = r'A:\file\path' workspace_to_save = 'file1' self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_save]) save_dir_mock.return_value = (path_to_save_to, workspace_to_save, '.mat') self.presenter.notify(Command.SaveSelectedWorkspaceMatlab) self.view.get_workspace_selected.assert_called_once_with() save_dir_mock.assert_called_once_with(multiple_files=False, save_as_image=False, default_ext='.mat') save_ws_mock.assert_called_once_with([workspace_to_save], path_to_save_to, 'file1', '.mat') @patch('mslice.presenters.workspace_manager_presenter.get_save_directory') @patch('mslice.presenters.workspace_manager_presenter.save_workspaces') def test_save_workspace_multiple_selected(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports multiple workspaces are selected on calls to get_workspace_selected path_to_save_to = r'A:\file\path' self.view.get_workspace_selected = mock.Mock( return_value=['file1', 'file2']) save_dir_mock.return_value = (path_to_save_to, None, '.nxs') save_ws_mock.side_effect = [True, RuntimeError] self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() save_dir_mock.assert_called_once_with(multiple_files=True, save_as_image=False, default_ext='.nxs') save_ws_mock.assert_called_with(['file1', 'file2'], path_to_save_to, None, '.nxs') @patch('mslice.presenters.workspace_manager_presenter.get_save_directory') @patch('mslice.presenters.workspace_manager_presenter.save_workspaces') def test_save_workspace_non_selected_prompt_user(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports no workspaces arw selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_workspace.assert_called_once_with() save_dir_mock.assert_not_called() save_ws_mock.assert_not_called() @patch('mslice.presenters.workspace_manager_presenter.get_save_directory') @patch('mslice.presenters.workspace_manager_presenter.save_workspaces') def test_save_workspace_cancelled(self, save_ws_mock, save_dir_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that report a single selected workspace on calls to get_workspace_selected and supplies a path # to save to on calls to get_workspace_to_save_filepath path_to_save_to = "" # view returns empty string to indicate operation cancelled workspace_to_save = 'file1' self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_save]) self.view.error_invalid_save_path = mock.Mock() save_dir_mock.return_value = (path_to_save_to, workspace_to_save, '.nxs') self.presenter.notify(Command.SaveSelectedWorkspaceNexus) self.view.get_workspace_selected.assert_called_once_with() self.view.get_workspace_selected.assert_called_once_with() save_dir_mock.assert_called_once_with(multiple_files=False, save_as_image=False, default_ext='.nxs') self.view.error_invalid_save_path.assert_called_once() save_ws_mock.assert_not_called() @patch('mslice.presenters.workspace_manager_presenter.delete_workspace') def test_remove_workspace(self, delete_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a workspace that reports a single selected workspace on calls to get_workspace_selected workspace_to_be_removed = wrap_workspace( CloneWorkspace(self.m_workspace.raw_ws, OutputWorkspace='file1'), 'file1') self.view.get_workspace_selected = mock.Mock( return_value=[workspace_to_be_removed]) self.view.display_loaded_workspaces = mock.Mock() self.presenter.notify(Command.RemoveSelectedWorkspaces) self.view.get_workspace_selected.assert_called_once_with() delete_ws_mock.assert_called_once_with(workspace_to_be_removed) self.view.display_loaded_workspaces.assert_called_once() @patch('mslice.presenters.workspace_manager_presenter.delete_workspace') def test_remove_multiple_workspaces(self, delete_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports 3 selected workspaces on calls to get_workspace_selected workspace1 = wrap_workspace( CloneWorkspace(self.m_workspace.raw_ws, OutputWorkspace='file1'), 'file1') workspace2 = wrap_workspace( CloneWorkspace(self.m_workspace.raw_ws, OutputWorkspace='file2'), 'file2') self.view.get_workspace_selected = mock.Mock( return_value=[workspace1, workspace2]) self.presenter.notify(Command.RemoveSelectedWorkspaces) self.view.get_workspace_selected.assert_called_once_with() delete_calls = [call(workspace1), call(workspace2)] delete_ws_mock.assert_has_calls(delete_calls, any_order=True) assert (self.view.display_loaded_workspaces.called) @patch('mslice.presenters.workspace_manager_presenter.delete_workspace') def test_remove_workspace_non_selected_prompt_user(self, delete_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports no workspace selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.RemoveSelectedWorkspaces) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_or_more_workspaces.assert_called_once_with() delete_ws_mock.assert_not_called() assert (not self.view.display_loaded_workspaces.called) def test_broadcast_success(self): self.presenter = WorkspaceManagerPresenter(self.view) self.presenter.register_master(self.main_presenter) self.presenter.notify(Command.SelectionChanged) self.main_presenter.notify_workspace_selection_changed() def test_call_presenter_with_unknown_command(self): self.presenter = WorkspaceManagerPresenter(self.view) unknown_command = 10 self.assertRaises(ValueError, self.presenter.notify, unknown_command) def test_notify_presenter_clears_error(self): presenter = WorkspaceManagerPresenter(self.view) presenter.register_master(self.main_presenter) # This unit test will verify that notifying cut presenter will cause the error to be cleared on the view. # The actual subsequent procedure will fail, however this irrelevant to this. Hence the try, except blocks for command in [x for x in dir(Command) if x[0] != "_"]: try: presenter.notify(command) except ValueError: pass assert (self.view.clear_displayed_error.called) self.view.reset_mock() def test_set_selected_workspace_index(self): self.presenter = WorkspaceManagerPresenter(self.view) self.view.get_workspace_index = mock.Mock() self.presenter.set_selected_workspaces([1]) self.view.set_workspace_selected.assert_called_once_with([1]) def test_set_selected_workspace_name(self): self.presenter = WorkspaceManagerPresenter(self.view) self.view.get_workspace_index = mock.Mock(return_value=0) self.presenter.set_selected_workspaces(['ws']) self.view.get_workspace_index.assert_called_once_with('ws') self.view.set_workspace_selected.assert_called_once_with([0]) @patch('mslice.presenters.workspace_manager_presenter.get_workspace_name') def test_set_selected_workspace_handle(self, get_ws_name_mock): self.presenter = WorkspaceManagerPresenter(self.view) self.view.get_workspace_index = mock.Mock(return_value=0) get_ws_name_mock.return_value = 'ws' self.presenter.set_selected_workspaces([mock.Mock()]) get_ws_name_mock.called_once_with(mock.Mock()) self.view.get_workspace_index.assert_called_once_with('ws') self.view.set_workspace_selected.assert_called_once_with([0]) @patch('mslice.presenters.workspace_manager_presenter.combine_workspace') @patch('mslice.presenters.workspace_manager_presenter.is_pixel_workspace') def test_combine_workspace_single_ws(self, is_pixel_ws_mock, combine_ws_mock): # Checks that it will fail if only one workspace is selected. is_pixel_ws_mock.return_value = True self.presenter = WorkspaceManagerPresenter(self.view) selected_workspaces = ['ws1'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) self.view.add_workspace_dialog = mock.Mock(return_value='ws2') self.presenter.notify(Command.CombineWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.add_workspace_dialog.assert_called_once() combine_ws_mock.assert_called_once_with(['ws1', 'ws2'], 'ws1_combined') @patch('mslice.presenters.workspace_manager_presenter.is_pixel_workspace') def test_combine_workspace_wrong_type(self, is_pixel_ws_mock): # Checks that it will fail if one of the workspace is not a MDEventWorkspace self.presenter = WorkspaceManagerPresenter(self.view) selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) is_pixel_ws_mock.side_effect = [True, False] self.presenter.notify(Command.CombineWorkspace) self.view.get_workspace_selected.assert_called_once_with() check_calls = [call('ws1'), call('ws2')] is_pixel_ws_mock.assert_has_calls(check_calls, any_order=True) assert (self.view.error_select_more_than_one_workspaces.called) @patch('mslice.presenters.workspace_manager_presenter.combine_workspace') @patch('mslice.presenters.workspace_manager_presenter.is_pixel_workspace') def test_combine_workspace(self, is_pixel_ws_mock, combine_ws_mock): # Now checks it succeeds otherwise is_pixel_ws_mock.return_value = True self.presenter = WorkspaceManagerPresenter(self.view) selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock( return_value=selected_workspaces) self.presenter.notify(Command.CombineWorkspace) self.view.get_workspace_selected.assert_called() assert (not self.view.error_select_more_than_one_workspaces.called) combine_ws_mock.assert_called_once_with( selected_workspaces, selected_workspaces[0] + '_combined')
def test_set_selected_workspace_name(self): self.presenter = WorkspaceManagerPresenter(self.view) self.view.get_workspace_index = mock.Mock(return_value=0) self.presenter.set_selected_workspaces(['ws']) self.view.get_workspace_index.assert_called_once_with('ws') self.view.set_workspace_selected.assert_called_once_with([0])
def test_set_selected_workspace_index(self): self.presenter = WorkspaceManagerPresenter(self.view) self.view.get_workspace_index = mock.Mock() self.presenter.set_selected_workspaces([1]) self.view.set_workspace_selected.assert_called_once_with([1])
def test_call_presenter_with_unknown_command(self): self.presenter = WorkspaceManagerPresenter(self.view) unknown_command = 10 self.assertRaises(ValueError, self.presenter.notify, unknown_command)
def test_broadcast_success(self): self.presenter = WorkspaceManagerPresenter(self.view) self.presenter.register_master(self.main_presenter) self.presenter.notify(Command.SelectionChanged) self.main_presenter.notify_workspace_selection_changed()
class MantidWorkspaceProviderTest(unittest.TestCase): def setUp(self): self.view = mock.create_autospec(spec=WorkspaceView) self.test_ws_2d = CreateSimulationWorkspace(OutputWorkspace='test_ws_2d', Instrument='MAR', BinParams=[-10, 1, 10], UnitX='DeltaE') AddSampleLog(Workspace=self.test_ws_2d.raw_ws, LogName='Ei', LogText='50.', LogType='Number', StoreInADS=False) self.test_ws_md = ConvertToMD(OutputWorkspace='test_ws_md', InputWorkspace=self.test_ws_2d, QDimensions='|Q|', dEAnalysisMode='Direct', MinValues='-10,0,0', MaxValues='10,6,500', SplitInto='50,50') self.test_ws_2d.e_mode = "Direct" self.test_ws_md.ef_defined = False self.test_ws_md.is_PSD = True self.test_ws_md.e_mode = "Direct" self.test_ws_md.limits = {'DeltaE': [0, 2, 1]} def test_delete_workspace(self): delete_workspace('test_ws_md') self.assertFalse('test_ws_md' in get_visible_workspace_names()) def test_subtract_workspace(self): subtract(['test_ws_2d'], 'test_ws_2d', 0.95) result = get_workspace_handle('test_ws_2d_subtracted') np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [0.05] * 20) np.testing.assert_array_almost_equal(self.test_ws_2d.raw_ws.dataY(0), [1] * 20) self.assertFalse('scaled_bg_ws' in get_visible_workspace_names()) def test_add_workspace(self): original_data = self.test_ws_2d.raw_ws.dataY(0) add_workspace_runs(['test_ws_2d', 'test_ws_2d']) result = get_workspace_handle('test_ws_2d_sum') np.testing.assert_array_almost_equal(result.raw_ws.dataY(0), [2.0] * 20) np.testing.assert_array_almost_equal(original_data, [1] * 20) @patch('mslice.models.workspacemanager.workspace_algorithms._original_step_size') def test_combine_workspace(self, step_mock): ws_2 = CloneWorkspace(OutputWorkspace='ws_2', InputWorkspace=self.test_ws_md) ws_2.e_mode = "Direct" step_mock.return_value = 1 combined = combine_workspace([self.test_ws_md, ws_2], 'combined') np.testing.assert_array_almost_equal(combined.limits['DeltaE'], [-10, 10, 1], 4) np.testing.assert_array_almost_equal(combined.limits['|Q|'], [0.2939, 9.4817, 0.0919], 4) self.assertTrue(combined.is_PSD) def test_process_EFixed(self): processEfixed(self.test_ws_2d) self.assertTrue(self.test_ws_2d.ef_defined) def test_rename_workspace(self): rename_workspace('test_ws_md', 'newname') self.assertTrue('newname' in get_visible_workspace_names()) self.assertFalse('test_ws_md' in get_visible_workspace_names()) new_ws = get_workspace_handle('newname') self.assertFalse(new_ws.ef_defined) self.assertEqual(new_ws.limits['DeltaE'], [0, 2, 1]) @patch('mslice.presenters.workspace_manager_presenter.rename_workspace') @patch('mslice.presenters.workspace_manager_presenter.get_visible_workspace_names') def test_that_rename_workspace_works_as_expected(self, get_ws_names_mock, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that will return a single selected workspace on call to get_workspace_selected and supply a # name on call to get_workspace_new_name old_workspace_name = 'file1' new_workspace_name = 'new_name' self.view.get_workspace_selected = mock.Mock(return_value=[old_workspace_name]) self.view.get_workspace_new_name = mock.Mock(return_value=new_workspace_name) self.view.display_loaded_workspaces = mock.Mock() get_ws_names_mock.return_value = ['file1', 'file2', 'file3'] self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.get_workspace_new_name.assert_called_once_with() rename_ws_mock.assert_called_once_with('file1', 'new_name') self.view.display_loaded_workspaces.assert_called_once() @patch('mslice.presenters.workspace_manager_presenter.rename_workspace') def test_rename_workspace_multiple_workspace_selected_prompt_user(self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports multiple selected workspaces on calls to get_workspace_selected selected_workspaces = ['ws1', 'ws2'] self.view.get_workspace_selected = mock.Mock(return_value=selected_workspaces) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_only_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called() @patch('mslice.presenters.workspace_manager_presenter.rename_workspace') def test_rename_workspace_non_selected_prompt_user(self, rename_ws_mock): self.presenter = WorkspaceManagerPresenter(self.view) # Create a view that reports that no workspaces are selected on calls to get_workspace_selected self.view.get_workspace_selected = mock.Mock(return_value=[]) self.presenter.notify(Command.RenameWorkspace) self.view.get_workspace_selected.assert_called_once_with() self.view.error_select_one_workspace.assert_called_once_with() rename_ws_mock.assert_not_called() def test_propagate_properties(self): ws_2 = CreateSimulationWorkspace(OutputWorkspace='test_ws_2', Instrument='MAR', BinParams=[-1, 1, 20], UnitX='DeltaE') propagate_properties(self.test_ws_md, ws_2) delete_workspace('test_ws_md') self.assertFalse(ws_2.ef_defined) self.assertEqual({'DeltaE': [0, 2, 1]}, ws_2.limits) def test_get_limits(self): limits = get_limits('test_ws_md', 'DeltaE') self.assertEqual(limits[0], 0) self.assertEqual(limits[1], 2) self.assertEqual(limits[2], 1) @patch('mslice.models.workspacemanager.workspace_algorithms._original_step_size') def test_get_limits_100_steps(self, step_mock): self.test_ws_md.limits = {} step_mock.return_value = 1 limits = get_limits('test_ws_md', 'DeltaE') np.testing.assert_array_almost_equal(limits, [-10, 10, 1], 4) limits = get_limits('test_ws_md', '|Q|') np.testing.assert_allclose(limits, [0.545576, 8.615743, 0.042867], rtol=0, atol=1e-3) def test_get_limits_saved(self): self.test_ws_2d.limits = {} get_limits('test_ws_2d', 'DeltaE') np.testing.assert_array_equal(self.test_ws_2d.limits['DeltaE'], [-10, 10, 1]) np.testing.assert_array_equal(self.test_ws_2d.limits['|Q|'], self.test_ws_2d.limits['MomentumTransfer']) np.testing.assert_almost_equal(self.test_ws_2d.limits['|Q|'], [0.25116, 9.52454, 0.04287], 5) np.testing.assert_almost_equal(self.test_ws_2d.limits['2Theta'], [3.43, 134.14, 0.57296], 5)