Ejemplo n.º 1
0
    def test_that_will_load_user_file(self):
        # Setup self.presenter.and mock view
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, _ = create_mock_view(user_file_path)

        self.presenter.set_view(view)

        # Mock out methods which should be called
        self.presenter._beam_centre_presenter = mock.Mock()
        self.presenter._masking_table_presenter = mock.Mock()
        self.presenter.update_view_from_model = mock.Mock()
        self.presenter._workspace_diagnostic_presenter = mock.Mock()

        with mock.patch(
                "sans.gui_logic.presenter.run_tab_presenter.FileLoading"
        ) as mocked_loader:
            mocked_loader.load_user_file.return_value = AllStates()
            self.presenter.on_user_file_load()
            mocked_loader.load_user_file.assert_called_once_with(
                file_path=user_file_path, file_information=mock.ANY)

        self.presenter._beam_centre_presenter.update_centre_positions.assert_called(
        )
        self.presenter._beam_centre_presenter.on_update_rows.assert_called()
        self.presenter._masking_table_presenter.on_update_rows.assert_called()
        self.presenter._workspace_diagnostic_presenter.on_user_file_loadassert_called(
        )

        self.presenter.update_view_from_model.assert_called()

        # Assert
        # Note that the event slices are not set in the user file

        # clean up
        remove_file(user_file_path)
Ejemplo n.º 2
0
 def _get_files_and_mock_presenter(content):
     batch_file_path = save_to_csv(content)
     user_file_path = create_user_file(sample_user_file)
     view, _, _ = create_mock_view(user_file_path, batch_file_path)
     # We just use the sample_user_file since it exists.
     view.get_mask_file = mock.MagicMock(return_value=user_file_path)
     presenter = RunTabPresenter(SANSFacility.ISIS)
     presenter.set_view(view)
     return batch_file_path, user_file_path, presenter, view
Ejemplo n.º 3
0
    def test_fails_silently_when_user_file_does_not_exist(self):
        view, _, _ = create_mock_view("non_existent_user_file")

        presenter = RunTabPresenter(SANSFacility.ISIS)
        presenter.set_view(view)

        try:
            presenter.on_user_file_load()
            has_raised = False
        except:  # noqa
            has_raised = True
        self.assertFalse(has_raised)
Ejemplo n.º 4
0
 def _get_files_and_mock_presenter(self, content, is_multi_period=True, row_user_file_path = ""):
     batch_parser = mock.MagicMock()
     batch_parser.parse_batch_file = mock.MagicMock(return_value=content)
     self.BatchCsvParserMock.return_value = batch_parser
     batch_file_path = 'batch_file_path'
     user_file_path = create_user_file(sample_user_file)
     view, _, _ = create_mock_view(user_file_path, batch_file_path, row_user_file_path)
     # We just use the sample_user_file since it exists.
     view.get_mask_file = mock.MagicMock(return_value=user_file_path)
     view.is_multi_period_view = mock.MagicMock(return_value=is_multi_period)
     presenter = RunTabPresenter(SANSFacility.ISIS)
     presenter.set_view(view)
     return batch_file_path, user_file_path, presenter, view
Ejemplo n.º 5
0
 def _get_files_and_mock_presenter(self, content, is_multi_period=True, row_user_file_path = ""):
     batch_parser = mock.MagicMock()
     batch_parser.parse_batch_file = mock.MagicMock(return_value=content)
     self.BatchCsvParserMock.return_value = batch_parser
     batch_file_path = 'batch_file_path'
     user_file_path = create_user_file(sample_user_file)
     view, _, _ = create_mock_view(user_file_path, batch_file_path, row_user_file_path)
     # We just use the sample_user_file since it exists.
     view.get_mask_file = mock.MagicMock(return_value=user_file_path)
     view.is_multi_period_view = mock.MagicMock(return_value=is_multi_period)
     presenter = RunTabPresenter(SANSFacility.ISIS)
     presenter.set_view(view)
     return batch_file_path, user_file_path, presenter, view
Ejemplo n.º 6
0
    def test_fails_silently_when_user_file_does_not_exist(self):
        self.os_patcher.stop()
        view, _, _ = create_mock_view("non_existent_user_file")

        presenter = RunTabPresenter(SANSFacility.ISIS)
        presenter.set_view(view)

        try:
            presenter.on_user_file_load()
            has_raised = False
        except:  # noqa
            has_raised = True
        self.assertFalse(has_raised)
        self.os_patcher.start()
Ejemplo n.º 7
0
    def test_batch_file_dir_not_added_to_config_if_batch_file_load_fails(self):
        presenter = RunTabPresenter(SANSFacility.ISIS)
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, masking_table = create_mock_view(
            user_file_path, "A/Path/batch_file.csv")
        presenter.set_view(view)

        presenter.on_batch_file_load()
        config_dirs = config["datasearch.directories"]
        result = "A/Path/" in config_dirs

        self.assertFalse(
            result, "We do not expect A/Path/ to be added to config, "
            "datasearch.directories is now {}".format(config_dirs))
Ejemplo n.º 8
0
    def test_that_checks_default_user_file(self):
        # Setup self.presenter.and mock view
        view, settings_diagnostic_tab, _ = create_mock_view("")

        self.presenter.set_view(view)

        self.assertEqual(
            self.presenter._view.set_out_default_user_file.call_count, 1,
            "Expected mock to have been called once. Called {} times.".format(
                self.presenter._view.set_out_default_user_file.call_count))

        self.assertEqual(
            self.presenter._view._call_settings_listeners.call_count, 0,
            "Expected mock to not have been called. Called {} times.".format(
                self.presenter._view._call_settings_listeners.call_count))
    def test_that_checks_default_user_file(self):
        # Setup presenter and mock view
        view, settings_diagnostic_tab, _ = create_mock_view("")
        presenter = RunTabPresenter(SANSFacility.ISIS)
        presenter.set_view(view)

        self.assertEqual(
            presenter._view.set_out_default_user_file.call_count, 1,
            "Expected mock to have been called once. Called {} times.".format(
                presenter._view.set_out_default_user_file.call_count))

        self.assertEqual(
            presenter._view._call_settings_listeners.call_count, 0,
            "Expected mock to not have been called. Called {} times.".format(
                presenter._view._call_settings_listeners.call_count))
Ejemplo n.º 10
0
    def test_fails_silently_when_batch_file_does_not_exist(self):
        presenter = RunTabPresenter(SANSFacility.ISIS)
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, masking_table = create_mock_view(
            user_file_path, "non_existent_batch_file")
        presenter.set_view(view)

        try:
            presenter.on_batch_file_load()
            has_raised = False
        except:  # noqa
            has_raised = True
        self.assertFalse(has_raised)

        # Clean up
        self._remove_files(user_file_path=user_file_path)
Ejemplo n.º 11
0
    def test_that_returns_none_when_index_does_not_exist(self):
        # Arrange
        batch_file_path, user_file_path, presenter, _ = self._get_files_and_mock_presenter(BATCH_FILE_TEST_CONTENT_2)
        view, _, _ = create_mock_view(user_file_path, batch_file_path)
        presenter.set_view(view)
        presenter.on_user_file_load()
        presenter.on_batch_file_load()

        # Act
        state = presenter.get_state_for_row(3)

        # Assert
        self.assertTrue(state is None)

        # Clean up
        remove_file(batch_file_path)
        remove_file(user_file_path)
Ejemplo n.º 12
0
    def test_fails_silently_when_batch_file_does_not_exist(self):
        self.os_patcher.stop()
        presenter = RunTabPresenter(SANSFacility.ISIS)
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, masking_table = create_mock_view(user_file_path, "non_existent_batch_file")
        presenter.set_view(view)

        try:
            presenter.on_batch_file_load()
            has_raised = False
        except:  # noqa
            has_raised = True
        self.assertFalse(has_raised)

        # Clean up
        self._remove_files(user_file_path=user_file_path)
        self.os_patcher.start()
Ejemplo n.º 13
0
    def test_that_returns_none_when_index_does_not_exist(self):
        # Arrange
        batch_file_path, user_file_path, presenter, _ = self._get_files_and_mock_presenter(BATCH_FILE_TEST_CONTENT_2)
        view, _, _ = create_mock_view(user_file_path, batch_file_path)
        presenter.set_view(view)
        presenter.on_user_file_load()
        presenter.on_batch_file_load()

        # Act
        state = presenter.get_state_for_row(3)

        # Assert
        self.assertTrue(state is None)

        # Clean up
        remove_file(batch_file_path)
        remove_file(user_file_path)
Ejemplo n.º 14
0
    def test_that_returns_none_when_index_does_not_exist(self):
        # Arrange
        batch_file_path = save_to_csv(BATCH_FILE_TEST_CONTENT_2)
        user_file_path = create_user_file(sample_user_file)
        view, _, _ = create_mock_view(user_file_path, batch_file_path)
        presenter = RunTabPresenter(SANSFacility.ISIS)
        presenter.set_view(view)
        presenter.on_user_file_load()
        presenter.on_batch_file_load()

        # Act
        state = presenter.get_state_for_row(3)

        # Assert
        self.assertTrue(state is None)

        # Clean up
        remove_file(batch_file_path)
        remove_file(user_file_path)
Ejemplo n.º 15
0
    def _get_files_and_mock_presenter(self,
                                      content,
                                      is_multi_period=True,
                                      row_user_file_path=""):
        if row_user_file_path:
            content[1].user_file = row_user_file_path

        batch_parser = mock.MagicMock()
        batch_parser.parse_batch_file = mock.MagicMock(return_value=content)
        self._mock_csv_parser.return_value = batch_parser
        batch_file_path = 'batch_file_path'

        user_file_path = create_user_file(sample_user_file)
        view, _, _ = create_mock_view(user_file_path, batch_file_path,
                                      row_user_file_path)
        # We just use the sample_user_file since it exists.
        view.get_mask_file = mock.MagicMock(return_value=user_file_path)
        view.is_multi_period_view = mock.MagicMock(
            return_value=is_multi_period)
        self.presenter.set_view(view)
        return batch_file_path, user_file_path, view
Ejemplo n.º 16
0
    def test_fails_silently_when_user_file_does_not_exist(self):
        view, _, _ = create_mock_view("non_existent_user_file")
        self.presenter.set_view(view)

        self.assertIsNone(self.presenter.on_user_file_load())
Ejemplo n.º 17
0
    def test_that_will_load_user_file(self):
        # Setup presenter and mock view
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, _ = create_mock_view(user_file_path)
        presenter = RunTabPresenter(SANSFacility.ISIS)
        presenter.set_view(view)

        # Act
        presenter.on_user_file_load()

        # Assert
        # Note that the event slices are not set in the user file
        self.assertFalse(view.event_slices)
        self.assertTrue(view.reduction_dimensionality is ReductionDimensionality.OneDim)
        self.assertTrue(view.save_types[0] is SaveType.NXcanSAS)
        self.assertTrue(view.zero_error_free)
        self.assertTrue(view.use_optimizations)
        self.assertTrue(view.reduction_mode is ISISReductionMode.LAB)
        self.assertTrue(view.merge_scale == 1.)
        self.assertTrue(view.merge_shift == 0.)
        self.assertFalse(view.merge_scale_fit)
        self.assertFalse(view.merge_shift_fit)
        self.assertTrue(view.event_binning == "7000.0,500.0,60000.0")
        self.assertTrue(view.wavelength_step_type is RangeStepType.Lin)
        self.assertTrue(view.wavelength_min == 1.5)
        self.assertTrue(view.wavelength_max == 12.5)
        self.assertTrue(view.wavelength_step == 0.125)
        self.assertTrue(view.absolute_scale == 0.074)
        self.assertTrue(view.z_offset == 53.)
        self.assertTrue(view.normalization_incident_monitor == 1)
        self.assertTrue(view.normalization_interpolate)
        self.assertTrue(view.transmission_incident_monitor == 1)
        self.assertTrue(view.transmission_interpolate)
        self.assertTrue(view.transmission_roi_files == "test2.xml")
        self.assertTrue(view.transmission_mask_files == "test4.xml")
        self.assertTrue(view.transmission_radius == 7.)
        self.assertTrue(view.transmission_monitor == 4)
        self.assertTrue(view.transmission_mn_shift == -70)
        self.assertTrue(view.transmission_sample_use_fit)
        self.assertTrue(view.transmission_sample_fit_type is FitType.Logarithmic)
        self.assertTrue(view.transmission_sample_polynomial_order == 2)
        self.assertTrue(view.transmission_sample_wavelength_min == 1.5)
        self.assertTrue(view.transmission_sample_wavelength_max == 12.5)
        self.assertTrue(view.transmission_sample_use_wavelength)
        self.assertFalse(view.pixel_adjustment_det_1)
        self.assertFalse(view.pixel_adjustment_det_2)
        self.assertFalse(view.wavelength_adjustment_det_1)
        self.assertFalse(view.wavelength_adjustment_det_2)
        self.assertTrue(view.q_1d_min_or_rebin_string == "0.001,0.001,0.0126,-0.08,0.2")
        self.assertTrue(view.q_xy_max == 0.05)
        self.assertTrue(view.q_xy_step == 0.001)
        self.assertTrue(view.q_xy_step_type == RangeStepType.Lin)
        self.assertTrue(view.gravity_on_off)
        self.assertTrue(view.use_q_resolution)
        self.assertTrue(view.q_resolution_sample_a == 14.)
        self.assertTrue(view.q_resolution_source_a == 13.)
        self.assertTrue(view.q_resolution_delta_r == 11.)
        self.assertTrue(view.q_resolution_collimation_length == 12.)
        self.assertTrue(view.q_resolution_moderator_file == "moderator_rkh_file.txt")
        self.assertFalse(view.phi_limit_use_mirror)
        self.assertTrue(view.radius_limit_min == 12.)
        self.assertTrue(view.radius_limit_min == 12.)
        self.assertTrue(view.radius_limit_max == 15.)
        self.assertFalse(view.compatibility_mode)
        self.assertFalse(view.show_transmission)

        # Assert that Beam Centre View is updated correctly
        self.assertEqual(view.beam_centre.lab_pos_1, 155.45)
        self.assertEqual(view.beam_centre.lab_pos_2, -169.6)
        self.assertEqual(view.beam_centre.hab_pos_1, 155.45)
        self.assertEqual(view.beam_centre.hab_pos_2, -169.6)

        # Assert certain function calls
        self.assertTrue(view.get_user_file_path.call_count == 3)
        self.assertTrue(view.get_batch_file_path.call_count == 2)  # called twice for the sub presenter updates (masking table and settings diagnostic tab)  # noqa
        self.assertTrue(view.get_cell.call_count == 64)

        self.assertTrue(view.get_number_of_rows.call_count == 6)

        # clean up
        remove_file(user_file_path)
Ejemplo n.º 18
0
    def test_that_will_load_user_file(self):
        # Setup presenter and mock view
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, _ = create_mock_view(user_file_path)
        presenter = RunTabPresenter(SANSFacility.ISIS)
        presenter.set_view(view)

        # Act
        presenter.on_user_file_load()

        # Assert
        # Note that the event slices are not set in the user file
        self.assertFalse(view.event_slices)
        self.assertTrue(
            view.reduction_dimensionality is ReductionDimensionality.OneDim)
        self.assertTrue(view.save_types[0] is SaveType.NXcanSAS)
        self.assertTrue(view.zero_error_free)
        self.assertTrue(view.use_optimizations)
        self.assertTrue(view.reduction_mode is ISISReductionMode.LAB)
        self.assertTrue(view.merge_scale == 1.)
        self.assertTrue(view.merge_shift == 0.)
        self.assertFalse(view.merge_scale_fit)
        self.assertFalse(view.merge_shift_fit)
        self.assertTrue(view.event_binning == "7000.0,500.0,60000.0")
        self.assertTrue(view.wavelength_step_type is RangeStepType.Lin)
        self.assertTrue(view.wavelength_min == 1.5)
        self.assertTrue(view.wavelength_max == 12.5)
        self.assertTrue(view.wavelength_step == 0.125)
        self.assertTrue(view.absolute_scale == 0.074)
        self.assertTrue(view.z_offset == 53.)
        self.assertTrue(view.normalization_incident_monitor == 1)
        self.assertTrue(view.normalization_interpolate)
        self.assertTrue(view.transmission_incident_monitor == 1)
        self.assertTrue(view.transmission_interpolate)
        self.assertTrue(view.transmission_roi_files == "test2.xml")
        self.assertTrue(view.transmission_mask_files == "test4.xml")
        self.assertTrue(view.transmission_radius == 7.)
        self.assertTrue(view.transmission_monitor == 4)
        self.assertTrue(view.transmission_mn_shift == -70)
        self.assertTrue(view.transmission_sample_use_fit)
        self.assertTrue(
            view.transmission_sample_fit_type is FitType.Logarithmic)
        self.assertTrue(view.transmission_sample_polynomial_order == 2)
        self.assertTrue(view.transmission_sample_wavelength_min == 1.5)
        self.assertTrue(view.transmission_sample_wavelength_max == 12.5)
        self.assertTrue(view.transmission_sample_use_wavelength)
        self.assertFalse(view.pixel_adjustment_det_1)
        self.assertFalse(view.pixel_adjustment_det_2)
        self.assertFalse(view.wavelength_adjustment_det_1)
        self.assertFalse(view.wavelength_adjustment_det_2)
        self.assertTrue(
            view.q_1d_min_or_rebin_string == "0.001,0.001,0.0126,-0.08,0.2")
        self.assertTrue(view.q_xy_max == 0.05)
        self.assertTrue(view.q_xy_step == 0.001)
        self.assertTrue(view.q_xy_step_type == RangeStepType.Lin)
        self.assertTrue(view.gravity_on_off)
        self.assertTrue(view.use_q_resolution)
        self.assertTrue(view.q_resolution_sample_a == 14.)
        self.assertTrue(view.q_resolution_source_a == 13.)
        self.assertTrue(view.q_resolution_delta_r == 11.)
        self.assertTrue(view.q_resolution_collimation_length == 12.)
        self.assertTrue(
            view.q_resolution_moderator_file == "moderator_rkh_file.txt")
        self.assertTrue(view.phi_limit_use_mirror)
        self.assertTrue(view.radius_limit_min == 12.)
        self.assertTrue(view.radius_limit_min == 12.)
        self.assertTrue(view.radius_limit_max == 15.)
        self.assertFalse(view.compatibility_mode)
        self.assertTrue(view.show_transmission)

        # Assert that Beam Centre View is updated correctly
        self.assertEqual(view.beam_centre.lab_pos_1, 155.45)
        self.assertEqual(view.beam_centre.lab_pos_2, -169.6)
        self.assertEqual(view.beam_centre.hab_pos_1, 155.45)
        self.assertEqual(view.beam_centre.hab_pos_2, -169.6)

        # clean up
        remove_file(user_file_path)
Ejemplo n.º 19
0
    def test_that_will_load_user_file(self):
        # Setup self.presenter.and mock view
        user_file_path = create_user_file(sample_user_file)
        view, settings_diagnostic_tab, _ = create_mock_view(user_file_path)

        self.presenter.set_view(view)

        # Act
        try:
            self.presenter.on_user_file_load()
        except RuntimeError:
            # Assert that RuntimeError from no instrument is caught
            self.fail(
                "on_user_file_load raises a RuntimeError which should be caught"
            )

        # Assert
        # Note that the event slices are not set in the user file
        self.assertFalse(view.event_slices)
        self.assertEqual(view.reduction_dimensionality,
                         ReductionDimensionality.ONE_DIM)
        self.assertEqual(view.save_types[0], SaveType.NX_CAN_SAS)
        self.assertTrue(view.zero_error_free)
        self.assertTrue(view.use_optimizations)
        self.assertEqual(view.reduction_mode, ReductionMode.LAB)
        self.assertEqual(view.merge_scale, 1.)
        self.assertEqual(view.merge_shift, 0.)
        self.assertFalse(view.merge_scale_fit)
        self.assertFalse(view.merge_shift_fit)
        self.assertEqual(view.event_binning, "7000.0,500.0,60000.0")
        self.assertEqual(view.wavelength_step_type, RangeStepType.LIN)
        self.assertEqual(view.wavelength_min, 1.5)
        self.assertEqual(view.wavelength_max, 12.5)
        self.assertEqual(view.wavelength_step, 0.125)
        self.assertEqual(view.absolute_scale, 0.074)
        self.assertEqual(view.z_offset, 53.)
        self.assertEqual(view.normalization_incident_monitor, 1)
        self.assertTrue(view.normalization_interpolate)
        self.assertEqual(view.transmission_incident_monitor, 1)
        self.assertTrue(view.transmission_interpolate)
        self.assertEqual(view.transmission_roi_files, "test2.xml")
        self.assertEqual(view.transmission_mask_files, "test4.xml")
        self.assertEqual(view.transmission_radius, 7.)
        self.assertEqual(view.transmission_monitor, 4)
        self.assertEqual(view.transmission_mn_4_shift, -70)
        self.assertTrue(view.transmission_sample_use_fit)
        self.assertEqual(view.transmission_sample_fit_type,
                         FitType.LOGARITHMIC)
        self.assertEqual(view.transmission_sample_polynomial_order, 2)
        self.assertEqual(view.transmission_sample_wavelength_min, 1.5)
        self.assertEqual(view.transmission_sample_wavelength_max, 12.5)
        self.assertTrue(view.transmission_sample_use_wavelength)
        self.assertFalse(view.pixel_adjustment_det_1)
        self.assertFalse(view.pixel_adjustment_det_2)
        self.assertFalse(view.wavelength_adjustment_det_1)
        self.assertFalse(view.wavelength_adjustment_det_2)
        self.assertEqual(view.q_1d_min_or_rebin_string,
                         "0.001,0.001,0.0126,-0.08,0.2")
        self.assertEqual(view.q_xy_max, 0.05)
        self.assertEqual(view.q_xy_step, 0.001)
        self.assertEqual(view.q_xy_step_type, RangeStepType.LIN)
        self.assertTrue(view.gravity_on_off)
        self.assertTrue(view.use_q_resolution)
        self.assertEqual(view.q_resolution_sample_a, 14.)
        self.assertEqual(view.q_resolution_source_a, 13.)
        self.assertEqual(view.q_resolution_delta_r, 11.)
        self.assertEqual(view.q_resolution_collimation_length, 12.)
        self.assertEqual(view.q_resolution_moderator_file,
                         "moderator_rkh_file.txt")
        self.assertTrue(view.phi_limit_use_mirror)
        self.assertEqual(view.radius_limit_min, 12.)
        self.assertEqual(view.radius_limit_min, 12.)
        self.assertEqual(view.radius_limit_max, 15.)
        self.assertTrue(view.compatibility_mode)

        # clean up
        remove_file(user_file_path)