class HomeTabRunInfoPresenterTest(unittest.TestCase): def setUp(self): self._qapp = mock_widget.mockQapp() self.obj = QtGui.QWidget() self.context = MuonDataContext() self.view = HomeRunInfoWidgetView(self.obj) self.model = HomeRunInfoWidgetModel(self.context) self.presenter = HomeRunInfoWidgetPresenter(self.view, self.model) self.view.warning_popup = mock.MagicMock() def tearDown(self): self.obj = None def test_runinfo_correct(self): file_path = FileFinder.findRuns('MUSR00022725.nxs')[0] ws, run, filename = load_utils.load_workspace_from_filename(file_path) self.context._loaded_data.remove_data(run=run) self.context._loaded_data.add_data(run=run, workspace=ws, filename=filename) self.context.update_current_data() test_pair = MuonPair('test_pair', 'top', 'bottom', alpha=0.75) self.context.add_pair(pair=test_pair) self.presenter.update_view_from_model() expected_string_list = ['Instrument:MUSR', 'Run:22725', 'Title:FeTeSeT=1F=100', 'Comment:FCfirstsample', 'Start:2009-03-24T04:18:58', 'End:2009-03-24T04:56:26', 'Counts(MEv):20.076704', 'GoodFrames:88540', 'CountsperGoodFrame:226.753', 'CountsperGoodFrameperdet:3.543', 'AverageTemperature(K):2.53386', 'SampleTemperature(K):1.0', 'SampleMagneticField(G):100.0'] self.assertEqual(str(self.view.run_info_box.toPlainText()).replace(' ', '').splitlines(), expected_string_list)
def setUp(self): self._qapp = mock_widget.mockQapp() self.obj = QtGui.QWidget() self.context = MuonDataContext() self.view = HomeRunInfoWidgetView(self.obj) self.model = HomeRunInfoWidgetModel(self.context) self.presenter = HomeRunInfoWidgetPresenter(self.view, self.model) self.view.warning_popup = mock.MagicMock()
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonDataContext() self.model = PairingTableModel(data=self.data) self.view = PairingTableView(parent=self.obj) self.presenter = PairingTablePresenter(self.view, self.model) self.view.warning_popup = mock.Mock()
def setUp(self): self.loaded_data = MuonLoadData() self.context = MuonDataContext(self.loaded_data) self.context.instrument = 'EMU' filepath = FileFinder.findRuns('EMU00019489.nxs')[0] load_result, run, filename = load_workspace_from_filename(filepath) self.loaded_data.add_data(workspace=load_result, run=[run], filename=filename, instrument='EMU') self.context.current_runs = [[run]] self.context.update_current_data()
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.context = MuonDataContext() self.model = GroupingTabModel(data=self.context) self.grouping_table_view = GroupingTableView(parent=self.obj) self.grouping_table_widget = GroupingTablePresenter(self.grouping_table_view, self.model) self.pairing_table_view = PairingTableView(parent=self.obj) self.pairing_table_widget = PairingTablePresenter(self.pairing_table_view, self.model) self.add_three_groups() self.add_two_pairs() self.view = GroupingTabView(self.grouping_table_view, self.pairing_table_view, parent=self.obj) self.presenter = GroupingTabPresenter(self.view, self.model, self.grouping_table_widget, self.pairing_table_widget) self.view.display_warning_box = mock.MagicMock() self.grouping_table_view.warning_popup = mock.MagicMock() self.pairing_table_view.warning_popup = mock.MagicMock()
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.context = MuonDataContext() self.context.instrument = 'EMU' self.data = MuonLoadData() self.view = LoadRunWidgetView(parent=self.obj) self.model = LoadRunWidgetModel(self.data, self.context) self.presenter = LoadRunWidgetPresenter(self.view, self.model) self.model.load_workspace_from_filename = mock.Mock(return_value=([1, 2, 3], "currentRun.nxs", 1234)) self.view.warning_popup = mock.Mock() self.view.disable_load_buttons = mock.Mock() self.view.enable_load_buttons = mock.Mock() self.presenter.set_current_instrument("EMU") fileUtils.get_current_run_filename = mock.Mock(return_value="EMU0001234.nxs") patcher = mock.patch('Muon.GUI.Common.load_run_widget.load_run_model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start() self.load_utils_patcher.exception_message_for_failed_files.return_value = ''
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() ConfigService['default.instrument'] = 'MUSR' self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.context.instrument = 'MUSR' self.load_file_view = BrowseFileWidgetView(self.obj) self.load_run_view = LoadRunWidgetView(self.obj) self.load_file_model = BrowseFileWidgetModel(self.data, self.context) self.load_run_model = LoadRunWidgetModel(self.data, self.context) self.view = LoadWidgetView(parent=self.obj, load_file_view=self.load_file_view, load_run_view=self.load_run_view) self.presenter = LoadWidgetPresenter( self.view, LoadWidgetModel(self.data, self.context)) self.presenter.set_load_file_widget( BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model)) self.presenter.set_load_run_widget( LoadRunWidgetPresenter(self.load_run_view, self.load_run_model)) self.presenter.load_file_widget._view.warning_popup = mock.MagicMock() self.presenter.load_run_widget._view.warning_popup = mock.MagicMock() self.view.multiple_loading_check.setCheckState(1) self.presenter.handle_multiple_files_option_changed() self.runs = [15196, 15197] self.workspaces = [self.create_fake_workspace(1) for _ in self.runs] self.filenames = FileFinder.findRuns( 'MUSR00015196.nxs, MUSR00015197.nxs')
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.popup_patcher = mock.patch('Muon.GUI.Common.thread_model.warning') self.addCleanup(self.popup_patcher.stop) self.popup_mock = self.popup_patcher.start() self.load_patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_patcher.stop) self.load_mock = self.load_patcher.start() self.load_run_patcher = mock.patch( 'Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_run_patcher.stop) self.load_run_mock = self.load_run_patcher.start() self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.context.instrument = 'EMU' self.load_file_view = BrowseFileWidgetView(self.obj) self.load_run_view = LoadRunWidgetView(self.obj) self.load_file_model = BrowseFileWidgetModel(self.data, self.context) self.load_run_model = LoadRunWidgetModel(self.data, self.context) self.model = LoadWidgetModel(self.data, self.context) self.view = LoadWidgetView(parent=self.obj, load_run_view=self.load_run_view, load_file_view=self.load_file_view) self.presenter = LoadWidgetPresenter(view=self.view, model=self.model) self.presenter.set_load_file_widget( BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model)) self.presenter.set_load_run_widget( LoadRunWidgetPresenter(self.load_run_view, self.load_run_model)) self.presenter.load_run_widget.set_current_instrument('EMU') self.presenter.load_file_widget._view.warning_popup = mock.MagicMock() self.presenter.load_run_widget._view.warning_popup = mock.MagicMock() self.load_file_view.show_file_browser_and_return_selection = mock.Mock( return_value=["C:\\dir1\\EMU0001234.nxs"]) self.workspace_mock = self.create_fake_workspace(1) self.load_mock.return_value = (self.workspace_mock, 1234, "C:\\dir1\\EMU0001234.nxs") self.load_run_mock.return_value = (self.workspace_mock, 1234, "C:\\dir1\\EMU0001234.nxs") self.presenter.load_file_widget.on_browse_button_clicked() self.wait_for_thread(self.presenter.load_file_widget._load_thread) self.mock_loading_to_throw() file_utils.get_current_run_filename = mock.Mock( return_value="EMU0001234.nxs")
def setUp(self): self._qapp = mock_widget.mockQapp() self.obj = QtGui.QWidget() self.context = MuonDataContext() self.context.instrument = 'MUSR' self.view = InstrumentWidgetView(self.obj) self.view.set_instrument('MUSR', block=True) self.model = InstrumentWidgetModel(self.context) self.presenter = InstrumentWidgetPresenter(self.view, self.model) self.view.warning_popup = mock.MagicMock() self.view.instrument_changed_warning = mock.MagicMock(return_value=1)
def __init__(self, parent=None): super(MuonAnalysisGui, self).__init__(parent) self.setFocusPolicy(QtCore.Qt.StrongFocus) try: check_facility() except AttributeError as error: self.warning_popup(error.args[0]) # initialise the data storing classes of the interface self.loaded_data = MuonLoadData() self.context = MuonDataContext(load_data=self.loaded_data) # construct all the widgets. self.load_widget = LoadWidget(self.loaded_data, self.context, self) self.grouping_tab_widget = GroupingTabWidget(self.context) self.home_tab = HomeTabWidget(self.context, self) self.setup_tabs() self.help_widget = HelpWidget() central_widget = QtGui.QWidget() vertical_layout = QtGui.QVBoxLayout() vertical_layout.addWidget(self.load_widget.load_widget_view) vertical_layout.addWidget(self.tabs) vertical_layout.addWidget(self.help_widget.view) central_widget.setLayout(vertical_layout) self.setCentralWidget(central_widget) self.setWindowTitle("Muon Analysis version 2") self.home_tab.group_widget.pairAlphaNotifier.add_subscriber( self.grouping_tab_widget.group_tab_presenter.loadObserver) self.grouping_tab_widget.group_tab_presenter.groupingNotifier.add_subscriber( self.home_tab.home_tab_widget.groupingObserver) self.context.instrumentNotifier.add_subscriber( self.home_tab.home_tab_widget.instrumentObserver) self.context.instrumentNotifier.add_subscriber( self.load_widget.load_widget.instrumentObserver) self.context.instrumentNotifier.add_subscriber( self.grouping_tab_widget.group_tab_presenter.instrumentObserver) self.load_widget.load_widget.loadNotifier.add_subscriber( self.home_tab.home_tab_widget.loadObserver) self.load_widget.load_widget.loadNotifier.add_subscriber( self.grouping_tab_widget.group_tab_presenter.loadObserver)
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.context.instrument = 'EMU' self.load_file_view = BrowseFileWidgetView(self.obj) self.load_run_view = LoadRunWidgetView(self.obj) self.load_file_model = BrowseFileWidgetModel(self.data, self.context) self.load_run_model = LoadRunWidgetModel(self.data, self.context) self.presenter = LoadWidgetPresenter( LoadWidgetView(parent=self.obj, load_file_view=self.load_file_view, load_run_view=self.load_run_view), LoadWidgetModel(self.data, self.context)) self.presenter.set_load_file_widget( BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model)) self.presenter.set_load_run_widget( LoadRunWidgetPresenter(self.load_run_view, self.load_run_model)) self.filepath = FileFinder.findRuns('MUSR00022725.nxs')[0] self.load_patcher = mock.patch( 'Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_patcher.stop) self.load_mock = self.load_patcher.start() self.load_run_patcher = mock.patch( 'Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename' ) self.addCleanup(self.load_run_patcher.stop) self.load_run_mock = self.load_run_patcher.start() self.mock_workspace = self.create_fake_workspace(1) self.mock_loading_from_browse(self.mock_workspace, "C:\dir1\dir2\dir3\EMU0001234.nxs", 1234) file_utils.get_current_run_filename = mock.Mock( return_value="C:\dir1\dir2\dir3\EMU0001234.nxs") self.presenter.load_file_widget._view.warning_popup = mock.MagicMock() self.presenter.load_run_widget._view.warning_popup = mock.MagicMock() self.popup_patcher = mock.patch('Muon.GUI.Common.thread_model.warning') self.addCleanup(self.popup_patcher.stop) self.popup_mock = self.popup_patcher.start()
def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonLoadData() self.context = MuonDataContext(self.data) self.view = LoadRunWidgetView(parent=self.obj) self.model = LoadRunWidgetModel(self.data, self.context) self.presenter = LoadRunWidgetPresenter(self.view, self.model) self.view.warning_popup = mock.Mock() self.presenter.set_current_instrument("EMU") patcher = mock.patch( 'Muon.GUI.Common.load_run_widget.load_run_model.load_utils') self.addCleanup(patcher.stop) self.load_utils_patcher = patcher.start() self.load_utils_patcher.exception_message_for_failed_files.return_value = '' self.load_single_run()
def __init__(self, data=MuonDataContext()): self._data = data
def __init__(self, muon_data=MuonDataContext()): self._data = muon_data self._data.gui_variables['RebinType'] = 'None'
def __init__(self, muon_data=MuonDataContext()): self._data = muon_data
def setUp(self): self.context = MuonDataContext() self.context.instrument = 'EMU' self.model = LoadRunWidgetModel(MuonLoadData(), self.context)
class PairingTablePresenterTest(unittest.TestCase): def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonDataContext() self.add_three_groups_to_model() self.model = PairingTableModel(data=self.data) self.view = PairingTableView(parent=self.obj) self.presenter = PairingTablePresenter(self.view, self.model) self.view.warning_popup = mock.Mock() def tearDown(self): self.obj = None def assert_model_empty(self): self.assertEqual(len(self.model.pair_names), 0) self.assertEqual(len(self.model.pairs), 0) def assert_view_empty(self): self.assertEqual(self.view.num_rows(), 0) def add_three_groups_to_model(self): group1 = MuonGroup(group_name="my_group_0", detector_ids=[1]) group2 = MuonGroup(group_name="my_group_1", detector_ids=[2]) group3 = MuonGroup(group_name="my_group_2", detector_ids=[3]) self.data.add_group(group1) self.data.add_group(group2) self.data.add_group(group3) def add_two_pairs_to_table(self): pair1 = MuonPair(pair_name="my_pair_0", forward_group_name="my_group_0", backward_group_name="my_group_1", alpha=1.0) pair2 = MuonPair(pair_name="my_pair_1", forward_group_name="my_group_1", backward_group_name="my_group_2", alpha=1.0) self.presenter.add_pair(pair1) self.presenter.add_pair(pair2) # ------------------------------------------------------------------------------------------------------------------ # TESTS : Initialization # ------------------------------------------------------------------------------------------------------------------ def test_that_table_has_five_columns_when_initialized(self): # these are : pair name, group 1, group 2, alpha, guess alpha self.assertEqual(self.view.num_cols(), 5) def test_that_model_is_initialized_as_empty(self): self.assert_model_empty() def test_that_view_is_initialized_as_empty(self): self.assert_view_empty() # ------------------------------------------------------------------------------------------------------------------ # TESTS : Adding and removing groups # ------------------------------------------------------------------------------------------------------------------ def test_that_add_pair_button_adds_pair(self): self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.view.num_rows(), 1) self.assertEqual(len(self.model.pairs), 1) def test_that_remove_pair_button_removes_group(self): self.add_two_pairs_to_table() self.presenter.handle_remove_pair_button_clicked() self.assertEqual(self.view.num_rows(), 1) def test_that_add_pair_button_adds_pair_to_end_of_table(self): self.add_two_pairs_to_table() self.presenter.add_pair(MuonPair(pair_name="new")) self.assertEqual( self.view.get_table_item_text(self.view.num_rows() - 1, 0), "new") def test_that_remove_pair_button_removes_pair_from_end_of_table(self): self.add_two_pairs_to_table() self.presenter.handle_remove_pair_button_clicked() self.assertEqual( self.view.get_table_item_text(self.view.num_rows() - 1, 0), "my_pair_0") def test_that_highlighting_rows_and_clicking_remove_pair_removes_the_selected_rows( self): self.add_two_pairs_to_table() self.view._get_selected_row_indices = mock.Mock(return_value=[0, 1]) self.presenter.handle_remove_pair_button_clicked() self.assert_model_empty() self.assert_view_empty() def test_that_cannot_add_more_than_20_rows(self): for i in range(21): self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.view.num_rows(), 20) self.assertEqual(len(self.model.pairs), 20) def test_that_trying_to_add_a_20th_row_gives_warning_message(self): for i in range(21): self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.view.warning_popup.call_count, 1) def test_that_remove_group_when_table_is_empty_does_not_throw(self): for i in range(3): self.presenter.handle_remove_pair_button_clicked() self.view.warning_popup.assert_not_called() # ------------------------------------------------------------------------------------------------------------------ # TESTS : Context menu has "add pair" and "remove pair" functionality # ------------------------------------------------------------------------------------------------------------------ def test_context_menu_add_pairing_with_no_rows_selected_adds_pair_to_end_of_table( self): self.view.contextMenuEvent(0) self.view.add_pair_action.triggered.emit(True) self.assertEqual(len(self.model.pairs), 1) self.assertEqual(self.view.num_rows(), 1) self.assertEqual(self.view.get_table_item_text(0, 0), "pair_1") def test_context_menu_add_pairing_with_rows_selected_does_not_add_pair( self): self.add_two_pairs_to_table() self.view._get_selected_row_indices = mock.Mock(return_value=[0]) self.view.contextMenuEvent(0) self.assertFalse(self.view.add_pair_action.isEnabled()) def test_context_menu_remove_pairing_with_no_rows_selected_removes_last_row( self): for i in range(3): # names : pair_1, pair_2, pair_3 self.presenter.handle_add_pair_button_clicked() self.view.contextMenuEvent(0) self.view.remove_pair_action.triggered.emit(True) self.assertEqual(len(self.model.pairs), 2) self.assertEqual(self.view.num_rows(), 2) self.assertEqual(self.view.get_table_item_text(0, 0), "pair_1") self.assertEqual(self.view.get_table_item_text(1, 0), "pair_2") def test_context_menu_remove_pairing_removes_selected_rows(self): for i in range(3): # names : pair_0, pair_1, pair_2 self.presenter.handle_add_pair_button_clicked() self.view._get_selected_row_indices = mock.Mock(return_value=[0, 2]) self.view.contextMenuEvent(0) self.view.remove_pair_action.triggered.emit(True) self.assertEqual(len(self.model.pairs), 1) self.assertEqual(self.view.num_rows(), 1) self.assertEqual(self.view.get_table_item_text(0, 0), "pair_2") def test_context_menu_remove_pairing_disabled_if_no_pairs_in_table(self): self.view.contextMenuEvent(0) self.assertFalse(self.view.remove_pair_action.isEnabled()) # ------------------------------------------------------------------------------------------------------------------ # TESTS : Pair name validation # ------------------------------------------------------------------------------------------------------------------ def test_that_can_change_pair_name_to_valid_name_and_update_view_and_model( self): self.add_two_pairs_to_table() self.view.pairing_table.setCurrentCell(0, 0) self.view.pairing_table.item(0, 0).setText("new_name") self.assertEqual(self.view.get_table_item_text(0, 0), "new_name") self.assertIn("new_name", self.model.pair_names) def test_that_if_invalid_name_given_warning_message_is_shown(self): self.add_two_pairs_to_table() invalid_names = ["", "@", "name!", "+-"] call_count = self.view.warning_popup.call_count for invalid_name in invalid_names: call_count += 1 self.view.pairing_table.setCurrentCell(0, 0) self.view.pairing_table.item(0, 0).setText(invalid_name) self.assertEqual(self.view.warning_popup.call_count, call_count) def test_that_if_invalid_name_given_name_reverts_to_its_previous_value( self): self.add_two_pairs_to_table() invalid_names = ["", "@", "name!", "+-"] for invalid_name in invalid_names: print(self.view.get_table_contents()) self.view.pairing_table.setCurrentCell(0, 0) self.view.pairing_table.item(0, 0).setText(invalid_name) print(self.view.get_table_contents()) self.assertEqual(str(self.view.get_table_item_text(0, 0)), "my_pair_0") self.assertIn("my_pair_0", self.model.pair_names) def test_that_pair_names_with_numbers_and_letters_and_underscores_are_valid( self): self.add_two_pairs_to_table() valid_names = ["fwd", "fwd_1", "1234", "FWD0001", "_fwd"] for valid_name in valid_names: self.view.pairing_table.setCurrentCell(0, 0) self.view.pairing_table.item(0, 0).setText(valid_name) self.assertEqual(str(self.view.get_table_item_text(0, 0)), valid_name) self.assertIn(valid_name, self.model.pair_names) def test_that_renaming_group_to_duplicate_fails_and_reverts_to_previous_value( self): self.add_two_pairs_to_table() self.view.pairing_table.setCurrentCell(0, 0) self.view.pairing_table.item(0, 0).setText("my_pair_1") self.assertEqual(str(self.view.get_table_item_text(0, 0)), "my_pair_0") self.assertIn("my_pair_0", self.model.pair_names) def test_that_warning_shown_if_duplicated_pair_name_used(self): self.add_two_pairs_to_table() self.view.pairing_table.setCurrentCell(0, 0) self.view.pairing_table.item(0, 0).setText("my_group_1") self.assertEqual(self.view.warning_popup.call_count, 1) def test_that_default_pair_name_is_pair_0(self): self.presenter.handle_add_pair_button_clicked() self.assertEqual(str(self.view.get_table_item_text(0, 0)), "pair_1") self.assertIn("pair_1", self.model.pair_names) def test_that_adding_new_pair_creates_incremented_default_name(self): self.presenter.handle_add_pair_button_clicked() self.presenter.handle_add_pair_button_clicked() self.presenter.handle_add_pair_button_clicked() self.assertEqual(str(self.view.get_table_item_text(0, 0)), "pair_1") self.assertEqual(str(self.view.get_table_item_text(1, 0)), "pair_2") self.assertEqual(str(self.view.get_table_item_text(2, 0)), "pair_3") six.assertCountEqual(self, self.model.pair_names, ["pair_1", "pair_2", "pair_3"])
class AlphaTest(unittest.TestCase): def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonDataContext() self.model = PairingTableModel(data=self.data) self.view = PairingTableView(parent=self.obj) self.presenter = PairingTablePresenter(self.view, self.model) self.view.warning_popup = mock.Mock() def tearDown(self): self.obj = None def assert_model_empty(self): self.assertEqual(len(self.model.pair_names), 0) self.assertEqual(len(self.model.pairs), 0) def assert_view_empty(self): self.assertEqual(self.view.num_rows(), 0) def add_three_groups_to_model(self): group1 = MuonGroup(group_name="my_group_0", detector_ids=[1]) group2 = MuonGroup(group_name="my_group_1", detector_ids=[2]) group3 = MuonGroup(group_name="my_group_2", detector_ids=[3]) self.data.add_group(group1) self.data.add_group(group2) self.data.add_group(group3) def add_two_pairs_to_table(self): pair1 = MuonPair(pair_name="my_pair_0", forward_group_name="my_group_0", backward_group_name="my_group_1", alpha=1.0) pair2 = MuonPair(pair_name="my_pair_1", forward_group_name="my_group_1", backward_group_name="my_group_2", alpha=1.0) self.presenter.add_pair(pair1) self.presenter.add_pair(pair2) def get_group_1_selector(self, row): return self.view.pairing_table.cellWidget(row, 1) def get_group_2_selector(self, row): return self.view.pairing_table.cellWidget(row, 2) # ------------------------------------------------------------------------------------------------------------------ # TESTS : test the functionality around alpha. # ------------------------------------------------------------------------------------------------------------------ def test_that_alpha_defaults_to_1(self): self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.view.get_table_item_text(0, 3), "1.0") def test_that_table_reverts_to_previous_value_when_adding_values_which_arent_numbers_to_alpha_column( self): self.presenter.handle_add_pair_button_clicked() non_numeric_alphas = ["", "a", "long", "!", "_", "1+2"] default_value = self.view.get_table_item_text(0, 3) for invalid_alpha in non_numeric_alphas: self.view.pairing_table.setCurrentCell(0, 3) self.view.pairing_table.item(0, 3).setText(invalid_alpha) self.assertEqual(self.view.get_table_item_text(0, 3), default_value) def test_that_warning_displayed_when_adding_invalid_alpha_values(self): self.presenter.handle_add_pair_button_clicked() non_numeric_alphas = ["", "a", "long", "!", "_", "1+2"] call_count = 0 for invalid_alpha in non_numeric_alphas: call_count += 1 self.view.pairing_table.setCurrentCell(0, 3) self.view.pairing_table.item(0, 3).setText(invalid_alpha) self.assertEqual(self.view.warning_popup.call_count, call_count) def test_that_alpha_values_stored_to_three_decimal_places(self): self.presenter.handle_add_pair_button_clicked() self.view.pairing_table.setCurrentCell(0, 3) # test that rounds correctly self.view.pairing_table.item(0, 3).setText("1.1239") self.assertEqual(self.view.get_table_item_text(0, 3), "1.124") def test_that_alpha_values_stored_to_three_decimal_places_when_rounding_down( self): self.presenter.handle_add_pair_button_clicked() self.view.pairing_table.setCurrentCell(0, 3) # test that rounds correctly self.view.pairing_table.item(0, 3).setText("1.1244") self.assertEqual(self.view.get_table_item_text(0, 3), "1.124") def test_that_valid_alpha_values_are_added_correctly(self): self.presenter.handle_add_pair_button_clicked() valid_inputs = ["1.0", "12", ".123", "0.00001", "0.0005"] expected_output = ["1.0", "12.0", "0.123", "1e-05", "0.001"] for valid_alpha, expected_alpha in iter( zip(valid_inputs, expected_output)): self.view.pairing_table.setCurrentCell(0, 3) self.view.pairing_table.item(0, 3).setText(valid_alpha) self.assertEqual(self.view.get_table_item_text(0, 3), expected_alpha) def test_that_negative_alpha_is_not_allowed(self): self.presenter.handle_add_pair_button_clicked() self.view.pairing_table.setCurrentCell(0, 3) default_value = self.view.get_table_item_text(0, 3) self.view.pairing_table.item(0, 3).setText("-1.0") self.assertEqual(self.view.get_table_item_text(0, 3), default_value) self.assertEqual(self.view.warning_popup.call_count, 1) def test_that_clicking_guess_alpha_triggers_correct_slot_with_correct_row_supplied( self): # Guess alpha functionality must be implemented by parent widgets. So we just check that the # design for implementing this works (via an Observable in the presenter) self.presenter.handle_add_pair_button_clicked() self.presenter.handle_add_pair_button_clicked() self.presenter.guessAlphaNotifier.notify_subscribers = mock.Mock() self.view.pairing_table.cellWidget(1, 4).clicked.emit(True) self.assertEqual( self.presenter.guessAlphaNotifier.notify_subscribers.call_count, 1) self.assertEqual( self.presenter.guessAlphaNotifier.notify_subscribers. call_args_list[0][0][0], ["pair_2", "", ""])
class MuonDataContextTest(unittest.TestCase): def setUp(self): self.loaded_data = MuonLoadData() self.context = MuonDataContext(self.loaded_data) self.context.instrument = 'EMU' filepath = FileFinder.findRuns('EMU00019489.nxs')[0] load_result, run, filename = load_workspace_from_filename(filepath) self.loaded_data.add_data(workspace=load_result, run=[run], filename=filename, instrument='EMU') self.context.current_runs = [[run]] self.context.update_current_data() def tearDown(self): AnalysisDataService.clear() def test_that_setting_a_fixed_rebin_step_calculates_all_groups_and_pairs_twice( self): self.context.gui_variables['RebinType'] = 'Fixed' self.context.gui_variables['RebinFixed'] = '2' self.context.show_all_groups() expected_workspaces = [ 'EMU19489', 'EMU19489 Groups', 'EMU19489; Group; bwd; Asymmetry; #1', 'EMU19489; Group; bwd; Asymmetry; Rebin; #1', 'EMU19489; Group; bwd; Counts; #1', 'EMU19489; Group; bwd; Counts; Rebin; #1', 'EMU19489; Group; fwd; Asymmetry; #1', 'EMU19489; Group; fwd; Asymmetry; Rebin; #1', 'EMU19489; Group; fwd; Counts; #1', 'EMU19489; Group; fwd; Counts; Rebin; #1', 'Muon Data' ] self.assertEqual(AnalysisDataService.getObjectNames(), expected_workspaces) def test_that_setting_no_rebinning_calculates_groups_and_pairs_once(self): self.context.gui_variables['RebinType'] = 'None' self.context.show_all_groups() expected_workspaces = [ 'EMU19489', 'EMU19489 Groups', 'EMU19489; Group; bwd; Asymmetry; #1', 'EMU19489; Group; bwd; Counts; #1', 'EMU19489; Group; fwd; Asymmetry; #1', 'EMU19489; Group; fwd; Counts; #1', 'Muon Data' ] self.assertEqual(AnalysisDataService.getObjectNames(), expected_workspaces) def test_that_setting_a_variable_rebin_step_calculates_all_groups_and_pairs_twice( self): self.context.gui_variables['RebinType'] = 'Variable' self.context.gui_variables['RebinVariable'] = '1,0.1,2' self.context.show_all_groups() expected_workspaces = [ 'EMU19489', 'EMU19489 Groups', 'EMU19489; Group; bwd; Asymmetry; #1', 'EMU19489; Group; bwd; Asymmetry; Rebin; #1', 'EMU19489; Group; bwd; Counts; #1', 'EMU19489; Group; bwd; Counts; Rebin; #1', 'EMU19489; Group; fwd; Asymmetry; #1', 'EMU19489; Group; fwd; Asymmetry; Rebin; #1', 'EMU19489; Group; fwd; Counts; #1', 'EMU19489; Group; fwd; Counts; Rebin; #1', 'Muon Data' ] self.assertEqual(AnalysisDataService.getObjectNames(), expected_workspaces)
class GroupSelectorTest(unittest.TestCase): def setUp(self): self._qapp = mock_widget.mockQapp() # Store an empty widget to parent all the views, and ensure they are deleted correctly self.obj = QtGui.QWidget() self.data = MuonDataContext() self.model = PairingTableModel(data=self.data) self.view = PairingTableView(parent=self.obj) self.presenter = PairingTablePresenter(self.view, self.model) self.view.warning_popup = mock.Mock() def tearDown(self): self.obj = None def assert_model_empty(self): self.assertEqual(len(self.model.pair_names), 0) self.assertEqual(len(self.model.pairs), 0) def assert_view_empty(self): self.assertEqual(self.view.num_rows(), 0) def add_three_groups_to_model(self): group1 = MuonGroup(group_name="my_group_0", detector_ids=[1]) group2 = MuonGroup(group_name="my_group_1", detector_ids=[2]) group3 = MuonGroup(group_name="my_group_2", detector_ids=[3]) self.data.add_group(group1) self.data.add_group(group2) self.data.add_group(group3) def add_two_pairs_to_table(self): pair1 = MuonPair(pair_name="my_pair_0", forward_group_name="my_group_0", backward_group_name="my_group_1", alpha=1.0) pair2 = MuonPair(pair_name="my_pair_1", forward_group_name="my_group_1", backward_group_name="my_group_2", alpha=1.0) self.presenter.add_pair(pair1) self.presenter.add_pair(pair2) def get_group_1_selector_from_pair(self, row): return self.view.pairing_table.cellWidget(row, 1) def get_group_2_selector_from_pair(self, row): return self.view.pairing_table.cellWidget(row, 2) # ------------------------------------------------------------------------------------------------------------------ # TESTS : test the functionality around the combo boxes which allow the user to select the two groups that # together make the muon pair. # ------------------------------------------------------------------------------------------------------------------ def test_that_adding_pair_when_no_groups_exist_leaves_combo_boxes_empty( self): self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 0) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 0) self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), "") self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), "") def test_that_adding_pair_then_adding_group_puts_group_in_combos(self): self.presenter.handle_add_pair_button_clicked() self.data.add_group( MuonGroup(group_name="my_group_0", detector_ids=[1])) self.presenter.update_view_from_model() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 1) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 1) def test_that_adding_pair_then_adding_group_sets_combo_to_added_group( self): self.presenter.handle_add_pair_button_clicked() self.data.add_group( MuonGroup(group_name="my_group_0", detector_ids=[1])) self.presenter.update_view_from_model() self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), "my_group_0") self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), "my_group_0") def test_that_adding_two_groups_and_then_pair_sets_combo_to_added_groups( self): self.data.add_group( MuonGroup(group_name="my_group_0", detector_ids=[1])) self.data.add_group( MuonGroup(group_name="my_group_1", detector_ids=[2])) self.presenter.handle_add_pair_button_clicked() self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), "my_group_0") self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), "my_group_1") def test_that_added_groups_appear_in_group_combo_boxes_in_new_pairs(self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 3) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_0"), -1) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_1"), -1) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_2"), -1) def test_that_get_index_of_text_returns_correct_index_if_text_exists(self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() index = self.view.get_index_of_text( self.get_group_1_selector_from_pair(0), 'my_group_1') self.assertEqual(index, 1) def test_that_get_index_of_text_returns_0_if_text_does_not_exists(self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() index = self.view.get_index_of_text( self.get_group_1_selector_from_pair(0), 'random string') self.assertEqual(index, 0) def test_that_added_groups_appear_in_group_combo_boxes_in_existing_pairs_if_update_called( self): self.presenter.handle_add_pair_button_clicked() self.add_three_groups_to_model() # the following method must be called self.presenter.update_view_from_model() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 3) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_0"), -1) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_1"), -1) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_2"), -1) def test_that_changing_group_selection_triggers_cell_changed_method_in_view( self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() self.presenter.handle_add_pair_button_clicked() self.view.on_cell_changed = mock.Mock() self.get_group_1_selector_from_pair(0).setCurrentIndex(1) self.assertEqual(self.view.on_cell_changed.call_count, 1) self.assertEqual(self.view.on_cell_changed.call_args_list[0][0], (0, 1)) def test_that_removing_groups_and_then_calling_update_removes_groups_from_selections( self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() del self.data.groups["my_group_1"] del self.data.groups["my_group_2"] self.presenter.update_view_from_model() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 1) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 1) self.assertNotEqual( self.get_group_1_selector_from_pair(0).findText("my_group_0"), -1) self.assertNotEqual( self.get_group_2_selector_from_pair(0).findText("my_group_0"), -1) def test_adding_new_group_does_not_change_pair_slection(self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 3) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 3) self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), 'my_group_0') self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), 'my_group_1') group4 = MuonGroup(group_name="my_group_4", detector_ids=[4]) self.data.add_group(group4) self.presenter.update_view_from_model() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 4) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 4) self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), 'my_group_0') self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), 'my_group_1') def test_removing_group_used_in_pair_handled_gracefully(self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() del self.data.groups["my_group_0"] self.presenter.update_view_from_model() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 2) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 2) self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), 'my_group_1') self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), 'my_group_1') def test_group_changed_to_other_group_switches_groups(self): self.add_three_groups_to_model() self.presenter.handle_add_pair_button_clicked() self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 3) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 3) self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), 'my_group_0') self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), 'my_group_1') group_selector = self.get_group_1_selector_from_pair(0) group_selector.setCurrentIndex(1) self.assertEqual(self.get_group_1_selector_from_pair(0).count(), 3) self.assertEqual(self.get_group_2_selector_from_pair(0).count(), 3) self.assertEqual( self.get_group_1_selector_from_pair(0).currentText(), 'my_group_1') self.assertEqual( self.get_group_2_selector_from_pair(0).currentText(), 'my_group_0')