Beispiel #1
0
class WorkloadManagementTest(unittest.TestCase):
    def setUp(self):
        self.progress_manager = InMemoryProgressManager(
            Mock(spec=LocalDirectoryWorkingContext))
        self.workunit_provider = Mock(spec=WorkUnitProvider)

        self.workunit1 = MagicMock(spec=WorkUnit)
        self.file1 = "file1"
        self.workunit1.get_filename.return_value = self.file1

        self.workunit2 = MagicMock(spec=WorkUnit)
        self.file2 = "file2"
        self.workunit2.get_filename.return_value = self.file2

        workunits = [self.workunit1, self.workunit2]

        def get_workunit(index):
            workunit = workunits[index]
            self.progress_manager.lock(workunit.get_filename())
            return workunit

        self.workunit_provider.get_workunit.side_effect = (
            get_workunit(index) for index in range(2))
        image_manager = Mock(spec=ImageManager)

        self.undertest = ValidationModel(self.workunit_provider, image_manager,
                                         None)
        self.undertest.start_work()

    def test_workunits_on_demand(self):
        assert_that(self.undertest.get_current_workunit(),
                    equal_to(self.workunit1))
        assert_that(self.workunit_provider.get_workunit.call_count,
                    equal_to(1))

        self.undertest.next_workunit()
        assert_that(self.undertest.get_current_workunit(),
                    equal_to(self.workunit2))
        assert_that(self.workunit_provider.get_workunit.call_count,
                    equal_to(2))

    def test_shift_locks(self):
        assert_that(self.undertest.get_current_workunit(),
                    equal_to(self.workunit1))
        assert_that(self.progress_manager.owns_lock(self.file1),
                    equal_to(True))
        assert_that(self.progress_manager.owns_lock(self.file2),
                    equal_to(False))

        self.undertest.next_workunit()
        assert_that(self.undertest.get_current_workunit(),
                    equal_to(self.workunit2))

        # Note we don't give up lock just by going to next workunit.  It is
        # released once it is done processing.
        assert_that(self.progress_manager.owns_lock(self.file1),
                    equal_to(True))
        assert_that(self.progress_manager.owns_lock(self.file2),
                    equal_to(True))
Beispiel #2
0
class GeneralModelTest(FileReadingTestCase, DirectoryCleaningTestCase):
    def setUp(self):
        events.unsub_all()

        parser = AstromParser()
        directory_manager = LocalDirectoryWorkingContext(
            self._get_working_dir())
        self.context = directory_manager
        progress_manager = LocalProgressManager(directory_manager)
        workunit_provider = WorkUnitProvider(
            tasks.get_suffix(self._get_task()), directory_manager,
            progress_manager,
            self._get_workunit_builder(parser, progress_manager))

        self.workunit_provider = workunit_provider
        self.progress_manager = progress_manager

        self.singlet_download_manager = Mock(spec=AsynchronousDownloadManager)
        self.triplet_download_manager = Mock(spec=AsynchronousDownloadManager)
        self.image_manager = ImageManager(self.singlet_download_manager,
                                          self.triplet_download_manager)

        self.model = ValidationModel(self.workunit_provider,
                                     self.image_manager, None)

        self.custom_setup()

        self.model.start_work()

    def custom_setup(self):
        pass

    def _get_task(self):
        raise NotImplementedError()

    def _get_working_dir(self):
        raise NotImplementedError()

    def get_directory_to_clean(self):
        return self._get_working_dir()

    def _get_workunit_builder(self, parser, progress_manager):
        raise NotImplementedError()

    def create_real_first_image(self, path="data/testimg.fits"):
        # Put a real fits image on the first source, first observation
        apcor = ApcorData.from_string("4 15   0.19   0.01")
        hdulist = fits.open(self.get_abs_path(path))
        first_reading = self.model.get_current_workunit().get_sources(
        )[0].get_readings()[0]
        self.first_snapshot = SourceCutout(first_reading, hdulist, apcor)
        self.image_manager.on_singlet_image_loaded(self.first_snapshot)
Beispiel #3
0
class GeneralModelTest(FileReadingTestCase, DirectoryCleaningTestCase):
    def setUp(self):
        events.unsub_all()

        parser = AstromParser()
        directory_manager = LocalDirectoryWorkingContext(self._get_working_dir())
        self.context = directory_manager
        progress_manager = LocalProgressManager(directory_manager)
        workunit_provider = WorkUnitProvider(tasks.get_suffix(self._get_task()),
                                             directory_manager, progress_manager,
                                             self._get_workunit_builder(parser, progress_manager))

        self.workunit_provider = workunit_provider
        self.progress_manager = progress_manager

        self.singlet_download_manager = Mock(spec=AsynchronousDownloadManager)
        self.triplet_download_manager = Mock(spec=AsynchronousDownloadManager)
        self.image_manager = ImageManager(self.singlet_download_manager,
                                          self.triplet_download_manager)

        self.model = ValidationModel(self.workunit_provider, self.image_manager, None)

        self.custom_setup()

        self.model.start_work()

    def custom_setup(self):
        pass

    def _get_task(self):
        raise NotImplementedError()

    def _get_working_dir(self):
        raise NotImplementedError()

    def get_directory_to_clean(self):
        return self._get_working_dir()

    def _get_workunit_builder(self, parser, progress_manager):
        raise NotImplementedError()

    def create_real_first_image(self, path="data/testimg.fits"):
        # Put a real fits image on the first source, first observation
        apcor = ApcorData.from_string("4 15   0.19   0.01")
        hdulist = fits.open(self.get_abs_path(path))
        first_reading = self.model.get_current_workunit().get_sources()[0].get_readings()[0]
        self.first_snapshot = SourceCutout(
            first_reading, hdulist, CoordinateConverter(0, 0), apcor)
        self.image_manager.on_singlet_image_loaded(self.first_snapshot)
Beispiel #4
0
class WorkloadManagementTest(unittest.TestCase):
    def setUp(self):
        self.progress_manager = InMemoryProgressManager(Mock(spec=LocalDirectoryWorkingContext))
        self.workunit_provider = Mock(spec=WorkUnitProvider)

        self.workunit1 = MagicMock(spec=WorkUnit)
        self.file1 = "file1"
        self.workunit1.get_filename.return_value = self.file1

        self.workunit2 = MagicMock(spec=WorkUnit)
        self.file2 = "file2"
        self.workunit2.get_filename.return_value = self.file2

        workunits = [self.workunit1, self.workunit2]

        def get_workunit(index):
            workunit = workunits[index]
            self.progress_manager.lock(workunit.get_filename())
            return workunit

        self.workunit_provider.get_workunit.side_effect = (get_workunit(index) for index in xrange(2))
        image_manager = Mock(spec=ImageManager)

        self.undertest = ValidationModel(self.workunit_provider, image_manager, None)
        self.undertest.start_work()

    def test_workunits_on_demand(self):
        assert_that(self.undertest.get_current_workunit(), equal_to(self.workunit1))
        assert_that(self.workunit_provider.get_workunit.call_count, equal_to(1))

        self.undertest.next_workunit()
        assert_that(self.undertest.get_current_workunit(), equal_to(self.workunit2))
        assert_that(self.workunit_provider.get_workunit.call_count, equal_to(2))

    def test_shift_locks(self):
        assert_that(self.undertest.get_current_workunit(), equal_to(self.workunit1))
        assert_that(self.progress_manager.owns_lock(self.file1), equal_to(True))
        assert_that(self.progress_manager.owns_lock(self.file2), equal_to(False))

        self.undertest.next_workunit()
        assert_that(self.undertest.get_current_workunit(), equal_to(self.workunit2))

        # Note we don't give up lock just by going to next workunit.  It is
        # released once it is done processing.
        assert_that(self.progress_manager.owns_lock(self.file1), equal_to(True))
        assert_that(self.progress_manager.owns_lock(self.file2), equal_to(True))
Beispiel #5
0
class ProcessRealsControllerTest(WxWidgetTestCase, FileReadingTestCase, DirectoryCleaningTestCase):
    def setUp(self):
        WxWidgetTestCase.setUp(self)

        parser = AstromParser()
        context = LocalDirectoryWorkingContext(
            self.get_abs_path("data/controller_testdir"))
        progress_manager = LocalProgressManager(context)
        workunit_provider = WorkUnitProvider(tasks.get_suffix(tasks.REALS_TASK),
                                             context, progress_manager,
                                             RealsWorkUnitBuilder(
                                                 parser,
                                                 context,
                                                 context,
                                                 progress_manager))

        image_manager = Mock(spec=ImageManager)

        self.model = ValidationModel(workunit_provider, image_manager, None)
        self.model.start_work()

        self.model.get_writer = Mock(return_value=Mock(spec=mpc.MPCWriter))

        # We don't actually have any images loaded, so mock this out
        source_cutout = Mock(spec=SourceCutout)
        source_cutout.pixel_x = 11
        source_cutout.pixel_y = 50
        self.model.get_current_cutout = Mock(return_value=source_cutout)

        x_cen = 10
        y_cen = 50
        mag = 24.0
        self.model.get_current_source_observed_magnitude = Mock(return_value=(x_cen, y_cen, mag))

        self.model.is_current_source_adjusted = Mock(return_value=False)
        self.model.get_current_fits_header = Mock()

        self.name_generator = Mock(spec=ProvisionalNameGenerator)
        self.name_generator.generate_name.return_value = TEST_PROVISIONAL_NAME

        class TestFactory(object):
            def __init__(self, model, name_generator):
                self.model = model
                self.name_generator = name_generator

            def create_controller(self, view):
                return ProcessRealsController(self.model, view,
                                              self.name_generator)

        self.view = ApplicationView(
            TestFactory(self.model, self.name_generator))
        self.controller = self.view.controller
        self.controller.display_current_image = Mock()

    def tearDown(self):
        WxWidgetTestCase.tearDown(self)
        DirectoryCleaningTestCase.tearDown(self)

    def get_directory_to_clean(self):
        return self.get_abs_path("data/controller_testdir")

    def get_files_to_keep(self):
        return ["1584431p15.measure3.reals.astrom", "1616681p10.measure3.reals.astrom"]

    @patch("ossos.gui.controllers.mpc.Observation", spec=mpc.Observation)
    def test_reject_disables_validation_controls(self, mock_Observation):
        comment = "test"

        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_do_reject(comment)

        # We have moved to the next item, so it should still be enabled
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

        # Loop back to that first item
        self.controller.on_next_obs()
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(False))

        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

    @patch("ossos.gui.controllers.mpc.Observation", spec=mpc.Observation)
    def test_reject_last_item_disables_validation_controls(self, mock_Observation):
        comment = "test"

        self.controller.on_next_obs()
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_do_reject(comment)

        # We have moved to the next item (looped back to beginning), so it should still be enabled
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

        # Move forward to that last item again
        self.controller.on_next_obs()
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(False))

        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

    def accept_source_reading(self):
        self.controller.on_do_accept(TEST_MINOR_PLANET_NUMBER,
                                     TEST_PROVISIONAL_NAME,
                                     TEST_NOTE1,
                                     TEST_NOTE2,
                                     TEST_DATE,
                                     TEST_RA,
                                     TEST_DEC,
                                     TEST_MAG,
                                     TEST_MAG_ERR,
                                     TEST_BAND,
                                     TEST_OBS_CODE,
                                     TEST_COMMENT
        )

    def reject_source_reading(self):
        self.controller.on_do_reject(TEST_COMMENT)

    def test_accept_moves_to_first_observation_of_new_workunit(self):
        workunit1 = self.model.get_current_workunit()

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(1))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(2))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(1))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(1))
        assert_that(self.model.get_current_obs_number(), equal_to(1))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(1))
        assert_that(self.model.get_current_obs_number(), equal_to(2))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(2))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(2))
        assert_that(self.model.get_current_obs_number(), equal_to(1))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(2))
        assert_that(self.model.get_current_obs_number(), equal_to(2))

        self.accept_source_reading()

        workunit2 = self.model.get_current_workunit()

        assert_that(workunit2, is_not(same_instance(workunit1)))

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

    def test_phot_error_handling(self):
        view_mock = Mock(spec=ApplicationView)
        self.controller.view = view_mock

        error_message = "Photometry failed"
        self.model.get_current_source_observed_magnitude = Mock(
            side_effect=TaskError(error_message))
        self.model.get_current_band = Mock(return_value="B")

        self.controller.on_accept()

        view_mock.show_accept_source_dialog.assert_called_once_with(
            ANY,
            ANY,
            ANY,
            ANY,
            "", # obs_mag
            -1, # obs_mag_err
            "", # band
            note1_choices=ANY,
            note2_choices=ANY,
            note1_default=ANY,
            note2_default=ANY,
            default_observatory_code=ANY,
            default_comment=error_message,
            phot_failure=True
        )
Beispiel #6
0
class ProcessRealsControllerTest(WxWidgetTestCase, FileReadingTestCase, DirectoryCleaningTestCase):
    def setUp(self):
        WxWidgetTestCase.setUp(self)

        parser = AstromParser()
        context = LocalDirectoryWorkingContext(
            self.get_abs_path("data/controller_testdir"))
        progress_manager = LocalProgressManager(context)
        workunit_provider = WorkUnitProvider(tasks.get_suffix(tasks.REALS_TASK),
                                             context, progress_manager,
                                             RealsWorkUnitBuilder(
                                                 parser,
                                                 context,
                                                 context,
                                                 progress_manager))

        image_manager = Mock(spec=ImageManager)

        self.model = ValidationModel(workunit_provider, image_manager, None)
        self.model.start_work()

        self.model.get_writer = Mock(return_value=Mock(spec=mpc.MPCWriter))

        # We don't actually have any images loaded, so mock this out
        source_cutout = Mock(spec=SourceCutout)
        source_cutout.pixel_x = 11
        source_cutout.pixel_y = 50
        self.model.get_current_cutout = Mock(return_value=source_cutout)

        x_cen = 10
        y_cen = 50
        mag = 24.0
        self.model.get_current_source_observed_magnitude = Mock(return_value=(x_cen, y_cen, mag))

        self.model.is_current_source_adjusted = Mock(return_value=False)
        self.model.get_current_fits_header = Mock()

        self.name_generator = Mock(spec=ProvisionalNameGenerator)
        self.name_generator.generate_name.return_value = TEST_PROVISIONAL_NAME

        class TestFactory(object):
            def __init__(self, model, name_generator):
                self.model = model
                self.name_generator = name_generator

            def create_controller(self, view):
                return ProcessRealsController(self.model, view,
                                              self.name_generator)

        self.view = ApplicationView(
            TestFactory(self.model, self.name_generator))
        self.controller = self.view.controller
        self.controller.display_current_image = Mock()

    def tearDown(self):
        WxWidgetTestCase.tearDown(self)
        DirectoryCleaningTestCase.tearDown(self)

    def get_directory_to_clean(self):
        return self.get_abs_path("data/controller_testdir")

    def get_files_to_keep(self):
        return ["1584431p15.measure3.reals.astrom", "1616681p10.measure3.reals.astrom"]

    @patch("daomop.gui.controllers.mpc.Observation", spec=mpc.Observation)
    def test_reject_disables_validation_controls(self, mock_Observation):
        comment = "test"

        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_do_reject(comment)

        # We have moved to the next item, so it should still be enabled
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

        # Loop back to that first item
        self.controller.on_next_obs()
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(False))

        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

    @patch("daomop.gui.controllers.mpc.Observation", spec=mpc.Observation)
    def test_reject_last_item_disables_validation_controls(self, mock_Observation):
        comment = "test"

        self.controller.on_next_obs()
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_do_reject(comment)

        # We have moved to the next item (looped back to beginning), so it should still be enabled
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

        # Move forward to that last item again
        self.controller.on_next_obs()
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(False))

        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))
        self.controller.on_next_obs()
        assert_that(self.view.is_source_validation_enabled(), equal_to(True))

    def accept_source_reading(self):
        self.controller.on_do_accept(TEST_MINOR_PLANET_NUMBER,
                                     TEST_PROVISIONAL_NAME,
                                     TEST_NOTE1,
                                     TEST_NOTE2,
                                     TEST_DATE,
                                     TEST_RA,
                                     TEST_DEC,
                                     TEST_MAG,
                                     TEST_MAG_ERR,
                                     TEST_BAND,
                                     TEST_OBS_CODE,
                                     TEST_COMMENT
        )

    def reject_source_reading(self):
        self.controller.on_do_reject(TEST_COMMENT)

    def test_accept_moves_to_first_observation_of_new_workunit(self):
        workunit1 = self.model.get_current_workunit()

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(1))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(2))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(1))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(1))
        assert_that(self.model.get_current_obs_number(), equal_to(1))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(1))
        assert_that(self.model.get_current_obs_number(), equal_to(2))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(2))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(2))
        assert_that(self.model.get_current_obs_number(), equal_to(1))

        self.accept_source_reading()

        assert_that(self.model.get_current_source_number(), equal_to(2))
        assert_that(self.model.get_current_obs_number(), equal_to(2))

        self.accept_source_reading()

        workunit2 = self.model.get_current_workunit()

        assert_that(workunit2, is_not(same_instance(workunit1)))

        assert_that(self.model.get_current_source_number(), equal_to(0))
        assert_that(self.model.get_current_obs_number(), equal_to(0))

    def test_phot_error_handling(self):
        view_mock = Mock(spec=ApplicationView)
        self.controller.view = view_mock

        error_message = "Photometry failed"
        self.model.get_current_source_observed_magnitude = Mock(
            side_effect=TaskError(error_message))
        self.model.get_current_band = Mock(return_value="B")

        self.controller.on_accept()

        view_mock.show_accept_source_dialog.assert_called_once_with(
            ANY,
            ANY,
            ANY,
            ANY,
            "", # obs_mag
            -1, # obs_mag_err
            "", # band
            note1_choices=ANY,
            note2_choices=ANY,
            note1_default=ANY,
            note2_default=ANY,
            default_observatory_code=ANY,
            default_comment=error_message,
            phot_failure=True
        )