예제 #1
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.gui_variable_observer = Observer()

        self.gui_context.gui_variables_notifier.add_subscriber(self.gui_variable_observer)
        self.model = GroupingTabModel(context=self.context)
        self.presenter = DifferencePresenter(self.model)

        # Mock user input for getting diff name
        self.presenter.group_view.enter_diff_name = mock.Mock(side_effect=enter_diff_name_side_effect())
        self.presenter.pair_view.enter_diff_name = mock.Mock(side_effect=enter_diff_name_side_effect())

        # Mock warning methods
        self.presenter.group_view.warning_popup = mock.Mock()
        self.presenter.pair_view.warning_popup = mock.Mock()
    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()
예제 #3
0
    def __init__(self, context, parent=None):

        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.diff_table = DifferencePresenter(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.diff_table.view)
        self.group_tab_presenter = GroupingTabPresenter(
            self.group_tab_view, self.group_tab_model,
            self.grouping_table_widget, self.pairing_table_widget,
            self.diff_table, parent)

        context.update_view_from_model_notifier.add_subscriber(
            self.group_tab_presenter.update_view_from_model_observer)
예제 #4
0
class DifferenceTablePresenterTest(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.gui_variable_observer = Observer()

        self.gui_context.gui_variables_notifier.add_subscriber(self.gui_variable_observer)
        self.model = GroupingTabModel(context=self.context)
        self.presenter = DifferencePresenter(self.model)

        # Mock user input for getting diff name
        self.presenter.group_view.enter_diff_name = mock.Mock(side_effect=enter_diff_name_side_effect())
        self.presenter.pair_view.enter_diff_name = mock.Mock(side_effect=enter_diff_name_side_effect())

        # Mock warning methods
        self.presenter.group_view.warning_popup = mock.Mock()
        self.presenter.pair_view.warning_popup = mock.Mock()

    def tearDown(self):
        self.obj = None

    def assert_model_empty(self):
        self.assertEqual(0, len(self.model.diff_names))
        self.assertEqual(0, len(self.model.diffs))

    def assert_view_empty(self):
        self.assertEqual(0, self.presenter.group_view.num_rows())
        self.assertEqual(0, self.presenter.pair_view.num_rows())

    def add_two_groups(self):
        group0 = MuonGroup(group_name="group_0", detector_ids=[1])
        group1 = MuonGroup(group_name="group_1", detector_ids=[2])
        self.model.add_group(group0)
        self.model.add_group(group1)

    def add_two_pairs(self):
        pair0 = MuonPair(pair_name="pair_0", forward_group_name="group_0", backward_group_name="group_1", alpha=1.0)
        pair1 = MuonPair(pair_name="pair_1", forward_group_name="group_1", backward_group_name="group_0", alpha=1.0)
        self.model.add_pair(pair0)
        self.model.add_pair(pair1)

    def add_two_group_diffs(self):
        self.add_two_groups()
        diff0 = MuonDiff('group_diff_0', 'group_0', 'group_1')
        diff1 = MuonDiff('group_diff_1', 'group_1', 'group_0')
        self.presenter.group_widget.add_diff(diff0)
        self.presenter.group_widget.add_diff(diff1)

    def add_two_pair_diffs(self):
        self.add_two_groups()
        self.add_two_pairs()
        diff0 = MuonDiff('pair_diff_0', 'pair_0', 'pair_1', group_or_pair='pair')
        diff1 = MuonDiff('pair_diff_1', 'pair_1', 'pair_0', group_or_pair='pair')
        self.presenter.pair_widget.add_diff(diff0)
        self.presenter.pair_widget.add_diff(diff1)

    def get_group_1_selector_from_diff(self, row):
        return self.presenter.group_view.diff_table.cellWidget(row, 2)

    def get_group_2_selector_from_diff(self, row):
        return self.presenter.group_view.diff_table.cellWidget(row, 3)

    def get_pair_1_selector_from_diff(self, row):
        return self.presenter.pair_view.diff_table.cellWidget(row, 2)

    def get_pair_2_selector_from_diff(self, row):
        return self.presenter.pair_view.diff_table.cellWidget(row, 3)

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS : test the functionality around the combo boxes which allow the user to select the two groups or two pairs
    #         that together make the muon difference.
    # ------------------------------------------------------------------------------------------------------------------

    def test_adding_groups_then_group_diff_sets_combo_boxes_to_added_groups(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()

        self.assertEqual('group_0', self.get_group_1_selector_from_diff(0).currentText())
        self.assertEqual('group_1', self.get_group_2_selector_from_diff(0).currentText())

    def test_adding_pairs_then_pair_diff_sets_combo_boxes_to_added_pairs(self):
        self.add_two_pairs()
        self.presenter.pair_widget.handle_add_diff_button_clicked()

        self.assertEqual('pair_0', self.get_pair_1_selector_from_diff(0).currentText())
        self.assertEqual('pair_1', self.get_pair_2_selector_from_diff(0).currentText())

    def test_get_index_of_text_returns_correct_index_if_text_exists(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()
        index = self.presenter.group_view.get_index_of_text(self.get_group_1_selector_from_diff(0), 'group_1')

        self.assertEqual(index, 1)

    def test_get_index_of_text_returns_0_if_text_does_not_exists(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()
        index = self.presenter.group_view.get_index_of_text(self.get_group_1_selector_from_diff(0), 'not_a_group_1')

        self.assertEqual(index, 0)

    def test_added_groups_appear_in_selector_combo_boxes_in_existing_diffs(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()
        self.presenter.update_view_from_model()

        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertNotEqual(-1, self.get_group_1_selector_from_diff(0).findText('group_0'))
        self.assertNotEqual(-1, self.get_group_1_selector_from_diff(0).findText('group_1'))

    def test_removing_groups_removes_them_from_selection(self):
        self.add_two_groups()
        # Add an extra group so we can remove one and keep a diff
        group = MuonGroup(group_name="group_2", detector_ids=[2])
        self.model.add_group(group)
        self.presenter.group_widget.handle_add_diff_button_clicked(group_1='group_0',group_2='group_2')

        self.group_context.remove_group('group_1')
        self.presenter.update_view_from_model()

        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual(2, self.get_group_2_selector_from_diff(0).count())
        self.assertEqual(-1, self.get_group_2_selector_from_diff(0).findText('group_1'))
        self.assertEqual(-1, self.get_group_2_selector_from_diff(0).findText('group_1'))

    def test_changing_group_selection_triggers_cell_changed_method_in_view(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()
        self.presenter.group_widget.handle_add_diff_button_clicked()
        self.presenter.group_view.on_cell_changed = mock.Mock()
        self.get_group_1_selector_from_diff(0).setCurrentIndex(1)

        self.assertEqual(1, self.presenter.group_view.on_cell_changed.call_count)
        self.assertEqual((0,2), self.presenter.group_view.on_cell_changed.call_args_list[0][0])

    def test_adding_new_group_does_not_change_current_selection(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()

        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual(2, self.get_group_2_selector_from_diff(0).count())
        self.assertEqual('group_0', self.get_group_1_selector_from_diff(0).currentText())
        self.assertEqual('group_1', self.get_group_2_selector_from_diff(0).currentText())

        group = MuonGroup(group_name="group_2", detector_ids=[2])
        self.model.add_group(group)
        self.presenter.update_view_from_model()

        self.assertEqual(3, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual(3, self.get_group_2_selector_from_diff(0).count())
        self.assertEqual('group_0', self.get_group_1_selector_from_diff(0).currentText())
        self.assertEqual('group_1', self.get_group_2_selector_from_diff(0).currentText())

    def test_changing_group_to_other_group_switches_groups(self):
        self.add_two_groups()
        self.presenter.group_widget.handle_add_diff_button_clicked()

        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual('group_0', self.get_group_1_selector_from_diff(0).currentText())
        self.assertEqual('group_1', self.get_group_2_selector_from_diff(0).currentText())

        self.get_group_1_selector_from_diff(0).setCurrentIndex(1)

        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual(2, self.get_group_1_selector_from_diff(0).count())
        self.assertEqual('group_1', self.get_group_1_selector_from_diff(0).currentText())
        self.assertEqual('group_0', self.get_group_2_selector_from_diff(0).currentText())
예제 #5
0
class DifferenceWidgetPresenterTest(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.gui_variable_observer = Observer()

        self.gui_context.gui_variables_notifier.add_subscriber(
            self.gui_variable_observer)
        self.model = GroupingTabModel(context=self.context)
        self.presenter = DifferencePresenter(self.model)

        # Mock user input for getting diff name
        self.presenter.group_view.enter_diff_name = mock.Mock(
            side_effect=enter_diff_name_side_effect())
        self.presenter.pair_view.enter_diff_name = mock.Mock(
            side_effect=enter_diff_name_side_effect())

        # Mock warning methods
        self.presenter.group_view.warning_popup = mock.Mock()
        self.presenter.pair_view.warning_popup = mock.Mock()

    def tearDown(self):
        self.obj = None

    def assert_model_empty(self):
        self.assertEqual(0, len(self.model.diff_names))
        self.assertEqual(0, len(self.model.diffs))

    def assert_view_empty(self):
        self.assertEqual(0, self.presenter.group_view.num_rows())
        self.assertEqual(0, self.presenter.pair_view.num_rows())

    def add_two_groups(self):
        group0 = MuonGroup(group_name="group_0", detector_ids=[1])
        group1 = MuonGroup(group_name="group_1", detector_ids=[2])
        self.model.add_group(group0)
        self.model.add_group(group1)

    def add_two_pairs(self):
        pair0 = MuonPair(pair_name="pair_0",
                         forward_group_name="group_0",
                         backward_group_name="group_1",
                         alpha=1.0)
        pair1 = MuonPair(pair_name="pair_1",
                         forward_group_name="group_1",
                         backward_group_name="group_0",
                         alpha=1.0)
        self.model.add_pair(pair0)
        self.model.add_pair(pair1)

    def add_two_group_diffs(self):
        if not self.model.group_names:
            self.add_two_groups()
        diff0 = MuonDiff('group_diff_0', 'group_0', 'group_1')
        diff1 = MuonDiff('group_diff_1', 'group_1', 'group_0')
        self.presenter.group_widget.add_diff(diff0)
        self.presenter.group_widget.add_diff(diff1)

    def add_two_pair_diffs(self):
        if not self.model.group_names:
            self.add_two_groups()
        if not self.model.pair_names:
            self.add_two_pairs()
        diff0 = MuonDiff('pair_diff_0',
                         'pair_0',
                         'pair_1',
                         group_or_pair='pair')
        diff1 = MuonDiff('pair_diff_1',
                         'pair_1',
                         'pair_0',
                         group_or_pair='pair')
        self.presenter.pair_widget.add_diff(diff0)
        self.presenter.pair_widget.add_diff(diff1)

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS : Tests for the widget presenter
    # ------------------------------------------------------------------------------------------------------------------

    def test_update_view_from_model_is_empty(self):
        self.add_two_group_diffs()
        self.add_two_pair_diffs()

        # Change the model
        self.model.clear_diffs('group')
        self.model.clear_diffs('pair')

        # Now update the view
        self.presenter.update_view_from_model()

        self.assert_view_empty()
        self.assert_model_empty()

    def test_pair_model_unchanged(self):
        self.add_two_group_diffs()

        # Change model
        self.group_context.remove_diff('group_diff_0')

        # Update view
        self.presenter.update_view_from_model()

        self.assertEqual(0, self.presenter.pair_view.num_rows())
        self.assertEqual(1, self.presenter.group_view.num_rows())

    def test_group_model_unchanged(self):
        self.add_two_pair_diffs()

        # Change model
        self.group_context.remove_diff('pair_diff_0')

        # Update view
        self.presenter.update_view_from_model()

        self.assertEqual(1, self.presenter.pair_view.num_rows())
        self.assertEqual(0, self.presenter.group_view.num_rows())

    def test_tables_are_disabled_correctly(self):
        self.presenter.group_view.disable_editing = mock.Mock()
        self.presenter.pair_view.disable_editing = mock.Mock()
        self.presenter.disable_editing()

        self.assertEqual(1,
                         self.presenter.group_view.disable_editing.call_count)
        self.assertEqual(1,
                         self.presenter.pair_view.disable_editing.call_count)

    def test_tables_are_enabled_correctly(self):
        self.presenter.group_view.enable_editing = mock.Mock()
        self.presenter.pair_view.enable_editing = mock.Mock()
        self.presenter.enable_editing()

        self.assertEqual(1,
                         self.presenter.group_view.enable_editing.call_count)
        self.assertEqual(1, self.presenter.pair_view.enable_editing.call_count)

    def test_add_subscribers(self):
        # Make some fake observers
        observer_1 = Observer()
        observer_2 = Observer()
        self.presenter.add_subscribers([observer_1, observer_2])

        self.assertEqual([observer_1, observer_2], self.presenter.group_widget.
                         selected_diff_changed_notifier._subscribers)
        self.assertEqual([observer_1, observer_2], self.presenter.pair_widget.
                         selected_diff_changed_notifier._subscribers)