def assert_call_as_expected(exp_xmin, exp_xmax, exp_start_index, exp_end_index, transpose, is_spectra): mock_ws = _create_mock_matrixworkspace(x_axis=[10, 20, 30], y_axis=[1, 2, 3, 4, 5], distribution=False, y_is_spectra=is_spectra) mock_ws.name.return_value = 'mock_ws' model = SliceViewerModel(mock_ws) slicepoint, bin_params = MagicMock(), MagicMock() help_msg = model.export_roi_to_workspace(slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose) self.assertEqual('ROI created: mock_ws_roi', help_msg) if is_spectra: self.assertEqual(2, mock_ws.getAxis(1).indexOfValue.call_count) else: mock_ws.getAxis(1).extractValues.assert_called_once() mock_extract_spectra.assert_called_once_with(InputWorkspace=mock_ws, OutputWorkspace='mock_ws_roi', XMin=exp_xmin, XMax=exp_xmax, StartWorkspaceIndex=exp_start_index, EndWorkspaceIndex=exp_end_index, EnableLogging=True) mock_extract_spectra.reset_mock()
def assert_error_returned_in_help(workspace, export_type, mock_alg, err_msg): model = SliceViewerModel(workspace) slicepoint, bin_params = MagicMock(), MagicMock() mock_alg.side_effect = RuntimeError(err_msg) try: if export_type == 'r': help_msg = model.export_roi_to_workspace(slicepoint, bin_params, ((1.0, 2.0), (-1, 2.0)), True) else: help_msg = model.export_cuts_to_workspace(slicepoint, bin_params, ((1.0, 2.0), (-1, 2.0)), True, export_type) except Exception as exc: help_msg = str(exc) mock_alg.reset_mock() self.assertTrue(err_msg in help_msg)
def assert_call_as_expected(transpose, export_type): model = SliceViewerModel(self.ws_MDE_3D) if export_type == 'r': help_msg = model.export_roi_to_workspace(slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose) else: help_msg = model.export_cuts_to_workspace(slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, export_type) if transpose: extents = [ymin, ymax, xmin, xmax, zmin, zmax] else: extents = [xmin, xmax, ymin, ymax, zmin, zmax] common_call_params = dict(InputWorkspace=self.ws_MDE_3D, AxisAligned=False, BasisVector0='h,rlu,1.0,0.0,0.0', BasisVector1='k,rlu,0.0,1.0,0.0', BasisVector2='l,rlu,0.0,0.0,1.0', OutputExtents=extents) xcut_name, ycut_name = 'ws_MDE_3D_cut_x', 'ws_MDE_3D_cut_y' if export_type == 'r': expected_help_msg = 'ROI created: ws_MDE_3D_roi' expected_calls = [ call(**common_call_params, OutputBins=[100, 100, 1], OutputWorkspace='ws_MDE_3D_roi') ] elif export_type == 'x': expected_help_msg = f'Cut along X created: {xcut_name}' expected_bins = [1, 100, 1] if transpose else [100, 1, 1] expected_calls = [ call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=xcut_name) ] elif export_type == 'y': expected_help_msg = f'Cut along Y created: {ycut_name}' expected_bins = [100, 1, 1] if transpose else [1, 100, 1] expected_calls = [ call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=ycut_name) ] elif export_type == 'c': expected_help_msg = f'Cuts along X/Y created: {xcut_name} & {ycut_name}' expected_bins = [100, 1, 1] if transpose else [1, 100, 1] expected_calls = [ call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=xcut_name), call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=ycut_name) ] mock_binmd.assert_has_calls(expected_calls, any_order=True) if export_type == 'r': if transpose: mock_transposemd.assert_called_once() else: mock_transposemd.assert_not_called() else: if export_type == 'x': index = 1 if transpose else 0 expected_calls = [ call(InputWorkspace=xcut_name, OutputWorkspace=xcut_name, Axes=[index]) ] elif export_type == 'y': index = 0 if transpose else 1 expected_calls = [ call(InputWorkspace=ycut_name, OutputWorkspace=ycut_name, Axes=[index]) ] elif export_type == 'c': xindex = 1 if transpose else 0 yindex = 0 if transpose else 1 expected_calls = [ call(InputWorkspace=xcut_name, OutputWorkspace=xcut_name, Axes=[xindex]), call(InputWorkspace=ycut_name, OutputWorkspace=ycut_name, Axes=[yindex]) ] mock_transposemd.assert_has_calls(expected_calls, any_order=True) self.assertEqual(expected_help_msg, help_msg) mock_binmd.reset_mock() mock_transposemd.reset_mock()