def test_on_rows_removed_updates_view(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) row_0 = [ '74040', '', '74040', '', '74040', '', '74040', '', '74040', '', '74040', '', 'test_reduction', 'user_file', '1.2', '' ] row_1 = [ '74041', '', '74041', '', '74041', '', '74041', '', '74041', '', '74041', '', 'test_reduction', 'user_file', '1.2', '' ] row_2 = [ '74042', '', '74042', '', '74042', '', '74042', '', '74042', '', '74042', '', 'test_reduction', 'user_file', '1.2', '' ] row_3 = [ '74043', '', '74043', '', '74043', '', '74043', '', '74043', '', '74043', '', 'test_reduction', 'user_file', '1.2', '' ] presenter._table_model.add_table_entry(0, TableIndexModel(*row_0)) presenter._table_model.add_table_entry(1, TableIndexModel(*row_1)) presenter._table_model.add_table_entry(2, TableIndexModel(*row_2)) presenter._table_model.add_table_entry(3, TableIndexModel(*row_3)) presenter.update_view_from_table_model = mock.MagicMock() rows = [0, 2] presenter.on_rows_removed(rows) presenter.update_view_from_table_model.assert_called_once_with()
def test_that_all_columns_shown_when_multi_period_is_true(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) presenter.on_multiperiod_changed(True) presenter._view.show_period_columns.assert_called_once_with()
def setUp(self): # Backup properties that the tests or GUI may change self._backup_facility = config["default.facility"] self._backup_instrument = config["default.instrument"] self._backup_datasearch_dirs = config["datasearch.directories"] self._backup_save_dir = config["defaultsave.directory"] config["default.facility"] = "ISIS" self._mock_model = mock.create_autospec(StateGuiModel, spec_set=True) self._mock_table = mock.create_autospec(TableModel, spec_set=True) self._mock_csv_parser = mock.create_autospec(BatchCsvParser, spec_set=True) self._mock_view = mock.Mock() self._mock_model.instrument = SANSInstrument.SANS2D # TODO, this top level presenter should not be creating sub presenters, instead we should use # TODO an observer pattern and common interface to exchange messages. However for the moment # TODO we will skip patching each self.presenter = RunTabPresenter(SANSFacility.ISIS, model=self._mock_model, table_model=self._mock_table, view=self._mock_view) # The beam centre presenter will run QThreads which leads to flaky tests, so mock out self.presenter._beam_centre_presenter = mock.Mock() self.presenter._masking_table_presenter = mock.Mock() self.presenter._workspace_diagnostic_presenter = mock.Mock() self.presenter._csv_parser = self._mock_csv_parser # Allows us to use mock objs as the method tries to directly use int/floats self.presenter.update_view_from_table_model = mock.Mock()
def test_on_erase_rows_clears_rows_from_table_model(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[1, 2]) presenter.set_view(view) test_row_0 = [ 'SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '', '', '', '' ] empty_row = TableModel.create_empty_row() presenter.on_row_inserted(0, test_row_0) presenter.on_row_inserted(1, test_row_0) presenter.on_row_inserted(2, test_row_0) presenter.on_erase_rows() self.assertEqual(presenter._table_model.get_number_of_rows(), 3) self.assertEqual( presenter._table_model.get_table_entry(0).to_list(), test_row_0) empty_row.id = 3 self.assertEqual( presenter._table_model.get_table_entry(1).__dict__, empty_row.__dict__) empty_row.id = 4 self.assertEqual( presenter._table_model.get_table_entry(2).__dict__, empty_row.__dict__)
def test_on_erase_rows_updates_view(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[1, 2]) presenter.set_view(view) test_row_0 = [ 'SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '', '', '', '' ] presenter.on_row_inserted(0, test_row_0) presenter.on_row_inserted(1, test_row_0) presenter.on_row_inserted(2, test_row_0) presenter.update_view_from_table_model = mock.MagicMock() presenter.on_erase_rows() self.assertEqual(presenter._table_model._table_entries[0].to_list(), [ 'SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '', '', '', '' ]) self.assertEqual(presenter._table_model._table_entries[1].to_list(), [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ]) self.assertEqual(presenter._table_model._table_entries[2].to_list(), [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ])
def test_on_row_removed_removes_correct_row(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) row_0 = [ '74040', '', '74040', '', '74040', '', '74040', '', '74040', '', '74040', '', 'test_reduction', 'user_file', '1.2', '' ] row_1 = [ '74041', '', '74041', '', '74041', '', '74041', '', '74041', '', '74041', '', 'test_reduction', 'user_file', '1.2', '' ] row_2 = [ '74042', '', '74042', '', '74042', '', '74042', '', '74042', '', '74042', '', 'test_reduction', 'user_file', '1.2', '' ] row_3 = [ '74043', '', '74043', '', '74043', '', '74043', '', '74043', '', '74043', '', 'test_reduction', 'user_file', '1.2', '' ] presenter._table_model.add_table_entry(0, TableIndexModel(*row_0)) presenter._table_model.add_table_entry(1, TableIndexModel(*row_1)) presenter._table_model.add_table_entry(2, TableIndexModel(*row_2)) presenter._table_model.add_table_entry(3, TableIndexModel(*row_3)) rows = [0, 2] presenter.on_rows_removed(rows) self.assertEqual(presenter._table_model.get_number_of_rows(), 2) model_row_0 = presenter._table_model.get_table_entry(0) self.assertEqual(model_row_0, TableIndexModel(*row_1)) model_row_1 = presenter._table_model.get_table_entry(1) self.assertEqual(model_row_1, TableIndexModel(*row_3))
def test_that_period_columns_hidden_when_multi_period_is_false(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) presenter.on_multiperiod_changed(False) presenter._view.hide_period_columns.assert_called_once_with()
def test_on_paste_rows_requested_replaces_first_row_and_removes_rest_if_multiple_rows_selected( self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[0, 2]) presenter.set_view(view) test_row_0 = [ 'SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '' ] test_row_1 = [ 'SANS2D00022024', '', '', '', '', '', '', '', '', '', '', '', 'test_file2', '', '1.0', '' ] test_row_2 = [ 'SANS2D00022024', '', '', '', '', '', '', '', '', '', '', '', 'test_file3', '', '1.0', '' ] presenter.on_row_inserted(0, test_row_0) presenter.on_row_inserted(1, test_row_1) presenter.on_row_inserted(2, test_row_2) presenter._clipboard = [test_row_2] presenter.on_paste_rows_requested() self.assertEqual(presenter._table_model.get_number_of_rows(), 2) self.assertEqual( presenter._table_model.get_table_entry(0).to_list(), test_row_2) self.assertEqual( presenter._table_model.get_table_entry(1).to_list(), test_row_1)
def test_on_insert_row_adds_row_to_table_model_after_selected_row(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[0]) presenter.set_view(view) test_row_0 = [ 'SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '' ] test_row_1 = [ 'SANS2D00022024', '', '', '', '', '', '', '', '', '', '', '', 'test_file2', '', '1.0', '' ] presenter.on_row_inserted(0, test_row_0) presenter.on_row_inserted(1, test_row_1) presenter.on_insert_row() self.assertEqual(presenter._table_model.get_number_of_rows(), 3) self.assertEqual( presenter._table_model.get_table_entry(1).to_list(), [''] * 16) self.assertEqual( presenter._table_model.get_table_entry(0).to_list(), test_row_0) self.assertEqual( presenter._table_model.get_table_entry(2).to_list(), test_row_1)
def test_table_model_is_initialised_upon_presenter_creation(self): presenter = RunTabPresenter(SANSFacility.ISIS) expected_table_model = TableModel() expected_table_model.subscribe_to_model_changes(presenter) expected_table_model.subscribe_to_model_changes(presenter._masking_table_presenter) expected_table_model.subscribe_to_model_changes(presenter._beam_centre_presenter) self.maxDiff = None self.assertEqual(presenter._table_model, expected_table_model)
def _get_files_and_mock_presenter(content): batch_file_path = save_to_csv(content) user_file_path = create_user_file(sample_user_file) view, _, _ = create_mock_view(user_file_path, batch_file_path) # We just use the sample_user_file since it exists. view.get_mask_file = mock.MagicMock(return_value=user_file_path) presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(view) return batch_file_path, user_file_path, presenter, view
def test_fails_silently_when_user_file_does_not_exist(self): view, _, _ = create_mock_view("non_existent_user_file") presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(view) try: presenter.on_user_file_load() has_raised = False except: # noqa has_raised = True self.assertFalse(has_raised)
def test_that_process_selected_does_nothing_if_no_states_selected(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[]) presenter.set_view(view) presenter._process_rows = mock.MagicMock() presenter.on_process_selected_clicked() self.assertEqual( presenter._process_rows.call_count, 0, "Expected presenter._process_rows to not have been called. Called {} times." .format(presenter._process_rows.call_count))
def test_that_notify_progress_updates_state_and_tooltip_of_row(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() presenter.set_view(view) test_row_0 = ['SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', ''] presenter.on_row_inserted(0, test_row_0) presenter.notify_progress(0, [], []) self.assertEqual(presenter._table_model.get_table_entry(0).row_state, RowState.Processed) self.assertEqual(presenter._table_model.get_table_entry(0).tool_tip, '')
def test_notify_progress_increments_progress(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() presenter.set_view(view) test_row_0 = [ 'SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '' ] presenter.on_row_inserted(0, test_row_0) presenter.notify_progress(0) self.assertEqual(presenter.progress, 1) self.assertEqual(presenter._view.progress_bar_value, 1)
def test_setup_instrument_specific_settings(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) presenter._beam_centre_presenter = mock.MagicMock() presenter._workspace_diagnostic_presenter = mock.MagicMock() instrument = SANSInstrument.LOQ presenter._setup_instrument_specific_settings(SANSInstrument.LOQ) presenter._view.set_instrument_settings.called_once_with(instrument) presenter._beam_centre_presenter.on_update_instrument.called_once_with(instrument) presenter._workspace_diagnostic_presenter.called_once_with(instrument)
def test_that_table_not_exported_if_table_is_empty(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() presenter.set_view(view) presenter._export_table = mock.MagicMock() presenter.on_export_table_clicked() self.assertEqual( presenter._export_table.call_count, 0, "_export table should not have been called." " It was called {} times.".format( presenter._export_table.call_count))
def test_on_cut_rows_requested_updates_clipboard(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[0]) presenter.set_view(view) test_row = ['SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '', '', '', ''] presenter.on_row_inserted(0, test_row) presenter.on_cut_rows_requested() self.assertEqual(presenter._clipboard, [test_row])
def test_observers_subscribed_to(self): mocked_view_observers = mock.create_autospec(SansGuiObservable()) self._mock_view.get_observable = mock.Mock( return_value=mocked_view_observers) presenter = RunTabPresenter(facility=SANSFacility.ISIS, run_tab_model=self.mock_run_tab_model, view=self._mock_view) self._mock_view.get_observable.assert_called_once() mocked_view_observers.reduction_dim.add_subscriber.assert_called_once_with( presenter._observers.reduction_dim) mocked_view_observers.save_options.add_subscriber.assert_called_once_with( presenter._observers.save_options)
def _get_files_and_mock_presenter(self, content, is_multi_period=True, row_user_file_path = ""): batch_parser = mock.MagicMock() batch_parser.parse_batch_file = mock.MagicMock(return_value=content) self.BatchCsvParserMock.return_value = batch_parser batch_file_path = 'batch_file_path' user_file_path = create_user_file(sample_user_file) view, _, _ = create_mock_view(user_file_path, batch_file_path, row_user_file_path) # We just use the sample_user_file since it exists. view.get_mask_file = mock.MagicMock(return_value=user_file_path) view.is_multi_period_view = mock.MagicMock(return_value=is_multi_period) presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(view) return batch_file_path, user_file_path, presenter, view
def test_that_process_all_ignores_selected_rows(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[0, 3, 4]) presenter._table_model.get_number_of_rows = mock.MagicMock(return_value=7) presenter.set_view(view) presenter._table_model.reset_row_state = mock.MagicMock() presenter.on_process_all_clicked() self.assertEqual( presenter._table_model.reset_row_state.call_count, 7, "Expected reset_row_state to have been called 7 times. Called {} times.".format( presenter._table_model.reset_row_state.call_count))
def test_batch_file_dir_not_added_to_config_if_batch_file_load_fails(self): presenter = RunTabPresenter(SANSFacility.ISIS) user_file_path = create_user_file(sample_user_file) view, settings_diagnostic_tab, masking_table = create_mock_view( user_file_path, "A/Path/batch_file.csv") presenter.set_view(view) presenter.on_batch_file_load() config_dirs = config["datasearch.directories"] result = "A/Path/" in config_dirs self.assertFalse( result, "We do not expect A/Path/ to be added to config, " "datasearch.directories is now {}".format(config_dirs))
def test_on_insert_row_updates_table_model(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) row = [ '74044', '', '74044', '', '74044', '', '74044', '', '74044', '', '74044', '', 'test_reduction', 'user_file', '1.2', '' ] index = 0 presenter.on_row_inserted(index, row) self.assertEqual(presenter._table_model.get_number_of_rows(), 1) model_row = presenter._table_model.get_table_entry(0) self.assertEqual(model_row, TableIndexModel(*row))
def __init__(self, facility): super(MantidQt.MantidWidgets.DataProcessor.DataProcessorMainPresenter, self).__init__() self._view = None # Algorithm details self._gui_algorithm_name = None self._white_list = None self._black_list = None self._facility = facility # Set of sub presenters self._presenters = {} self._presenters.update({PresenterEnum.RunTabPresenter: RunTabPresenter(facility=self._facility)})
def test_add_row_to_table_model_adds_row_to_table_model(self): presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(mock.MagicMock()) parsed_data = BATCH_FILE_TEST_CONTENT_2 for item, row in enumerate(parsed_data): presenter._add_row_to_table_model(row, item) table_entry_0 = presenter._table_model.get_table_entry(0) self.assertEqual(table_entry_0.output_name, 'test_file') self.assertEqual(table_entry_0.sample_scatter, 'SANS2D00022024') self.assertEqual(table_entry_0.sample_thickness, 1.0) table_entry_1 = presenter._table_model.get_table_entry(1) self.assertEqual(table_entry_1.output_name, 'test_file2')
def test_that_checks_default_user_file(self): # Setup presenter and mock view view, settings_diagnostic_tab, _ = create_mock_view("") presenter = RunTabPresenter(SANSFacility.ISIS) presenter.set_view(view) self.assertEqual( presenter._view.set_out_default_user_file.call_count, 1, "Expected mock to have been called once. Called {} times.".format( presenter._view.set_out_default_user_file.call_count)) self.assertEqual( presenter._view._call_settings_listeners.call_count, 0, "Expected mock to not have been called. Called {} times.".format( presenter._view._call_settings_listeners.call_count))
def test_on_insert_row_updates_view(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() view.get_selected_rows = mock.MagicMock(return_value=[0]) presenter.set_view(view) test_row_0 = ['SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', '', '', '', ''] test_row_1 = ['SANS2D00022024', '', '', '', '', '', '', '', '', '', '', '', 'test_file2', '', '1.0', '', '', '', ''] presenter.on_row_inserted(0, test_row_0) presenter.on_row_inserted(1, test_row_1) presenter.update_view_from_table_model = mock.MagicMock() presenter.on_insert_row() presenter.update_view_from_table_model.assert_called_once_with()
def test_that_notify_progress_updates_state_and_tooltip_of_row_for_scale_and_shift(self): presenter = RunTabPresenter(SANSFacility.ISIS) view = mock.MagicMock() presenter.set_view(view) test_row_0 = ['SANS2D00022024', '', 'SANS2D00022048', '', 'SANS2D00022048', '', '', '', '', '', '', '', 'test_file', '', '1.0', ''] presenter.on_row_inserted(0, test_row_0) presenter.notify_progress(0, [0.0], [1.0]) self.assertEqual(presenter._table_model.get_table_entry(0).row_state, RowState.Processed) self.assertEqual(presenter._table_model.get_table_entry(0).options_column_model.get_options_string(), 'MergeScale=1.0, MergeShift=0.0') self.assertEqual(presenter.progress, 1) self.assertEqual(presenter._view.progress_bar_value, 1)
def test_fails_silently_when_batch_file_does_not_exist(self): presenter = RunTabPresenter(SANSFacility.ISIS) user_file_path = create_user_file(sample_user_file) view, settings_diagnostic_tab, masking_table = create_mock_view( user_file_path, "non_existent_batch_file") presenter.set_view(view) try: presenter.on_batch_file_load() has_raised = False except: # noqa has_raised = True self.assertFalse(has_raised) # Clean up self._remove_files(user_file_path=user_file_path)
def test_that_process_all_ignores_empty_rows(self): presenter = RunTabPresenter(SANSFacility.ISIS) table_model = TableModel() row_entry0 = [''] * 16 row_entry1 = [ '74040', '', '74040', '', '74040', '', '74040', '', '74040', '', '74040', '', 'test_reduction', 'user_file', '1.2', '' ] table_model.add_table_entry(0, TableIndexModel(*row_entry0)) table_model.add_table_entry(1, TableIndexModel(*row_entry1)) presenter._table_model = table_model presenter._process_rows = mock.MagicMock() presenter.on_process_all_clicked() presenter._process_rows.assert_called_with([1])