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()
Exemple #2
0
    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()
Exemple #3
0
    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.add_three_groups_to_model()

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())
    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.loaded_data = MuonLoadData()
        self.data_context = MuonDataContext(self.loaded_data)
        self.gui_context = MuonGuiContext()
        self.group_context = MuonGroupPairContext(self.data_context.check_group_contains_valid_detectors)
        self.context = MuonContext(muon_data_context=self.data_context, muon_group_context=self.group_context,
                                   muon_gui_context=self.gui_context)

        self.model = GroupingTabModel(context=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.presenter.create_update_thread = mock.MagicMock(return_value=mock.MagicMock())
        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.obj = QWidget()

        self.loaded_data = MuonLoadData()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)

        self.grouping_table_view = GroupingTableView()
        self.grouping_table_widget = GroupingTablePresenter(
            self.grouping_table_view, self.model)

        self.pairing_table_view = PairingTableView()
        self.pairing_table_widget = PairingTablePresenter(
            self.pairing_table_view, self.model)

        self.diff_widget = DifferencePresenter(self.model)
        self.diff_widget.group_view.enter_diff_name = mock.Mock(
            side_effect=diff_name())
        self.diff_widget.pair_view.enter_diff_name = mock.Mock(
            side_effect=diff_name())

        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

        self.add_three_groups()
        self.add_two_pairs()

        self.view = GroupingTabView(self.grouping_table_view,
                                    self.pairing_table_view,
                                    self.diff_widget.view)
        self.presenter = GroupingTabPresenter(self.view, self.model,
                                              self.grouping_table_widget,
                                              self.pairing_table_widget,
                                              self.diff_widget)

        self.presenter.create_update_thread = mock.MagicMock(
            return_value=mock.MagicMock())
        self.presenter.pairing_table_widget.handle_add_pair_button_clicked = mock.MagicMock(
        )
        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.loaded_data = MuonLoadData()
        self.data_context = MuonDataContext(self.loaded_data)
        self.gui_context = MuonGuiContext()
        self.group_context = MuonGroupPairContext(
            self.data_context.check_group_contains_valid_detectors)
        self.context = MuonContext(muon_data_context=self.data_context,
                                   muon_group_context=self.group_context,
                                   muon_gui_context=self.gui_context)

        self.model = GroupingTabModel(context=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.presenter.create_update_thread = mock.MagicMock(
            return_value=mock.MagicMock())
        self.view.display_warning_box = mock.MagicMock()
        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()
Exemple #7
0
    def setUp(self):
        self.loaded_data = MuonLoadData()

        self.context = setup_context(False)

        self.data_context = self.context.data_context
        self.gui_context = self.context.gui_context
        self.group_context = self.context.group_pair_context

        self.model = GroupingTabModel(context=self.context)

        self.grouping_table_view = GroupingTableView()
        self.grouping_table_widget = GroupingTablePresenter(
            self.grouping_table_view, self.model)

        self.pairing_table_view = PairingTableView()
        self.pairing_table_widget = PairingTablePresenter(
            self.pairing_table_view, self.model)

        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

        self.add_three_groups()
        self.add_two_pairs()

        self.view = GroupingTabView(self.grouping_table_view,
                                    self.pairing_table_view)
        self.presenter = GroupingTabPresenter(self.view, self.model,
                                              self.grouping_table_widget,
                                              self.pairing_table_widget)

        self.presenter.create_update_thread = mock.MagicMock(
            return_value=mock.MagicMock())
        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()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.add_three_groups_to_model()

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())
    def __init__(self, context):

        self.group_tab_model = GroupingTabModel(context)

        self.grouping_table_view = GroupingTableView()
        self.grouping_table_widget = GroupingTablePresenter(
            self.grouping_table_view, self.group_tab_model)

        self.pairing_table_view = PairingTableView()
        self.pairing_table_widget = PairingTablePresenter(
            self.pairing_table_view, self.group_tab_model)

        self.group_tab_view = GroupingTabView(self.grouping_table_view,
                                              self.pairing_table_view)
        self.group_tab_presenter = GroupingTabPresenter(
            self.group_tab_view, self.group_tab_model,
            self.grouping_table_widget, self.pairing_table_widget)
Exemple #10
0
    def __init__(self, context):

        self.group_tab_model = GroupingTabModel(context)

        self.grouping_table_view = GroupingTableView()
        self.grouping_table_widget = GroupingTablePresenter(
            self.grouping_table_view, self.group_tab_model)

        self.pairing_table_view = PairingTableView()
        self.pairing_table_widget = PairingTablePresenter(
            self.pairing_table_view, self.group_tab_model)

        self.group_tab_view = GroupingTabView(self.grouping_table_view,
                                              self.pairing_table_view)
        self.group_tab_presenter = GroupingTabPresenter(
            self.group_tab_view, self.group_tab_model,
            self.grouping_table_widget, self.pairing_table_widget)

        context.update_view_from_model_notifier.add_subscriber(
            self.group_tab_presenter.update_view_from_model_observer)
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()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.add_three_groups_to_model()

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())

    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.group_context.add_group(group1)
        self.group_context.add_group(group2)
        self.group_context.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", "my_group_0", "my_group_1"])
class GroupingTabPresenterTest(unittest.TestCase):
    def setUp(self):
        self.obj = QWidget()

        self.loaded_data = MuonLoadData()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)

        self.grouping_table_view = GroupingTableView()
        self.grouping_table_widget = GroupingTablePresenter(
            self.grouping_table_view, self.model)

        self.pairing_table_view = PairingTableView()
        self.pairing_table_widget = PairingTablePresenter(
            self.pairing_table_view, self.model)

        self.diff_widget = DifferencePresenter(self.model)
        self.diff_widget.group_view.enter_diff_name = mock.Mock(
            side_effect=diff_name())
        self.diff_widget.pair_view.enter_diff_name = mock.Mock(
            side_effect=diff_name())

        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

        self.add_three_groups()
        self.add_two_pairs()

        self.view = GroupingTabView(self.grouping_table_view,
                                    self.pairing_table_view,
                                    self.diff_widget.view)
        self.presenter = GroupingTabPresenter(self.view, self.model,
                                              self.grouping_table_widget,
                                              self.pairing_table_widget,
                                              self.diff_widget)

        self.presenter.create_update_thread = mock.MagicMock(
            return_value=mock.MagicMock())
        self.presenter.pairing_table_widget.handle_add_pair_button_clicked = mock.MagicMock(
        )
        self.view.display_warning_box = mock.MagicMock()
        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

    def add_three_groups(self):
        testgroup1 = MuonGroup(group_name="fwd", detector_ids=[1, 2, 3, 4, 5])
        testgroup2 = MuonGroup(group_name="bwd", detector_ids=[6, 7, 8, 9, 10])
        testgroup3 = MuonGroup(group_name="top",
                               detector_ids=[11, 12, 13, 14, 15])
        self.grouping_table_widget.add_group(testgroup1)
        self.grouping_table_widget.add_group(testgroup2)
        self.grouping_table_widget.add_group(testgroup3)

    def add_two_pairs(self):
        testpair1 = MuonPair(pair_name="long1",
                             forward_group_name="fwd",
                             backward_group_name="bwd")
        testpair2 = MuonPair(pair_name="long2",
                             forward_group_name="fwd",
                             backward_group_name="top")
        self.pairing_table_widget.add_pair(testpair1)
        self.pairing_table_widget.add_pair(testpair2)

    def add_group_diff(self):
        self.diff_widget.group_widget.handle_add_diff_button_clicked(
            'fwd', 'top')

    def add_pair_diff(self):
        self.diff_widget.pair_widget.handle_add_diff_button_clicked(
            'long1', 'long2')

    def tearDown(self):
        self.obj = None

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS
    # ------------------------------------------------------------------------------------------------------------------
    def test_context_menu_add_pair_adds_pair_if_two_groups_selected(self):
        self.assertEqual(self.pairing_table_view.num_rows(), 2)
        self.grouping_table_view._get_selected_row_indices = mock.Mock(
            return_value=[0, 1])
        self.grouping_table_view.contextMenuEvent(0)
        self.grouping_table_view.add_pair_action.triggered.emit(True)

        self.presenter.pairing_table_widget.handle_add_pair_button_clicked.assert_called_once_with(
            'fwd', 'bwd')

    def test_that_clear_button_clears_model_and_view(self):
        self.view.clear_grouping_button.clicked.emit(True)

        self.assertEqual(len(self.model.groups), 0)
        self.assertEqual(len(self.model.pairs), 0)
        self.assertEqual(self.grouping_table_view.num_rows(), 0)
        self.assertEqual(self.pairing_table_view.num_rows(), 0)

    @mock.patch(
        "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML"
    )
    def test_that_load_grouping_triggers_the_correct_function(self, mock_load):
        self.view.show_file_browser_and_return_selection = mock.MagicMock(
            return_value="grouping.xml")
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]
        mock_load.return_value = (groups, pairs, [], 'description', 'pair1')

        self.view.load_grouping_button.clicked.emit(True)

        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(mock_load.call_args[0][0], "grouping.xml")

    def test_that_load_grouping_inserts_loaded_groups_and_pairs_correctly(
            self):
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]

        self._run_handle_load_grouping_with_mocked_load(groups,
                                                        pairs,
                                                        default='pair1')

        self.assertCountEqual(self.model.group_names, ["grp1", "grp2"])
        self.assertCountEqual(self.model.pair_names, ["pair1"])
        self.assertEqual(self.grouping_table_view.num_rows(), 2)
        self.assertEqual(self.pairing_table_view.num_rows(), 1)
        self.assertEqual(
            self.pairing_table_view.pairing_table.cellWidget(0,
                                                             2).currentText(),
            "grp1")
        self.assertEqual(
            self.pairing_table_view.pairing_table.cellWidget(0,
                                                             3).currentText(),
            "grp2")

    def test_loading_does_not_insert_invalid_groups(self):
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 1000])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]

        self._run_handle_load_grouping_with_mocked_load(groups,
                                                        pairs,
                                                        default='pair1')

        self.view.display_warning_box.assert_called_once_with(
            'Invalid detectors in group grp2')
        self.assertCountEqual(self.model.group_names, ["grp1"])
        self.assertCountEqual(self.model.pair_names, [])
        self.assertEqual(self.grouping_table_view.num_rows(), 1)
        self.assertEqual(self.pairing_table_view.num_rows(), 0)

    def test_loading_selects_all_pairs_if_any_pairs_exist_and_no_default_set(
            self):
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]

        self._run_handle_load_grouping_with_mocked_load(groups,
                                                        pairs,
                                                        default='')

        self.assertEqual(self.model.selected_pairs, ["pair1"])
        self.assertEqual(self.model.selected_groups, [])

    def test_loading_selects_groups_if_no_pairs_exist_and_no_default_set(self):
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = []

        self._run_handle_load_grouping_with_mocked_load(groups,
                                                        pairs,
                                                        default='')

        self.assertEqual(self.model.selected_pairs, [])
        self.assertEqual(self.model.selected_groups, ["grp1", "grp2"])

    def test_loading_selects_default_pairs_and_groups_correctly(self):
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]

        self._run_handle_load_grouping_with_mocked_load(groups,
                                                        pairs,
                                                        default='grp2')

        self.assertEqual(self.model.selected_pairs, [])
        self.assertEqual(self.model.selected_groups, ["grp2"])

    def test_loading_selects_correctly_when_default_is_invalid(self):
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]

        self._run_handle_load_grouping_with_mocked_load(groups,
                                                        pairs,
                                                        default='grp3')

        self.assertEqual(self.model.selected_pairs, ["pair1"])
        self.assertEqual(self.model.selected_groups, [])

    def test_that_save_grouping_triggers_the_correct_function(self):
        # Save functionality is tested elsewhere
        self.view.show_file_save_browser_and_return_selection = mock.Mock(
            return_value="grouping.xml")

        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.save_grouping_to_XML"
        ) as mock_save:
            self.view.save_grouping_button.clicked.emit(True)

            self.assertEqual(mock_save.call_count, 1)
            self.assertEqual(mock_save.call_args[0][-1], "grouping.xml")

    def test_update_all_calculates_groups_and_pairs(self):
        self.presenter.handle_update_all_clicked()

        self.presenter.update_thread.threadWrapperSetUp.assert_called_once_with(
            self.presenter.disable_editing,
            self.presenter.handle_update_finished,
            self.presenter.error_callback)
        self.presenter.update_thread.start.assert_called_once_with()

    def test_that_adding_pair_with_context_menu_allows_for_name_specification(
            self):
        self.presenter.add_pair_from_grouping_table("first", "second")
        self.pairing_table_widget.handle_add_pair_button_clicked.assert_called_once_with(
            "first", "second")

    def _run_handle_load_grouping_with_mocked_load(self,
                                                   groups,
                                                   pairs,
                                                   description='description',
                                                   default=''):
        self.view.show_file_browser_and_return_selection = mock.Mock(
            return_value="grouping.xml")
        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML"
        ) as mock_load:
            # mock the loading to return set groups/pairs
            mock_load.return_value = (groups, pairs, [], 'description',
                                      default)
            self.presenter.handle_load_grouping_from_file()

    def test_default_clicked(self):
        self.presenter._model.reset_groups_and_pairs_to_default = mock.MagicMock(
            return_value="success")
        self.presenter.handle_default_grouping_button_clicked()
        self.assertEqual(self.view.display_warning_box.call_count, 0)

    def test_default_clicked_no_data(self):
        self.presenter._model.reset_groups_and_pairs_to_default = mock.MagicMock(
            return_value="failed")
        self.presenter.handle_default_grouping_button_clicked()
        self.assertEqual(self.view.display_warning_box.call_count, 1)

    def test_update_description_to_empty_on_clear_all(self):
        self.presenter.on_clear_requested()
        self.assertEqual('', self.view.get_description_text())

    def test_cannot_remove_last_row_group_table_if_used_by_pair(self):
        self.grouping_table_widget.handle_remove_group_button_clicked()

        self.assertEqual(1, self.grouping_table_view.warning_popup.call_count)
        self.assertEqual(
            'top is used by: long2',
            self.grouping_table_view.warning_popup.call_args_list[0][0][0])

    def test_cannot_remove_last_row_group_table_if_used_by_diff(self):
        self.add_group_diff()
        self.grouping_table_widget.handle_remove_group_button_clicked()

        self.assertEqual(1, self.grouping_table_view.warning_popup.call_count)
        self.assertEqual(
            'top is used by: long2, diff_1',
            self.grouping_table_view.warning_popup.call_args_list[0][0][0])

    def test_cannot_remove_a_selected_group_used_by_pair(self):
        self.grouping_table_view.get_selected_group_names_and_indexes = mock.Mock(
            return_value=[['fwd', 0]])
        self.grouping_table_widget.handle_remove_group_button_clicked()

        self.assertEqual(1, self.grouping_table_view.warning_popup.call_count)
        self.assertEqual(
            'fwd is used by: long1, long2\n',
            self.grouping_table_view.warning_popup.call_args_list[0][0][0])

    def test_cannot_remove_a_selected_group_used_by_diff(self):
        self.add_group_diff()
        self.grouping_table_view.get_selected_group_names_and_indexes = mock.Mock(
            return_value=[['fwd', 0]])
        self.grouping_table_widget.handle_remove_group_button_clicked()

        self.assertEqual(1, self.grouping_table_view.warning_popup.call_count)
        self.assertEqual(
            'fwd is used by: long1, long2, diff_1\n',
            self.grouping_table_view.warning_popup.call_args_list[0][0][0])

    def test_cannot_remove_last_row_pair_table_if_used_by_diff(self):
        self.add_pair_diff()
        self.pairing_table_widget.handle_remove_pair_button_clicked()

        self.assertEqual(1, self.pairing_table_view.warning_popup.call_count)
        self.assertEqual(
            'long2 is used by: diff_1',
            self.pairing_table_view.warning_popup.call_args_list[0][0][0])

    def test_cannot_remove_a_selected_pair_used_by_diff(self):
        self.add_pair_diff()
        self.pairing_table_view.get_selected_pair_names_and_indexes = mock.Mock(
            return_value=[['long1', 0]])
        self.pairing_table_widget.handle_remove_pair_button_clicked()

        self.assertEqual(1, self.pairing_table_view.warning_popup.call_count)
        self.assertEqual(
            'long1 is used by: diff_1\n',
            self.pairing_table_view.warning_popup.call_args_list[0][0][0])

    def test_periods_button_no_data(self):
        self.presenter._model.is_data_loaded = mock.Mock(return_value=False)
        self.presenter.period_info_widget.addInfo = mock.MagicMock()
        self.presenter.period_info_widget.show = mock.MagicMock()
        self.presenter.handle_period_information_button_clicked()

        self.assertEqual(0,
                         self.presenter.period_info_widget.addInfo.call_count)
        self.assertEqual(1, self.presenter.period_info_widget.show.call_count)

    def test_periods_button_data_added_successfully(self):
        self.presenter._model.is_data_loaded = mock.Mock(return_value=True)
        self.presenter.period_info_widget.addInfo = mock.MagicMock()
        self.presenter.period_info_widget.show = mock.MagicMock()

        self.presenter.handle_period_information_button_clicked()

        self.assertEqual(1,
                         self.presenter.period_info_widget.addInfo.call_count)
        self.assertEqual(1, self.presenter.period_info_widget.show.call_count)

    def test_periods_button_data_missing_added_successfully(self):
        self.presenter._model.is_data_loaded = mock.Mock(return_value=True)
        self.model._data.get_sample_log = mock.Mock(return_value=None)
        self.presenter.period_info_widget.addInfo = mock.MagicMock()
        self.presenter.period_info_widget.show = mock.MagicMock()

        self.presenter.handle_period_information_button_clicked()

        self.assertEqual(1,
                         self.presenter.period_info_widget.addInfo.call_count)
        self.assertEqual(1, self.presenter.period_info_widget.show.call_count)
Exemple #13
0
class GroupSelectorTest(unittest.TestCase):

    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.add_three_groups_to_model()

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())

    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.group_context.add_group(group1)
        self.group_context.add_group(group2)
        self.group_context.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, 2)

    def get_group_2_selector_from_pair(self, row):
        return self.view.pairing_table.cellWidget(row, 3)

    # ------------------------------------------------------------------------------------------------------------------
    # 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_two_groups_and_then_pair_sets_combo_to_added_groups(self):
        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.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.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.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()
        # 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.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, 2))

    def test_that_removing_groups_and_then_calling_update_removes_groups_from_selections(self):
        self.presenter.handle_add_pair_button_clicked()
        self.group_context.remove_group("my_group_1")
        self.group_context.remove_group('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_selection(self):
        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.group_context.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.presenter.handle_add_pair_button_clicked()

        self.group_context.remove_group("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.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')
class AlphaTest(unittest.TestCase):
    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.add_three_groups_to_model()

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())

    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.group_context.add_group(group1)
        self.group_context.add_group(group2)
        self.group_context.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, 4), "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, 4)
        for invalid_alpha in non_numeric_alphas:
            self.view.pairing_table.setCurrentCell(0, 4)
            self.view.pairing_table.item(0, 4).setText(invalid_alpha)

            self.assertEqual(self.view.get_table_item_text(0, 4),
                             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, 4)
            self.view.pairing_table.item(0, 4).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, 4)
        # test that rounds correctly
        self.view.pairing_table.item(0, 4).setText("1.1239")

        self.assertEqual(self.view.get_table_item_text(0, 4), "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, 4)
        # test that rounds correctly
        self.view.pairing_table.item(0, 4).setText("1.1244")

        self.assertEqual(self.view.get_table_item_text(0, 4), "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, 4)
            self.view.pairing_table.item(0, 4).setText(valid_alpha)

            self.assertEqual(self.view.get_table_item_text(0, 4),
                             expected_alpha)

    def test_that_negative_alpha_is_not_allowed(self):
        self.presenter.handle_add_pair_button_clicked()

        self.view.pairing_table.setCurrentCell(0, 4)
        default_value = self.view.get_table_item_text(0, 4)
        self.view.pairing_table.item(0, 4).setText("-1.0")

        self.assertEqual(self.view.get_table_item_text(0, 4), 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, 5).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", "my_group_0", "my_group_1"])
Exemple #15
0
class PairingTablePresenterTest(unittest.TestCase):
    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        setup_context_for_tests(self)

        self.add_three_groups_to_model()

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())

    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.group_context.add_group(group1)
        self.group_context.add_group(group2)
        self.group_context.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(), 6)

    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:
            self.view.pairing_table.setCurrentCell(0, 0)
            self.view.pairing_table.item(0, 0).setText(invalid_name)

            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_warning_shown_if_duplicated_pair_name_used(self):
        self.add_two_pairs_to_table()

        self.view.enter_pair_name = mock.Mock(return_value="my_group_1")
        self.presenter.handle_add_pair_button_clicked()

        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 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()

        setup_context_for_tests(self)

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.add_three_groups_to_model()

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())

    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.group_context.add_group(group1)
        self.group_context.add_group(group2)
        self.group_context.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_two_groups_and_then_pair_sets_combo_to_added_groups(self):
        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.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.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.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()
        # 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.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.presenter.handle_add_pair_button_clicked()
        self.group_context.remove_group("my_group_1")
        self.group_context.remove_group('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_selection(self):
        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.group_context.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.presenter.handle_add_pair_button_clicked()

        self.group_context.remove_group("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.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')
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()

        setup_context_for_tests(self)

        self.add_three_groups_to_model()

        self.model = GroupingTabModel(context=self.context)
        self.view = PairingTableView(parent=self.obj)
        self.presenter = PairingTablePresenter(self.view, self.model)

        self.view.warning_popup = mock.Mock()
        self.view.enter_pair_name = mock.Mock(side_effect=pair_name())

    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.group_context.add_group(group1)
        self.group_context.add_group(group2)
        self.group_context.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_warning_shown_if_duplicated_pair_name_used(self):
        self.add_two_pairs_to_table()

        self.view.enter_pair_name = mock.Mock(return_value="my_group_1")
        self.presenter.handle_add_pair_button_clicked()

        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 GroupingTabPresenterTest(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.loaded_data = MuonLoadData()
        self.data_context = MuonDataContext(self.loaded_data)
        self.gui_context = MuonGuiContext()
        self.group_context = MuonGroupPairContext(self.data_context.check_group_contains_valid_detectors)
        self.context = MuonContext(muon_data_context=self.data_context, muon_group_context=self.group_context,
                                   muon_gui_context=self.gui_context)

        self.model = GroupingTabModel(context=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.presenter.create_update_thread = mock.MagicMock(return_value=mock.MagicMock())
        self.view.display_warning_box = mock.MagicMock()
        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

    def add_three_groups(self):
        testgroup1 = MuonGroup(group_name="fwd", detector_ids=[1, 2, 3, 4, 5])
        testgroup2 = MuonGroup(group_name="bwd", detector_ids=[6, 7, 8, 9, 10])
        testgroup3 = MuonGroup(group_name="top", detector_ids=[11, 12, 13, 14, 15])
        self.grouping_table_widget.add_group(testgroup1)
        self.grouping_table_widget.add_group(testgroup2)
        self.grouping_table_widget.add_group(testgroup3)

    def add_two_pairs(self):
        testpair1 = MuonPair(pair_name="long1", forward_group_name="fwd", backward_group_name="bwd")
        testpair2 = MuonPair(pair_name="long2", forward_group_name="fwd", backward_group_name="top")
        self.pairing_table_widget.add_pair(testpair1)
        self.pairing_table_widget.add_pair(testpair2)

    def tearDown(self):
        self.obj = None

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS
    # ------------------------------------------------------------------------------------------------------------------
    def test_context_menu_add_pair_adds_pair_if_two_groups_selected(self):
        self.assertEqual(self.pairing_table_view.num_rows(), 2)
        self.grouping_table_view._get_selected_row_indices = mock.Mock(return_value=[0, 1])
        self.grouping_table_view.contextMenuEvent(0)
        self.grouping_table_view.add_pair_action.triggered.emit(True)

        self.assertEqual(self.pairing_table_view.num_rows(), 3)

    def test_context_menu_add_pair_adds_correct_pair_if_two_groups_selected(self):
        self.grouping_table_view._get_selected_row_indices = mock.Mock(return_value=[0, 1])
        self.grouping_table_view.contextMenuEvent(0)
        self.grouping_table_view.add_pair_action.triggered.emit(True)

        pair_name = "pair_0"

        self.assertEqual(self.group_context[pair_name].forward_group, "fwd")
        self.assertEqual(self.group_context[pair_name].backward_group, "bwd")

    def test_that_clear_button_clears_model_and_view(self):
        self.view.clear_grouping_button.clicked.emit(True)

        self.assertEqual(len(self.model.groups), 0)
        self.assertEqual(len(self.model.pairs), 0)
        self.assertEqual(self.grouping_table_view.num_rows(), 0)
        self.assertEqual(self.pairing_table_view.num_rows(), 0)

    @mock.patch("Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML")
    def test_that_load_grouping_triggers_the_correct_function(self, mock_load):
        self.view.show_file_browser_and_return_selection = mock.MagicMock(return_value="grouping.xml")
        groups = [MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
                  MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])]
        pairs = [MuonPair(pair_name="pair1", forward_group_name="grp1", backward_group_name="grp2")]
        mock_load.return_value = (groups, pairs, 'description')

        self.view.load_grouping_button.clicked.emit(True)

        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(mock_load.call_args[0][0], "grouping.xml")

    def test_that_load_grouping_inserts_loaded_groups_and_pairs_correctly(self):
        self.view.show_file_browser_and_return_selection = mock.Mock(return_value="grouping.xml")
        groups = [MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
                  MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])]
        pairs = [MuonPair(pair_name="pair1", forward_group_name="grp1", backward_group_name="grp2")]

        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML") as mock_load:
            # mock the loading to return set groups/pairs
            mock_load.return_value = (groups, pairs, 'description')
            self.view.load_grouping_button.clicked.emit(True)

            six.assertCountEqual(self, self.model.group_names, ["grp1", "grp2"])
            six.assertCountEqual(self, self.model.pair_names, ["pair1"])
            self.assertEqual(self.grouping_table_view.num_rows(), 2)
            self.assertEqual(self.pairing_table_view.num_rows(), 1)
            self.assertEqual(self.pairing_table_view.pairing_table.cellWidget(0, 1).currentText(), "grp1")
            self.assertEqual(self.pairing_table_view.pairing_table.cellWidget(0, 2).currentText(), "grp2")

    def test_loading_does_not_insert_invalid_groups(self):
        self.view.show_file_browser_and_return_selection = mock.Mock(return_value="grouping.xml")
        groups = [MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
                  MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 1000])]
        pairs = [MuonPair(pair_name="pair1", forward_group_name="grp1", backward_group_name="grp2")]
        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML") as mock_load:
            # mock the loading to return set groups/pairs
            mock_load.return_value = (groups, pairs, 'description')
            self.view.load_grouping_button.clicked.emit(True)

            self.view.display_warning_box.assert_called_once_with('Invalid detectors in group grp2')
            six.assertCountEqual(self, self.model.group_names, ["grp1"])
            six.assertCountEqual(self, self.model.pair_names, [])
            self.assertEqual(self.grouping_table_view.num_rows(), 1)
            self.assertEqual(self.pairing_table_view.num_rows(), 0)

    def test_that_save_grouping_triggers_the_correct_function(self):
        # Save functionality is tested elsewhere
        self.view.show_file_save_browser_and_return_selection = mock.Mock(return_value="grouping.xml")

        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.save_grouping_to_XML") as mock_save:
            self.view.save_grouping_button.clicked.emit(True)

            self.assertEqual(mock_save.call_count, 1)
            self.assertEqual(mock_save.call_args[0][-1], "grouping.xml")

    def test_update_all_calculates_groups_and_pairs(self):
        self.presenter.handle_update_all_clicked()

        self.presenter.update_thread.threadWrapperSetUp.assert_called_once_with(self.presenter.disable_editing,
                                                                                self.presenter.handle_update_finished,
                                                                                self.presenter.error_callback)
        self.presenter.update_thread.start.assert_called_once_with()

    def test_removing_group_removes_linked_pairs(self):
        self.group_context.clear_pairs()
        self.group_context.clear_groups()
        self.add_three_groups()
        self.add_two_pairs()

        self.presenter.grouping_table_widget.remove_last_row_in_view_and_model()

        self.assertEqual(self.model.pair_names, ['long1'])
Exemple #19
0
class GroupingTabPresenterTest(unittest.TestCase):
    def setUp(self):
        self.loaded_data = MuonLoadData()

        self.context = setup_context(False)

        self.data_context = self.context.data_context
        self.gui_context = self.context.gui_context
        self.group_context = self.context.group_pair_context

        self.model = GroupingTabModel(context=self.context)

        self.grouping_table_view = GroupingTableView()
        self.grouping_table_widget = GroupingTablePresenter(
            self.grouping_table_view, self.model)

        self.pairing_table_view = PairingTableView()
        self.pairing_table_widget = PairingTablePresenter(
            self.pairing_table_view, self.model)

        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

        self.add_three_groups()
        self.add_two_pairs()

        self.view = GroupingTabView(self.grouping_table_view,
                                    self.pairing_table_view)
        self.presenter = GroupingTabPresenter(self.view, self.model,
                                              self.grouping_table_widget,
                                              self.pairing_table_widget)

        self.presenter.create_update_thread = mock.MagicMock(
            return_value=mock.MagicMock())
        self.view.display_warning_box = mock.MagicMock()
        self.grouping_table_view.warning_popup = mock.MagicMock()
        self.pairing_table_view.warning_popup = mock.MagicMock()

    def add_three_groups(self):
        testgroup1 = MuonGroup(group_name="fwd", detector_ids=[1, 2, 3, 4, 5])
        testgroup2 = MuonGroup(group_name="bwd", detector_ids=[6, 7, 8, 9, 10])
        testgroup3 = MuonGroup(group_name="top",
                               detector_ids=[11, 12, 13, 14, 15])
        self.grouping_table_widget.add_group(testgroup1)
        self.grouping_table_widget.add_group(testgroup2)
        self.grouping_table_widget.add_group(testgroup3)

    def add_two_pairs(self):
        testpair1 = MuonPair(pair_name="long1",
                             forward_group_name="fwd",
                             backward_group_name="bwd")
        testpair2 = MuonPair(pair_name="long2",
                             forward_group_name="fwd",
                             backward_group_name="top")
        self.pairing_table_widget.add_pair(testpair1)
        self.pairing_table_widget.add_pair(testpair2)

    def tearDown(self):
        self.obj = None

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS
    # ------------------------------------------------------------------------------------------------------------------
    def test_context_menu_add_pair_adds_pair_if_two_groups_selected(self):
        self.assertEqual(self.pairing_table_view.num_rows(), 2)
        self.grouping_table_view._get_selected_row_indices = mock.Mock(
            return_value=[0, 1])
        self.grouping_table_view.contextMenuEvent(0)
        self.grouping_table_view.add_pair_action.triggered.emit(True)

        self.assertEqual(self.pairing_table_view.num_rows(), 3)

    def test_context_menu_add_pair_adds_correct_pair_if_two_groups_selected(
            self):
        self.grouping_table_view._get_selected_row_indices = mock.Mock(
            return_value=[0, 1])
        self.grouping_table_view.contextMenuEvent(0)
        self.grouping_table_view.add_pair_action.triggered.emit(True)

        pair_name = "pair_0"

        self.assertEqual(self.group_context[pair_name].forward_group, "fwd")
        self.assertEqual(self.group_context[pair_name].backward_group, "bwd")

    def test_that_clear_button_clears_model_and_view(self):
        self.view.clear_grouping_button.clicked.emit(True)

        self.assertEqual(len(self.model.groups), 0)
        self.assertEqual(len(self.model.pairs), 0)
        self.assertEqual(self.grouping_table_view.num_rows(), 0)
        self.assertEqual(self.pairing_table_view.num_rows(), 0)

    @mock.patch(
        "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML"
    )
    def test_that_load_grouping_triggers_the_correct_function(self, mock_load):
        self.view.show_file_browser_and_return_selection = mock.MagicMock(
            return_value="grouping.xml")
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]
        mock_load.return_value = (groups, pairs, 'description', 'pair1')

        self.view.load_grouping_button.clicked.emit(True)

        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(mock_load.call_args[0][0], "grouping.xml")

    def test_that_load_grouping_inserts_loaded_groups_and_pairs_correctly(
            self):
        self.view.show_file_browser_and_return_selection = mock.Mock(
            return_value="grouping.xml")
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 10])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]

        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML"
        ) as mock_load:
            # mock the loading to return set groups/pairs
            mock_load.return_value = (groups, pairs, 'description', 'pair1')
            self.view.load_grouping_button.clicked.emit(True)

            six.assertCountEqual(self, self.model.group_names,
                                 ["grp1", "grp2"])
            six.assertCountEqual(self, self.model.pair_names, ["pair1"])
            self.assertEqual(self.grouping_table_view.num_rows(), 2)
            self.assertEqual(self.pairing_table_view.num_rows(), 1)
            self.assertEqual(
                self.pairing_table_view.pairing_table.cellWidget(
                    0, 1).currentText(), "grp1")
            self.assertEqual(
                self.pairing_table_view.pairing_table.cellWidget(
                    0, 2).currentText(), "grp2")

    def test_loading_does_not_insert_invalid_groups(self):
        self.view.show_file_browser_and_return_selection = mock.Mock(
            return_value="grouping.xml")
        groups = [
            MuonGroup(group_name="grp1", detector_ids=[1, 2, 3, 4, 5]),
            MuonGroup(group_name="grp2", detector_ids=[6, 7, 8, 9, 1000])
        ]
        pairs = [
            MuonPair(pair_name="pair1",
                     forward_group_name="grp1",
                     backward_group_name="grp2")
        ]
        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.load_grouping_from_XML"
        ) as mock_load:
            # mock the loading to return set groups/pairs
            mock_load.return_value = (groups, pairs, 'description', 'pair1')
            self.view.load_grouping_button.clicked.emit(True)

            self.view.display_warning_box.assert_called_once_with(
                'Invalid detectors in group grp2')
            six.assertCountEqual(self, self.model.group_names, ["grp1"])
            six.assertCountEqual(self, self.model.pair_names, [])
            self.assertEqual(self.grouping_table_view.num_rows(), 1)
            self.assertEqual(self.pairing_table_view.num_rows(), 0)

    def test_that_save_grouping_triggers_the_correct_function(self):
        # Save functionality is tested elsewhere
        self.view.show_file_save_browser_and_return_selection = mock.Mock(
            return_value="grouping.xml")

        with mock.patch(
                "Muon.GUI.Common.grouping_tab_widget.grouping_tab_widget_presenter.xml_utils.save_grouping_to_XML"
        ) as mock_save:
            self.view.save_grouping_button.clicked.emit(True)

            self.assertEqual(mock_save.call_count, 1)
            self.assertEqual(mock_save.call_args[0][-1], "grouping.xml")

    def test_update_all_calculates_groups_and_pairs(self):
        self.presenter.handle_update_all_clicked()

        self.presenter.update_thread.threadWrapperSetUp.assert_called_once_with(
            self.presenter.disable_editing,
            self.presenter.handle_update_finished,
            self.presenter.error_callback)
        self.presenter.update_thread.start.assert_called_once_with()

    def test_removing_group_removes_linked_pairs(self):
        self.group_context.clear_pairs()
        self.group_context.clear_groups()
        self.add_three_groups()
        self.add_two_pairs()

        self.presenter.grouping_table_widget.remove_last_row_in_view_and_model(
        )

        self.assertEqual(self.model.pair_names, ['long1'])