def assert_call_as_expected(mock_ws, transpose, export_type, is_spectra, is_ragged): model = SliceViewerModel(mock_ws) slicepoint, bin_params, dimension_indices = MagicMock(), MagicMock( ), MagicMock() help_msg = model.export_cuts_to_workspace( slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices, export_type) if export_type == 'c': if is_spectra: mock_extract_spectra.assert_called_once() if is_ragged: mock_rebin.assert_called_once() mock_sum_spectra.assert_called_once() else: if is_ragged: self.assertEqual(2, mock_rebin.call_count) else: mock_rebin.assert_called_once() self.assertEqual(1, mock_transpose.call_count) self.assertEqual(1, mock_extract_spectra.call_count) self.assertEqual( 'Cuts along X/Y created: mock_ws_cut_x & mock_ws_cut_y', help_msg) elif export_type == 'x': mock_extract_spectra.assert_called_once() if is_ragged: self.assertEqual(1, mock_rebin.call_count) if is_spectra: mock_sum_spectra.assert_called_once() else: mock_transpose.assert_not_called() self.assertEqual('Cut along X created: mock_ws_cut_x', help_msg) elif export_type == 'y': mock_extract_spectra.assert_called_once() mock_transpose.assert_called_once() if is_ragged: self.assertEqual(2, mock_rebin.call_count) else: self.assertEqual(1, mock_rebin.call_count) self.assertEqual('Cut along Y created: mock_ws_cut_y', help_msg) mock_transpose.reset_mock() mock_rebin.reset_mock() mock_extract_spectra.reset_mock() mock_sum_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()