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"))
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")
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>"))
def _mock_lcs_get_actions_for_model_run_wrong_dataset(model_run): cat = LandCoverRegionCategory() cat.id = 1 cat.name = "Countries" cat.driving_dataset_id = 2 region = LandCoverRegion() region.id = 1 region.name = "Wales" region.category = cat action = LandCoverAction() action.id = 1 action.region = region action.value_id = 1 return [action]
def add_land_cover_actions(self, land_cover_region, model_run, value_order_pairs, land_cover_service): """ Create land cover actions and save them using the model_run_service.save_land_cover_actions method :param land_cover_region: Land cover region actions should belong to :param model_run: Model run to add them against :param value_order_pairs: List of 2-tuples [(value, order)]; each tuple is a land cover action to be added :param land_cover_service: Land Cover service to use :return: """ land_cover_actions = [] for value, order in value_order_pairs: lca = LandCoverAction() lca.region_id = land_cover_region.id lca.value_id = value lca.order = order land_cover_actions.append(lca) land_cover_service.save_land_cover_actions_for_model(model_run, land_cover_actions)
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
def save_land_cover_actions(self, values, errors, model_run): """ Validate and Save requested land cover actions :param values: POST dictionary of form values :param errors: Object to add errors to :param model_run: The model run being created :return: """ self._validate_values(values, errors, model_run.driving_dataset, model_run) if len(errors) == 0: land_cover_actions = [] actions = [] for key in values: actions += re.findall("^action_(\d+)", key) for index in set(actions): lca = LandCoverAction() lca.region_id = int(values['action_%s_region' % index]) lca.value_id = int(values['action_%s_value' % index]) lca.order = int(values['action_%s_order' % index]) land_cover_actions.append(lca) self.land_cover_service.save_land_cover_actions_for_model(model_run, land_cover_actions)
def test_GIVEN_model_with_land_cover_actions_WHEN_convert_to_dictionary_THEN_land_cover_actions_present(self): job_runner_client = JobRunnerClient(config) def _alter_yearly_monthly_output_profiles(params): return params def _alter_driving_data_start(params): return params job_runner_client.alter_yearly_monthly_output_profiles = _alter_yearly_monthly_output_profiles job_runner_client.alter_driving_data_start = _alter_driving_data_start parameter = Parameter(name='file') expected_parameter_value = "'base_frac_file.nc'" parameter.parameter_values = [ParameterValue(value=expected_parameter_value)] parameter2 = Parameter(name='frac_name') expected_parameter_value2 = "'frac'" parameter2.parameter_values = [ParameterValue(value=expected_parameter_value2)] parameter3 = Parameter(name='latlon_region') expected_parameter_value3 = ".true." parameter3.parameter_values = [ParameterValue(value=expected_parameter_value3)] namelist = Namelist(name='JULES_FRAC') namelist_file = NamelistFile(filename='filename') expected_index = 3 namelist.parameters = [parameter, parameter2] namelist.namelist_file = namelist_file namelist.index_in_file = expected_index namelist2 = Namelist(name='JULES_MODEL_GRID') namelist_file = NamelistFile(filename='filename') expected_index = 3 namelist2.parameters = [parameter3] namelist2.namelist_file = namelist_file namelist2.index_in_file = expected_index model_run = ModelRun() model_run.id = 101 code_version = CodeVersion(name='Jules v3.4.1') model_run.code_version = code_version code_version.parameters = [parameter, parameter2, parameter3] user = User() user.id = 1 user.email = "email" user.username = "******" model_run.user = user lcr1 = LandCoverRegion() lcr1.mask_file = "region1.nc" lca1 = LandCoverAction() lca1.value_id = 9 lca1.order = 1 lca1.region = lcr1 lcr2 = LandCoverRegion() lcr2.mask_file = "region2.nc" lca2 = LandCoverAction() lca2.value_id = 5 lca2.order = 2 lca2.region = lcr2 land_cover_actions = [lca1, lca2] result = job_runner_client.convert_model_to_dictionary(model_run, code_version.parameters, land_cover_actions) result_lc = result[constants.JSON_LAND_COVER] assert_that(result_lc[constants.JSON_LAND_COVER_BASE_FILE], is_("base_frac_file.nc")) assert_that(result_lc[constants.JSON_LAND_COVER_BASE_KEY], is_("frac")) assert_that(result_lc[constants.JSON_LAND_COVER_ICE_INDEX], is_(9)) result_lc_actions = result_lc[constants.JSON_LAND_COVER_ACTIONS] assert_that(len(result_lc_actions), is_(2)) action1 = result_lc_actions[0] assert_that(action1[constants.JSON_LAND_COVER_MASK_FILE], is_("region1.nc")) assert_that(action1[constants.JSON_LAND_COVER_ORDER], is_(1)) assert_that(action1[constants.JSON_LAND_COVER_VALUE], is_(9)) action2 = result_lc_actions[1] assert_that(action2[constants.JSON_LAND_COVER_MASK_FILE], is_("region2.nc")) assert_that(action2[constants.JSON_LAND_COVER_ORDER], is_(2)) assert_that(action2[constants.JSON_LAND_COVER_VALUE], is_(5)) namelist_file_result = result[constants.JSON_MODEL_NAMELIST_FILES][0] parameters_result = namelist_file_result[constants.JSON_MODEL_NAMELISTS][0][constants.JSON_MODEL_PARAMETERS] assert_that(parameters_result['file'], is_("'" + constants.USER_EDITED_FRACTIONAL_FILENAME + "'")) assert_that(parameters_result['frac_name'], is_("'frac'"))
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)