def test_GIVEN_valid_output_and_model_run_WHEN_get_output_variable_name_THEN_output_variable_returned_(self): with session_scope() as session: param_val_output = ParameterValue() param_val_output.set_value_from_python('gpp_gb') parameter_service = ParameterService() param_output = parameter_service.get_parameter_by_constant(constants.JULES_PARAM_OUTPUT_VAR, session) param_output.parameter_values = [param_val_output] model_run = ModelRun() model_run.parameter_values = [param_val_output] session.add(model_run) session.commit() var = self.parameter_service.get_output_variable_name(model_run.id, param_val_output.id) assert_that(var, is_('gpp_gb'))
def setUp(self): dap_client_factory = DapClientFactory() dap_client_factory.get_land_cover_dap_client = self._mock_get_land_cover_dap_client dap_client_factory.get_soil_properties_dap_client = self._mock_get_soil_properties_dap_client self.land_cover_service = LandCoverService(dap_client_factory=dap_client_factory) self.model_run_service = ModelRunService() self.dataset_service = DatasetService() self.parameter_service = ParameterService() self.clean_database() self.user = self.login() self.create_model_run_ready_for_submit()
def setUp(self): self.parameter_service = ParameterService() super(TestModelRunSummaryController, self).setUp()
class TestModelRunSummaryController(TestWithFullModelRun): def setUp(self): self.parameter_service = ParameterService() super(TestModelRunSummaryController, self).setUp() def test_GIVEN_no_defining_model_WHEN_navigate_to_summary_THEN_redirect_to_create_model_run(self): response = self.app.get( url(controller='model_run', action='submit')) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='create')), "url") def test_GIVEN_select_previous_WHEN_post_THEN_redirect_to_output_page(self): self.model_run_service.update_model_run(self.user, "test", constants.DEFAULT_SCIENCE_CONFIGURATION) self.model_run_service.store_parameter_values({'1': 12}, self.user) response = self.app.post( url=url(controller='model_run', action='submit'), params={ 'submit': u'Previous' } ) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='output')), "url") def test_GIVEN_select_previous_and_user_over_quota_WHEN_post_THEN_redirect_to_index_page(self): self.create_run_model(storage_in_mb=self.user.storage_quota_in_gb * 1024 + 1, name="big_run", user=self.user) self.model_run_service.update_model_run(self.user, "test", constants.DEFAULT_SCIENCE_CONFIGURATION) self.model_run_service.store_parameter_values({'1': 12}, self.user) response = self.app.post( url=url(controller='model_run', action='submit'), params={ 'submit': u'Previous' } ) assert_that(response.status_code, is_(302), "Respose is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='index')), "url") def test_GIVEN_select_submit_WHEN_post_THEN_redirect_to_index_page_job_submitted(self): self.model_run_service.update_model_run(self.user, "test", constants.DEFAULT_SCIENCE_CONFIGURATION) self.model_run_service.store_parameter_values({'1': 12}, self.user) self.parameter_service.save_new_parameters([[constants.JULES_PARAM_LATLON_REGION, True], [constants.JULES_PARAM_DRIVE_DATA_START, dt.datetime(1901, 1, 1)], [constants.JULES_PARAM_SPINUP_START, dt.datetime(1905, 1, 1)]], [], self.user.id) response = self.app.post( url=url(controller='model_run', action='submit'), params={ 'submit': u'Submit' } ) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='index')), "url") session = Session() row = session.query("name").from_statement( """ SELECT s.name FROM model_runs m JOIN model_run_statuses s on s.id = m.status_id WHERE m.user_id = :userid """) \ .params({'userid': self.user.id}) \ .one() assert_that(row.name, is_(constants.MODEL_RUN_STATUS_SUBMIT_FAILED), "model run status") def test_GIVEN_select_submit_and_user_over_quota_WHEN_post_THEN_redirect_to_index_page_job_not_submitted(self): big_model_run = self.create_run_model(storage_in_mb=self.user.storage_quota_in_gb * 1024 + 1, name="big_run", user=self.user) self.model_run_service.update_model_run(self.user, "test", constants.DEFAULT_SCIENCE_CONFIGURATION) self.model_run_service.store_parameter_values({'1': 12}, self.user) response = self.app.post( url=url(controller='model_run', action='submit'), params={ 'submit': u'Submit' } ) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='index')), "url") session = Session() row = session.query("name").from_statement( """ SELECT s.name FROM model_runs m JOIN model_run_statuses s on s.id = m.status_id WHERE m.user_id = :userid AND m.id != :model_run_id """) \ .params({'userid': self.user.id, 'model_run_id': big_model_run.id}) \ .one() assert_that(row.name, is_(constants.MODEL_RUN_STATUS_CREATED), "model run status") def test_GIVEN_model_run_and_parameters_WHEN_view_submit_THEN_page_is_shown_and_contains_model_run_summary(self): self.create_model_run_ready_for_submit() response = self.app.get( url(controller='model_run', action='submit')) assert_that(response.normal_body, contains_string("<h1>Submit Model Run</h1>")) assert_that(response.normal_body, contains_string(str(self.model_name))) assert_that(response.normal_body, contains_string(str(self.model_description))) assert_that(response.normal_body, contains_string(str(self.science_config['name']))) assert_that(response.normal_body, contains_string(str(self.driving_data.name))) assert_that(response.normal_body, contains_string(str(self.lat_n))) assert_that(response.normal_body, contains_string(str(self.lat_s))) assert_that(response.normal_body, contains_string(str(self.lon_w))) assert_that(response.normal_body, contains_string(str(self.lon_e))) assert_that(response.normal_body, contains_string(str(self.date_start.strftime("%Y-%m-%d")))) assert_that(response.normal_body, contains_string(str(self.date_end.strftime("%Y-%m-%d")))) assert_that(response.normal_body, contains_string("No land cover edits made")) output_variable_1 = self.model_run_service.get_output_variable_by_id(1) output_variable_10 = self.model_run_service.get_output_variable_by_id(10) assert_that(response.normal_body, contains_string(str(output_variable_1.description))) assert_that(response.normal_body, contains_string(str(output_variable_10.description))) self.assert_model_run_creation_action(self.user, 'submit') 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 test_GIVEN_alternate_workflow_branch_followed_WHEN_reach_submit_THEN_parameter_values_the_same(self): # We create a model run, then simulate going back to the first page and recreating it with different options # Finally we again go back to the first page and recreate the original model run to check the end result is # the same both paths. self.create_model_run_ready_for_submit() model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) param_values_start = model_run.parameter_values self.create_alternate_model_run() self.create_model_run_ready_for_submit() model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) param_values_end = model_run.parameter_values # Sort by param_id param_values_start.sort(key=lambda pv: pv.parameter_id) param_values_end.sort(key=lambda pv: pv.parameter_id) # The two lists should match up assert_that(len(param_values_start), is_(len(param_values_end))) for i in range(len(param_values_start)): assert_that(param_values_start[i].parameter_id, is_(param_values_end[i].parameter_id)) assert_that(param_values_start[i].value, is_(param_values_end[i].value)) assert_that(param_values_start[i].group_id, is_(param_values_end[i].group_id)) def test_GIVEN_workflow_branch_with_user_driving_data_followed_WHEN_reach_submit_THEN_parameter_values_the_same(self): # We create a model run, then simulate going back to the first page and recreating it with different options # Finally we again go back to the first page and recreate the original model run to check the end result is # the same both paths. self.create_model_run_ready_for_submit() model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) param_values_start = model_run.parameter_values self.create_model_run_with_user_uploaded_driving_data() self.create_model_run_ready_for_submit() model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) param_values_end = model_run.parameter_values # Sort by param_id param_values_start.sort(key=lambda pv: pv.parameter_id) param_values_end.sort(key=lambda pv: pv.parameter_id) # The two lists should match up assert_that(len(param_values_start), is_(len(param_values_end))) for i in range(len(param_values_start)): assert_that(param_values_start[i].parameter_id, is_(param_values_end[i].parameter_id)) assert_that(param_values_start[i].value, is_(param_values_end[i].value)) assert_that(param_values_start[i].group_id, is_(param_values_end[i].group_id))
def setUp(self): super(TestParameterService, self).setUp() self.parameter_service = ParameterService() self.clean_database()
class TestLandCoverService(TestWithFullModelRun): def setUp(self): dap_client_factory = DapClientFactory() dap_client_factory.get_land_cover_dap_client = self._mock_get_land_cover_dap_client dap_client_factory.get_soil_properties_dap_client = self._mock_get_soil_properties_dap_client self.land_cover_service = LandCoverService(dap_client_factory=dap_client_factory) self.model_run_service = ModelRunService() self.dataset_service = DatasetService() self.parameter_service = ParameterService() self.clean_database() self.user = self.login() self.create_model_run_ready_for_submit() @staticmethod def _mock_get_land_cover_dap_client(url, key): return MockLandCoverDapClient(url, key) @staticmethod def _mock_get_soil_properties_dap_client(url): return MockSoilPropertiesDapClient(url) def test_GIVEN_land_cover_regions_exist_WHEN_get_land_cover_region_by_id_THEN_land_cover_region_returned(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) id = land_cover_region.id returned_region = self.land_cover_service.get_land_cover_region_by_id(id) assert_that(returned_region.id, is_(id)) assert_that(returned_region.name, is_("Wales")) def test_GIVEN_land_cover_categories_WHEN_get_land_cover_region_THEN_returned_region_has_category_loaded(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) id = land_cover_region.id with session_scope() as session: land_cover_cat = LandCoverRegionCategory() land_cover_cat.name = "River Catchments" land_cover_cat.driving_dataset_id = model_run.driving_dataset_id session.add(land_cover_cat) returned_region = self.land_cover_service.get_land_cover_region_by_id(id) assert_that(returned_region.category.name, is_("Countries")) def test_GIVEN_land_cover_values_WHEN_get_land_cover_values_THEN_land_cover_values_returned(self): lc_values = self.land_cover_service.get_land_cover_values(None) assert_that(len(lc_values), is_(9)) names = [lc_value.name for lc_value in lc_values] assert 'Urban' in names assert constants.FRACTIONAL_ICE_NAME in names def test_GIVEN_return_no_ice_WHEN_get_land_cover_values_THEN_land_cover_values_returned(self): lc_values = self.land_cover_service.get_land_cover_values(None, return_ice=False) assert_that(len(lc_values), is_(8)) names = [lc_value.name for lc_value in lc_values] assert 'Urban' in names assert constants.FRACTIONAL_ICE_NAME not in names def test_GIVEN_multiple_land_cover_categories_WHEN_get_categories_THEN_correct_categories_returned(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) datasets = self.dataset_service.get_driving_datasets(self.user) with session_scope() as session: cat1 = LandCoverRegionCategory() cat1.name = "Countries" cat1.driving_dataset_id = model_run.driving_dataset_id region1 = LandCoverRegion() region1.mask_file = "filepath" region1.name = "Wales" region1.category = cat1 cat2 = LandCoverRegionCategory() cat2.name = "Rivers" cat2.driving_dataset_id = datasets[1].id region2 = LandCoverRegion() region2.mask_file = "filepath2" region2.name = "Thames" region2.category = cat2 session.add_all([region1, region2]) categories = self.land_cover_service.get_land_cover_categories(model_run.driving_dataset_id) assert_that(len(categories), is_(1)) assert_that(categories[0].name, is_("Countries")) def test_GIVEN_categories_have_land_cover_regions_WHEN_get_categories_THEN_category_has_regions_loaded(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) with session_scope() as session: cat1 = LandCoverRegionCategory() cat1.name = "Countries" cat1.driving_dataset_id = model_run.driving_dataset_id region1 = LandCoverRegion() region1.mask_file = "filepath" region1.name = "Wales" region1.category = cat1 cat2 = LandCoverRegionCategory() cat2.name = "Rivers" cat2.driving_dataset_id = model_run.driving_dataset_id region2 = LandCoverRegion() region2.mask_file = "filepath2" region2.name = "Thames" region2.category = cat2 session.add_all([region1, region2]) categories = self.land_cover_service.get_land_cover_categories(model_run.driving_dataset_id) assert_that(len(categories[0].regions), is_(1)) assert_that(categories[0].regions[0].name, is_("Wales")) def test_GIVEN_land_cover_actions_WHEN_save_land_cover_actions_THEN_land_cover_actions_saved(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) self.add_land_cover_actions(land_cover_region, model_run, [(1, 1), (2, 3)], self.land_cover_service) with session_scope() as session: model_run = self.model_run_service._get_model_run_being_created(session, self.user) actions = model_run.land_cover_actions assert_that(len(actions), is_(2)) def test_GIVEN_existing_land_cover_actions_WHEN_save_land_cover_actions_THEN_land_cover_actions_overwritten(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) self.add_land_cover_actions(land_cover_region, model_run, [(1, 1), (2, 3)], self.land_cover_service) self.add_land_cover_actions(land_cover_region, model_run, [(2, 4)], self.land_cover_service) with session_scope() as session: model_run = self.model_run_service._get_model_run_being_created(session, self.user) actions = model_run.land_cover_actions assert_that(len(actions), is_(1)) assert_that(actions[0].value_id, is_(2)) assert_that(actions[0].order, is_(4)) def test_GIVEN_no_land_cover_actions_WHEN_save_land_cover_actions_THEN_all_land_cover_actions_removed(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) self.add_land_cover_actions(land_cover_region, model_run, [(1, 1), (2, 3)], self.land_cover_service) self.add_land_cover_actions(land_cover_region, model_run, [], self.land_cover_service) with session_scope() as session: model_run = self.model_run_service._get_model_run_being_created(session, self.user) actions = model_run.land_cover_actions assert_that(len(actions), is_(0)) def test_GIVEN_land_cover_actions_saved_on_model_run_WHEN_get_land_cover_actions_THEN_actions_returned(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) self.add_land_cover_actions(land_cover_region, model_run, [(1, 1), (2, 3)], self.land_cover_service) actions = self.land_cover_service.get_land_cover_actions_for_model(model_run) assert_that(len(actions), is_(2)) assert_that(actions[0].value_id, is_(1)) assert_that(actions[0].order, is_(1)) assert_that(actions[1].value_id, is_(2)) assert_that(actions[1].order, is_(3)) def test_GIVEN_land_cover_actions_saved_WHEN_get_actions_THEN_actions_have_regions_and_categories_loaded(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) self.add_land_cover_actions(land_cover_region, model_run, [(1, 1)], self.land_cover_service) action = self.land_cover_service.get_land_cover_actions_for_model(model_run)[0] assert_that(action.region.name, is_("Wales")) assert_that(action.region.category.name, is_("Countries")) def test_GIVEN_land_cover_actions_saved_WHEN_get_actions_THEN_actions_have_values_loaded(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) land_cover_region = self.add_land_cover_region(model_run) self.add_land_cover_actions(land_cover_region, model_run, [(1, 1)], self.land_cover_service) action = self.land_cover_service.get_land_cover_actions_for_model(model_run)[0] assert_that(action.value.name, is_("Broad-leaved Tree")) def test_GIVEN_fractional_string_WHEN_save_fractional_land_cover_for_model_THEN_fractional_cover_saved(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) assert_that(model_run.land_cover_frac, is_(None)) fractional_string = "0\t0\t0\t0\t0\t0\t0\t0\t1" self.land_cover_service.save_fractional_land_cover_for_model(model_run, fractional_string) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) assert_that(model_run.land_cover_frac, is_(fractional_string)) def test_GIVEN_user_uploaded_driving_data_WHEN_get_default_fractional_cover_THEN_fractional_cover_returned(self): self.clean_database() self.user = self.login() self.create_model_run_with_user_uploaded_driving_data() model_run = self.set_model_run_latlon(self.user, 70, 0) fractional_vals = self.land_cover_service.get_default_fractional_cover(model_run, self.user) assert_that(fractional_vals, is_([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0])) def test_GIVEN_two_matching_datasets_WHEN_get_default_cover_THEN_higher_priority_fractional_cover_returned(self): self.clean_database() self.user = self.login() self.create_model_run_with_user_uploaded_driving_data() model_run = self.set_model_run_latlon(self.user, 0, 0) fractional_vals = self.land_cover_service.get_default_fractional_cover(model_run, self.user) assert_that(fractional_vals, is_([0.02, 0.11, 0.02, 0.05, 0.35, 0.19, 0.22, 0.04, 0.0])) def test_GIVEN_no_matching_datasets_WHEN_get_default_cover_THEN_zeros_returned(self): self.clean_database() self.user = self.login() self.create_model_run_with_user_uploaded_driving_data() model_run = self.set_model_run_latlon(self.user, 88, 0) fractional_vals = self.land_cover_service.get_default_fractional_cover(model_run, self.user) assert_that(fractional_vals, is_([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])) def test_GIVEN_thredds_not_working_WHEN_get_default_cover_THEN_zeros_returned(self): self.clean_database() self.user = self.login() self.create_model_run_with_user_uploaded_driving_data() def _get_broken_dap_client(url, key): return MockLandCoverDapClient("broken_url", key) dap_client_factory = DapClientFactory() dap_client_factory.get_land_cover_dap_client = _get_broken_dap_client self.land_cover_service = LandCoverService(dap_client_factory=dap_client_factory) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) fractional_vals = self.land_cover_service.get_default_fractional_cover(model_run, self.user) assert_that(fractional_vals, is_([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])) def test_GIVEN_using_provided_driving_data_WHEN_get_default_fractional_cover_THEN_fractional_cover_returned(self): self.clean_database() self.user = self.login() self.create_alternate_model_run() model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) fractional_vals = self.land_cover_service.get_default_fractional_cover(model_run, self.user) assert_that(fractional_vals, is_([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0])) def test_GIVEN_multicell_model_run_WHEN_get_default_fractional_cover_THEN_ServiceException_raised(self): self.clean_database() self.user = self.login() 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 self.assertRaises(ServiceException): self.land_cover_service.get_default_fractional_cover(model_run, self.user) def test_GIVEN_user_uploaded_driving_data_WHEN_set_default_soil_properties_THEN_soil_cover_set(self): self.clean_database() self.user = self.login() self.create_model_run_with_user_uploaded_driving_data() model_run = self.set_model_run_latlon(self.user, 51, 0) self.land_cover_service.save_default_soil_properties(model_run, self.user) #self._land_cover_service.dap_client_factory.get_soil_properties_dap_client = _mock_get_soil_props_client model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) nvars = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_NVARS) var = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_VAR, is_list=True) use_file = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_USE_FILE, is_list=True) const_val = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_CONST_VAL, is_list=True) assert_that(nvars, is_(9)) assert_that(var, is_(['b', 'sathh', 'satcon', 'sm_sat', 'sm_crit', 'sm_wilt', 'hcap', 'hcon', 'albsoil'])) assert_that(use_file, is_(9 * [False])) assert_that(const_val, is_([0.9, 0.0, 0.0, 50.0, 275.0, 278.0, 10.0, 0.0, 0.5])) def test_GIVEN_no_appropriate_driving_data_WHEN_set_default_soil_properties_THEN_values_not_set(self): self.clean_database() self.user = self.login() self.create_model_run_with_user_uploaded_driving_data() model_run = self.set_model_run_latlon(self.user, 90, 0) self.land_cover_service.save_default_soil_properties(model_run, self.user) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) nvars = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_NVARS) var = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_VAR) use_file = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_USE_FILE, is_list=True) const_val = model_run.get_python_parameter_value(constants.JULES_PARAM_SOIL_PROPS_CONST_VAL, is_list=True) assert_that(nvars, is_(None)) assert_that(var, is_(None)) assert_that(use_file, is_(None)) assert_that(const_val, is_(None)) def set_model_run_latlon(self, user, lat, lon): params_to_save = [[constants.JULES_PARAM_POINTS_FILE, [lat, lon]]] params_to_del = constants.JULES_PARAM_POINTS_FILE self.parameter_service.save_new_parameters(params_to_save, params_to_del, user.id) return self.model_run_service.get_model_being_created_with_non_default_parameter_values(user)