コード例 #1
0
    def test_GIVEN_multiple_land_cover_actions_saved_out_of_order_WHEN_get_THEN_order_rendered_correctly(self):
        with session_scope() as session:
            model_run = self.model_run_service._get_model_run_being_created(session, self.user)
            dds = model_run.driving_dataset
            session.add_all(self.generate_categories_with_regions(dds))

            action = LandCoverAction()
            action.model_run = model_run
            action.region_id = 1  # Thames
            action.value_id = 5  # Shrub
            action.order = 5
            session.add(action)
            session.commit()

            action2 = LandCoverAction()
            action2.model_run = model_run
            action2.region_id = 2  # Itchen
            action2.value_id = 1  # Broad-leaved Tree
            action2.order = 1
            session.add(action2)
            session.commit()

            action3 = LandCoverAction()
            action3.model_run = model_run
            action3.region_id = 3  # Hampshire
            action3.value_id = 6  # Urban
            action3.order = 2
            session.add(action3)
            session.commit()

        response = self.app.get(url(controller='model_run', action='land_cover'))
        order1 = response.normal_body.index("Change <b>Itchen (Rivers)</b> to <b>Broad-leaved Tree</b>")
        order2 = response.normal_body.index("Change <b>Hampshire (Counties)</b> to <b>Urban</b>")
        order5 = response.normal_body.index("Change <b>Thames (Rivers)</b> to <b>Shrub</b>")
        assert (order1 < order2 < order5)
コード例 #2
0
    def test_GIVEN_land_cover_action_saved_WHEN_view_submit_page_THEN_land_cover_action_shown(self):
        self.create_model_run_ready_for_submit()
        model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user)

        with session_scope() as session:
            land_cover_cat = LandCoverRegionCategory()
            land_cover_cat.driving_dataset_id = model_run.driving_dataset_id
            land_cover_cat.name = "Rivers"

            land_cover_region = LandCoverRegion()
            land_cover_region.name = "Thames"
            land_cover_region.category = land_cover_cat

            land_cover_action = LandCoverAction()
            land_cover_action.model_run = model_run
            land_cover_action.value_id = 9
            land_cover_action.region = land_cover_region
            land_cover_action.order = 1

            session.add(land_cover_action)

        response = self.app.get(
            url(controller='model_run', action='submit'))

        assert_that(response.normal_body, contains_string("Change Thames (Rivers) to Ice"))
コード例 #3
0
    def duplicate_run_model(self, model_id, user):
        """
        Duplicate the run model and all its parameters to a new run model
        :param model_id: model run id to duplicate
        :param user: the user duplicating the model
        :return: nothing
        """

        id_for_user_upload_driving_dataset = self._dataset_service.get_id_for_user_upload_driving_dataset()
        self.delete_model_run_being_created(user)

        with self.transaction_scope() as session:
            model_run_to_duplicate = session\
                .query(ModelRun)\
                .join(ModelRunStatus)\
                .outerjoin(ParameterValue)\
                .outerjoin(LandCoverAction) \
                .filter(ModelRun.id == model_id) \
                .filter(or_(
                    ModelRun.user_id == user.id,
                    ModelRunStatus.name == constants.MODEL_RUN_STATUS_PUBLISHED,
                    ModelRunStatus.name == constants.MODEL_RUN_STATUS_PUBLIC,)) \
                .options(contains_eager(ModelRun.parameter_values)) \
                .options(contains_eager(ModelRun.land_cover_actions)) \
                .one()

            new_model_run_name = model_run_to_duplicate.name
            if self._is_duplicate_name(model_run_to_duplicate.name, session, user):
                new_model_run_name = "{} (Copy)".format(model_run_to_duplicate.name)
                copy_id = 1
                while self._is_duplicate_name(new_model_run_name, session, user):
                    copy_id += 1
                    new_model_run_name = "{} (Copy {})".format(model_run_to_duplicate.name, copy_id)

            new_model_run = ModelRun()

            new_model_run.duplicate_from(model_run_to_duplicate)
            new_model_run.name = new_model_run_name
            new_model_run.user = user
            new_model_run.change_status(session, constants.MODEL_RUN_STATUS_CREATED)
            for parameter_value in model_run_to_duplicate.parameter_values:
                new_parameter = ParameterValue()
                new_parameter.duplicate_from(parameter_value)
                new_parameter.model_run = new_model_run

            for land_cover_action in model_run_to_duplicate.land_cover_actions:
                new_land_cover_action = LandCoverAction()
                new_land_cover_action.duplicate_from(land_cover_action)
                new_land_cover_action.model_run = new_model_run

            session.add(new_model_run)

        if model_run_to_duplicate.driving_dataset_id == id_for_user_upload_driving_dataset:
            try:
                self._job_runner_client.duplicate_uploaded_driving_data(model_run_to_duplicate.id, new_model_run.id)
            except ServiceException:
                self.delete_model_run_being_created(user)
                raise ServiceException("Could not duplicate the model run because "
                                       "the user uploaded data can not be duplicated")
コード例 #4
0
    def test_GIVEN_land_cover_action_already_saved_WHEN_get_THEN_action_rendered(self):
        with session_scope() as session:
            model_run = self.model_run_service._get_model_run_being_created(session, self.user)
            dds = model_run.driving_dataset
            session.add_all(self.generate_categories_with_regions(dds))

            action = LandCoverAction()
            action.model_run = model_run
            action.region_id = 1  # Thames
            action.value_id = 5  # Shrub

        response = self.app.get(url(controller='model_run', action='land_cover'))
        assert_that(response.normal_body, contains_string("Change <b>Thames (Rivers)</b> to <b>Shrub</b>"))
コード例 #5
0
    def test_GIVEN_invalid_land_cover_actions_already_saved_WHEN_get_THEN_errors_returned_no_actions_rendered(self):
        with session_scope() as session:
            model_run = self.model_run_service._get_model_run_being_created(session, self.user)
            dds = model_run.driving_dataset
            session.add_all(self.generate_categories_with_regions(dds))

            action = LandCoverAction()
            action.model_run = model_run
            action.region_id = 1  # Thames
            action.value_id = 9  # Ice
            session.add(action)
            session.commit()

            # Set the model run to have a different driving dataset - this should result in an error
            model_run.driving_dataset = session.query(DrivingDataset).filter(DrivingDataset.name == "driving2").one()

        response = self.app.get(url(controller='model_run', action='land_cover'))
        assert_that(response.normal_body, contains_string("Your saved Land Cover edits are not valid for the "
                                                          "chosen driving data"))
        assert_that(response.normal_body, is_not(contains_string("Change <b>")))  # Actions start with this