def test_do_action_plot_success_error_plot(self, ws, view, twd): col_as_x = 1 col_as_y = 2 col_as_y_err = 3 expected_x_data = twd.model.get_column(col_as_x) expected_y_data = twd.model.get_column(col_as_y) expected_y_err_data = twd.model.get_column(col_as_y_err) view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, col_as_x)] twd.action_set_as_x() view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, col_as_y_err)] twd.action_set_as_y_err(col_as_y, '0') view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, col_as_y)] twd.action_plot(PlotType.LINEAR_WITH_ERR) twd.plot.subplots.assert_called_once_with(subplot_kw={'projection': 'mantid'}) twd.plot.mock_fig.canvas.set_window_title.assert_called_once_with(twd.model.get_name()) twd.plot.mock_ax.set_xlabel.assert_called_once_with(twd.model.get_column_header(col_as_x)) col_y_name = twd.model.get_column_header(col_as_y) twd.plot.mock_ax.set_ylabel.assert_called_once_with(col_y_name) twd.plot.mock_ax.errorbar.assert_called_once_with(expected_x_data, expected_y_data, label=TableWorkspaceDisplay.COLUMN_DISPLAY_LABEL.format( col_y_name), yerr=expected_y_err_data) twd.plot.mock_fig.show.assert_called_once_with() twd.plot.mock_ax.legend.assert_called_once_with()
def setup_mock_selection(self, mock_table, num_selected_rows=None, num_selected_cols=None): """ :type mock_table: MockQTableView :type num_selected_rows: int|None :type num_selected_cols: int|None """ mock_selected = [] if num_selected_rows is not None: for i in range(num_selected_rows): mock_selected.append(MockQModelIndex(i, 1)) mock_table.mock_selection_model.selectedRows = Mock( return_value=mock_selected) mock_table.mock_selection_model.selectedColumns = Mock() elif num_selected_cols is not None: for i in range(num_selected_cols): mock_selected.append(MockQModelIndex(1, i)) mock_table.mock_selection_model.selectedRows = Mock() mock_table.mock_selection_model.selectedColumns = Mock( return_value=mock_selected) else: mock_table.mock_selection_model.selectedRows = Mock() mock_table.mock_selection_model.selectedColumns = Mock() return mock_selected
def test_do_action_plot_multiple_y_success(self, ws, view, twd): col_as_x = 1 col_as_y1 = 2 col_as_y2 = 3 expected_x_data = twd.model.get_column(col_as_x) expected_y1_data = twd.model.get_column(col_as_y1) expected_y2_data = twd.model.get_column(col_as_y2) view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, col_as_x)] twd.action_set_as_x() view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, col_as_y1), MockQModelIndex(1, col_as_y2)] twd.action_plot(PlotType.LINEAR) twd.plot.subplots.assert_called_once_with(subplot_kw={'projection': 'mantid'}) twd.plot.mock_fig.canvas.set_window_title.assert_called_once_with(twd.model.get_name()) twd.plot.mock_ax.set_xlabel.assert_called_once_with(twd.model.get_column_header(col_as_x)) col_y1_name = twd.model.get_column_header(col_as_y1) col_y2_name = twd.model.get_column_header(col_as_y2) twd.plot.mock_ax.set_ylabel.assert_has_calls([call(col_y1_name), call(col_y2_name)]) twd.plot.mock_ax.plot.assert_has_calls([ call(expected_x_data, expected_y1_data, label=TableWorkspaceDisplay.COLUMN_DISPLAY_LABEL.format(col_y1_name)), call(expected_x_data, expected_y2_data, label=TableWorkspaceDisplay.COLUMN_DISPLAY_LABEL.format(col_y2_name))]) twd.plot.mock_fig.show.assert_called_once_with() twd.plot.mock_ax.legend.assert_called_once_with()
def test_do_action_plot_with_errors_missing_yerr_for_y_column(self, ws, view, twd): view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1)] twd.action_set_as_x() view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 2)] twd.action_set_as_y() twd.action_plot(PlotType.LINEAR_WITH_ERR) view.show_warning.assert_called_once_with(TableWorkspaceDisplay.NO_ASSOCIATED_YERR_FOR_EACH_Y_MESSAGE)
def test_action_plot_x_marked_but_not_selected(self, ws, view, twd, mock_do_plot): """ Test that the plot is successful if there is no X in the selection, but a column is marked X. The selection contains only an unmarked column, which is used as the Y data """ view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1)] # set only the first column to be X twd.action_set_as_x() # change the selection to a second column, that should be used for Y, but is not marked as anything view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 2)] twd.action_plot(PlotType.LINEAR) mock_do_plot.assert_called_once_with([2], 1, PlotType.LINEAR)
def test_action_plot_x_in_selection(self, ws, view, twd, mock_do_plot): """ Test that the plot is successful if there is an X in the selection, and an unmarked column: which is used as the Y data """ view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1)] # set only the first column to be X twd.action_set_as_x() # add a second selected column, that should be used for Y view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1), MockQModelIndex(1, 2)] twd.action_plot(PlotType.LINEAR) mock_do_plot.assert_called_once_with([2], 1, PlotType.LINEAR)
def _check_correct_data_is_displayed(self, model_type, column, mock_data, row): ws = MockWorkspace(read_return=mock_data) model = MatrixWorkspaceTableViewModel(ws, model_type) index = MockQModelIndex(row, column) output = model.data(index, Qt.DisplayRole) model.relevant_data.assert_called_once_with(row) self.assertEqual(str(mock_data[column]), output)
def test_action_set_as_y_err(self, ws, view, twd): view.mock_selection_model.selectedColumns = Mock(return_value=[MockQModelIndex(1, 1)]) twd.action_set_as_y_err(2, "0") self.assertEqual(1, len(twd.model.marked_columns.as_y_err)) err_col = twd.model.marked_columns.as_y_err[0] self.assertEqual(1, err_col.column) self.assertEqual(2, err_col.related_y_column) self.assertEqual("0", err_col.label_index)
def test_action_sort_peaks_ws(self, ws, view, twd, mock_SortPeaksWorkspace): view.mock_selection_model.selectedColumns = Mock(return_value=[MockQModelIndex(0, 0)]) ascending = True with patch('mantidqt.widgets.workspacedisplay.table.model.TableWorkspaceDisplayModel.is_peaks_workspace', return_value=True) as mock_is_peaks_workspace: twd.action_sort(ascending) mock_SortPeaksWorkspace.assert_called_once_with(InputWorkspace=twd.model.ws, OutputWorkspace=twd.model.ws, ColumnNameToSortBy="col0", SortAscending=ascending) mock_is_peaks_workspace.assert_called_once_with()
def test_action_copy_spectrum_values(self, mock_copy, mock_show_mouse_toast): mock_table = MockQTableView() # two rows are selected in different positions mock_indexes = [MockQModelIndex(0, 1), MockQModelIndex(3, 1)] mock_table.mock_selection_model.selectedRows = Mock( return_value=mock_indexes) mock_read = Mock(return_value=[43, 99]) expected_string = "43\t99\n43\t99" self.data_copier.copy_spectrum_values(mock_table, mock_read) mock_table.selectionModel.assert_called_once_with() mock_table.mock_selection_model.hasSelection.assert_called_once_with() mock_copy.assert_called_once_with(expected_string) mock_show_mouse_toast.assert_called_once_with( UserNotifier.COPY_SUCCESSFUL_MESSAGE)
def test_action_plot_column_against_itself(self, ws, view, twd): """ For example: mark a column as X and then try to do Right click -> plot -> line on it, using it as Y this will fail as it's the same column """ view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1)] # set only the first column to be X twd.action_set_as_x() # change the selection to a second column, that should be used for Y, but is not marked as anything twd.action_plot(PlotType.LINEAR) view.show_warning.assert_called_once_with(TableWorkspaceDisplay.CANNOT_PLOT_AGAINST_SELF_MESSAGE)
def test_action_copy_bin_values(self, mock_copy, mock_show_mouse_toast): mock_table = MockQTableView() # two columns are selected at different positions mock_indexes = [MockQModelIndex(0, 0), MockQModelIndex(0, 3)] mock_table.mock_selection_model.selectedColumns = Mock( return_value=mock_indexes) # change the mock ws to have 3 histograms num_hist = 3 mock_read = Mock(return_value=[83, 11, 33, 70]) expected_string = "83\t70\n83\t70\n83\t70" self.data_copier.copy_bin_values(mock_table, mock_read, num_hist) mock_table.selectionModel.assert_called_once_with() mock_table.mock_selection_model.hasSelection.assert_called_once_with() mock_copy.assert_called_once_with(expected_string) mock_show_mouse_toast.assert_called_once_with( UserNotifier.COPY_SUCCESSFUL_MESSAGE)
def test_do_action_plot__plot_func_throws_error(self, ws, view, twd, mock_logger_error): mock_plot_function = Mock() error_message = "See bottom of keyboard for HEALTH WARNING" mock_plot_function.side_effect = ValueError(error_message) with patch( 'mantidqt.widgets.workspacedisplay.table.presenter.TableWorkspaceDisplay._get_plot_function_from_type') \ as mock_get_plot_function_from_type: mock_get_plot_function_from_type.return_value = mock_plot_function view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1)] twd.action_set_as_x() view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 2)] twd.action_plot(PlotType.LINEAR) view.show_warning.assert_called_once_with( TableWorkspaceDisplay.PLOT_FUNCTION_ERROR_MESSAGE.format(error_message), TableWorkspaceDisplay.INVALID_DATA_WINDOW_TITLE) mock_logger_error.assert_called_once_with( TableWorkspaceDisplay.PLOT_FUNCTION_ERROR_MESSAGE.format(error_message)) self.assertNotCalled(twd.plot.mock_fig.show) self.assertNotCalled(twd.plot.mock_ax.legend)
def test_action_copy_spectrum_values(self, ws, view, presenter, mock_copy, mock_show_mouse_toast): mock_table = MockQTableView() # two rows are selected in different positions mock_indexes = [MockQModelIndex(0, 1), MockQModelIndex(3, 1)] mock_table.mock_selection_model.selectedRows = Mock( return_value=mock_indexes) mock_read = Mock(return_value=[43, 99]) presenter._get_ws_read_from_type = Mock(return_value=mock_read) expected_string = "43\t99\n43\t99" presenter.action_copy_spectrum_values(mock_table) mock_table.selectionModel.assert_called_once_with() mock_table.mock_selection_model.hasSelection.assert_called_once_with() mock_copy.assert_called_once_with(expected_string) mock_show_mouse_toast.assert_called_once_with( MatrixWorkspaceDisplay.COPY_SUCCESSFUL_MESSAGE) presenter._get_ws_read_from_type.assert_called_once_with( MatrixWorkspaceTableViewModelType.x)
def test_copy_cells(self, mock_copy, mock_show_mouse_toast): mock_table = MockQTableView() # two columns are selected at different positions mock_index = MockQModelIndex(None, None) mock_table.mock_selection_model.currentIndex = Mock( return_value=mock_index) self.data_copier.copy_cells(mock_table) mock_table.selectionModel.assert_called_once_with() self.assertEqual(1, mock_copy.call_count) self.assertEqual(9, mock_index.sibling.call_count) mock_show_mouse_toast.assert_called_once_with( UserNotifier.COPY_SUCCESSFUL_MESSAGE)
def test_action_copy_bin_values(self, ws, view, presenter, mock_copy, mock_show_mouse_toast): mock_table = MockQTableView() # two columns are selected at different positions mock_indexes = [MockQModelIndex(0, 0), MockQModelIndex(0, 3)] mock_table.mock_selection_model.selectedColumns = Mock( return_value=mock_indexes) # change the mock ws to have 3 histograms ws.getNumberHistograms = Mock(return_value=3) mock_read = Mock(return_value=[83, 11, 33, 70]) presenter._get_ws_read_from_type = Mock(return_value=mock_read) expected_string = "83\t70\n83\t70\n83\t70" presenter.action_copy_bin_values(mock_table) mock_table.selectionModel.assert_called_once_with() mock_table.mock_selection_model.hasSelection.assert_called_once_with() mock_copy.assert_called_once_with(expected_string) mock_show_mouse_toast.assert_called_once_with( MatrixWorkspaceDisplay.COPY_SUCCESSFUL_MESSAGE) presenter._get_ws_read_from_type.assert_called_once_with( MatrixWorkspaceTableViewModelType.x)
def test_action_copy_cell(self, ws, view, presenter, mock_copy, mock_show_mouse_toast): mock_table = MockQTableView() # two columns are selected at different positions mock_index = MockQModelIndex(None, None) mock_table.mock_selection_model.currentIndex = Mock( return_value=mock_index) presenter.action_copy_cells(mock_table) mock_table.selectionModel.assert_called_once_with() self.assertEqual(1, mock_copy.call_count) self.assertEqual(9, mock_index.sibling.call_count) mock_show_mouse_toast.assert_called_once_with( MatrixWorkspaceDisplay.COPY_SUCCESSFUL_MESSAGE)
def wrapper(self, *args): ws = MockWorkspace() view = Mock(spec=TableWorkspaceDisplayView) container = Mock(spec=StatusBarView) container.status_bar = Mock(spec=QStatusBar) if add_selection_model: mock_selection_model = MockQSelectionModel(has_selection=True) mock_selection_model.selectedRows = Mock( return_value=[MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3)]) mock_selection_model.selectedColumns = Mock( return_value=[MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3)]) view.mock_selection_model = mock_selection_model view.selectionModel.return_value = mock_selection_model twd = TableWorkspaceDisplay(ws, view=view, container=container) if add_plot: twd.plot = MockPlotLib() return func(self, ws, view, twd, *args)
def setup_common_for_test_data(): """ Common configuration of variables and mocking for testing MatrixWorkspaceDisplayTableViewModel's data and headerData functions """ # Create some mock data for the mock workspace row = 2 column = 2 # make a workspace with 0s mock_data = [0] * 10 # set one of them to be not 0 mock_data[column] = 999 model_type = MatrixWorkspaceTableViewModelType.x # pass onto the MockWorkspace so that it returns it when read from the TableViewModel ws = MockWorkspace(read_return=mock_data) ws.hasMaskedBins = Mock(return_value=True) ws.maskedBinsIndices = Mock(return_value=[column]) model = MatrixWorkspaceTableViewModel(ws, model_type) # The model retrieves the spectrumInfo object, and our MockWorkspace has already given it # the MockSpectrumInfo, so all that needs to be done here is to set up the correct method Mocks model.ws_spectrum_info.hasDetectors = Mock(return_value=True) index = MockQModelIndex(row, column) return ws, model, row, index
def setUp(self): self.mock_status_bar = MockQStatusBar() self.mock_clipboard = MockQClipboard() self.mock_clipboard.setText = Mock() self.data_copier = DataCopier(self.mock_status_bar) mock_selection_model = MockQSelectionModel(has_selection=True) mock_selection_model.selectedRows = Mock(return_value=[ MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3) ]) mock_selection_model.selectedColumns = Mock(return_value=[ MockQModelIndex(1, 1), MockQModelIndex(2, 2), MockQModelIndex(3, 3) ]) self.table = Mock(spec=TableWorkspaceDisplayView) self.table.mock_selection_model = mock_selection_model
def test_action_set_as_y_err_failed_to_create_ErrorColumn(self, ws, view, twd): view.mock_selection_model.selectedColumns = Mock(return_value=[MockQModelIndex(1, 1)]) # this will fail as we're trying to set an YErr column for itself -> (try to set col 1 to be YERR for col 1) twd.action_set_as_y_err(1, "0") view.show_warning.assert_called_once_with(ErrorColumn.CANNOT_SET_Y_TO_BE_OWN_YERR_MESSAGE)
def test_action_sort_table_ws(self, ws, view, twd, mock_SortTableWorkspace): view.mock_selection_model.selectedColumns = Mock(return_value=[MockQModelIndex(0, 0)]) ascending = True twd.action_sort(ascending) mock_SortTableWorkspace.assert_called_once_with(InputWorkspace=twd.model.ws, OutputWorkspace=twd.model.ws, Columns="col0", Ascending=ascending)
def test_action_plot_more_than_one_x(self, ws, view, twd): view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1), MockQModelIndex(1, 2)] twd.action_set_as_x() twd.action_plot(PlotType.LINEAR) view.mock_selection_model.selectedColumns.assert_has_calls([call(), call()]) view.show_warning.assert_called_once_with(TableWorkspaceDisplay.TOO_MANY_SELECTED_FOR_X)
def test_action_plot_selection_without_x(self, ws, view, twd, mock_do_plot): view.mock_selection_model.selectedColumns.return_value = [MockQModelIndex(1, 1)] twd.action_plot(PlotType.LINEAR) view.show_warning.assert_called_once_with(TableWorkspaceDisplay.NO_COLUMN_MARKED_AS_X)