class ModelRunServiceTest(TestWithFullModelRun): def setUp(self): super(ModelRunServiceTest, self).setUp() self.job_runner_client = JobRunnerClient(config) self.model_run_service = ModelRunService(job_runner_client=self.job_runner_client) self.clean_database() def test_GIVEN_no_model_runs_WHEN_get_model_runs_for_user_THEN_empty_list(self): # Add a user who doesn't have any model runs with session_scope(Session) as session: user = User() user.name = 'Has No Model Runs' session.add(user) # Get the users model runs model_runs = self.model_run_service.get_models_for_user(user) assert_that(model_runs, is_([])) def test_GIVEN_non_existent_user_WHEN_get_model_runs_for_user_THEN_returns_empty_list(self): user = User() user.id = -100 user.name = "Doesn't Exist" model_runs = self.model_run_service.get_models_for_user(user) assert_that(model_runs, is_([])) def test_GIVEN_two_users_with_one_model_each_WHEN_get_model_runs_for_user_THEN_returns_correct_model(self): # Add two users and give them each one model run with session_scope(Session) as session: # First add two users user1 = User() user1.name = 'user1' user2 = User() user2.name = 'user2' session.add_all([user1, user2]) session.commit() # Give them each a model model_run1 = ModelRun() model_run1.name = "MR1" model_run1.user_id = user1.id model_run2 = ModelRun() model_run2.name = "MR2" model_run2.user_id = user2.id session.add_all([model_run1, model_run2]) # Get the users model runs model_runs = self.model_run_service.get_models_for_user(user1) assert_that(len(model_runs), is_(1)) assert_that(model_runs[0].name, is_("MR1")) def test_GIVEN_user_has_published_model_run_WHEN_get_model_runs_for_user_THEN_published_model_is_returned(self): # Add one user and give them a published and unpublished model run with session_scope(Session) as session: # First add user user1 = User() user1.name = 'user1' session.add(user1) session.commit() # Give a model model_run1 = ModelRun() model_run1.name = "MR1" model_run1.user_id = user1.id model_run1.status = self._status(constants.MODEL_RUN_STATUS_CREATED) model_run2 = ModelRun() model_run2.name = "MR2" model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED) model_run2.user_id = user1.id model_run2.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED) session.add_all([model_run1, model_run2]) # Get the users model runs model_runs = self.model_run_service.get_models_for_user(user1) assert_that(len(model_runs), is_(2)) def test_GIVEN_user_has_public_model_run_WHEN_get_model_runs_for_user_THEN_public_model_is_returned(self): # Add one user and give them a public and unpublished model run with session_scope(Session) as session: # First add user user1 = User() user1.name = 'user1' session.add(user1) session.commit() # Give a model model_run1 = ModelRun() model_run1.name = "MR1" model_run1.user_id = user1.id model_run1.status = self._status(constants.MODEL_RUN_STATUS_CREATED) model_run2 = ModelRun() model_run2.name = "MR2" model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLIC) model_run2.user_id = user1.id session.add_all([model_run1, model_run2]) # Get the users model runs model_runs = self.model_run_service.get_models_for_user(user1) assert_that(len(model_runs), is_(2)) def test_GIVEN_user_has_published_model_run_WHEN_get_published_model_runs_THEN_only_published_model_run_returned(self): # Add one user and give them a published and unpublished model run with session_scope(Session) as session: # First add user user1 = User() user1.name = 'user1' session.add(user1) session.commit() # Give them each a model model_run1 = ModelRun() model_run1.name = "MR1" model_run1.user_id = user1.id model_run2 = ModelRun() model_run2.name = "MR2" model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED) model_run2.user_id = user1.id session.add_all([model_run1, model_run2]) # Get the published model runs model_runs = self.model_run_service.get_published_models() assert_that(len(model_runs), is_(1)) assert_that(model_runs[0].name, is_("MR2")) def test_GIVEN_user_has_public_model_run_WHEN_get_published_model_runs_THEN_only_public_model_run_returned(self): # Add one user and give them a published and unpublished model run with session_scope(Session) as session: # First add user user1 = User() user1.name = 'user1' session.add(user1) session.commit() # Give them each a model model_run1 = ModelRun() model_run1.name = "MR1" model_run1.user_id = user1.id model_run2 = ModelRun() model_run2.name = "MR2" model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLIC) model_run2.user_id = user1.id session.add_all([model_run1, model_run2]) # Get the published model runs model_runs = self.model_run_service.get_published_models() assert_that(len(model_runs), is_(1)) assert_that(model_runs[0].name, is_("MR2")) def test_GIVEN_no_published_or_public_runs_WHEN_get_published_model_runs_THEN_empty_list(self): model_runs = self.model_run_service.get_published_models() assert_that(model_runs, is_([])) def test_WHEN_get_user_model_runs_THEN_list_ordered_by_date_created_newest_first(self): with session_scope(Session) as session: # First add user user1 = User() user1.name = 'user1' session.add(user1) session.commit() # Create three published models not in datetime order model_run1 = ModelRun() model_run1.user_id = user1.id model_run1.name = "MR1" model_run1.date_created = datetime(2013, 12, 7, 17, 15, 30) session.add(model_run1) session.commit() model_run2 = ModelRun() model_run2.user_id = user1.id model_run2.name = "MR2" model_run2.date_created = datetime(2014, 6, 9, 12, 30, 24) session.add(model_run2) session.commit() model_run3 = ModelRun() model_run3.user_id = user1.id model_run3.name = "MR3" model_run3.date_created = datetime(2014, 6, 9, 11, 39, 30) session.add_all([model_run1, model_run2, model_run3]) model_runs = self.model_run_service.get_models_for_user(user1) assert_that(model_runs[0].name, is_("MR2")) assert_that(model_runs[1].name, is_("MR3")) assert_that(model_runs[2].name, is_("MR1")) def test_WHEN_get_published_model_runs_THEN_list_ordered_by_date_created_newest_first(self): with session_scope(Session) as session: # Create three published models not in datetime order model_run1 = ModelRun() model_run1.name = "MR1" model_run1.date_created = datetime(2013, 12, 7, 17, 15, 30) model_run1.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED) session.add(model_run1) session.commit() model_run2 = ModelRun() model_run2.name = "MR2" model_run2.date_created = datetime(2014, 6, 9, 12, 30, 24) model_run2.status = self._status(constants.MODEL_RUN_STATUS_PUBLIC) session.add(model_run2) session.commit() model_run3 = ModelRun() model_run3.name = "MR3" model_run3.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED) model_run3.date_created = datetime(2014, 6, 9, 11, 39, 30) session.add(model_run3) session.commit() model_runs = self.model_run_service.get_published_models() assert_that(model_runs[0].name, is_("MR2")) assert_that(model_runs[1].name, is_("MR3")) assert_that(model_runs[2].name, is_("MR1")) def test_WHEN_get_code_versions_THEN_returns_list_including_default_code_version(self): models = self.model_run_service.get_code_versions() assert_that(len(models), is_not(0), "There should be at least one code version on the list") assert_that([x.name for x in models], has_item(config['default_code_version']), "The list of code versions") def test_GIVEN_valid_code_version_id_WHEN_get_code_version_THEN_code_version_returned(self): expectedModel = self.model_run_service.get_code_versions()[0] model = self.model_run_service.get_code_version_by_id(expectedModel.id) assert_that(model.id, is_(expectedModel.id), "Id") assert_that(model.name, is_(expectedModel.name), "Name") def test_GIVEN_non_existent_code_version_id_WHEN_get_code_version_THEN_raises_NoResultFound_exception(self): with self.assertRaises(NoResultFound, msg="Should have thrown a NoResultFound exception"): model = self.model_run_service.get_code_version_by_id(-100) def test_GIVEN_user_has_model_run_WHEN_get_model_run_by_id_THEN_model_run_returned(self): # Add a user and give them a model with session_scope(Session) as session: # First add user user = User() user.name = 'user1' session.add(user) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_PUBLIC) session.add(model_run) # Get the users model runs model_run_returned = self.model_run_service.get_model_by_id(user, model_run.id) assert_that(model_run_returned.name, is_("MR1")) def test_GIVEN_model_run_has_parameters_WHEN_get_model_run_by_id_THEN_model_run_has_parameter_values_loaded(self): # Add a user and give them a model user = self.login() model_run = self.create_run_model(0, "MR1", user) with session_scope(Session) as session: # Add parameter value parameter_value = ParameterValue() parameter_value.parameter_id = 1 parameter_value.set_value_from_python(123) parameter = Parameter() parameter.name = "Param" parameter.parameter_values = [parameter_value] # Give them a model model_run = session.query(ModelRun).filter(ModelRun.id == model_run.id).one() model_run.parameter_values = [parameter_value] session.add(model_run) with session_scope(Session) as session: model_run_id = session.query(ModelRun).filter(ModelRun.name == model_run.name).one().id # Get the users model runs model_run_returned = self.model_run_service.get_model_by_id(user, model_run_id) pv = model_run_returned.parameter_values[0] assert_that(pv.value, is_('123')) assert_that(pv.parameter.name, is_("Param")) def test_GIVEN_model_run_id_belongs_to_another_user_WHEN_get_model_run_by_id_THEN_NoResultFound_exception(self): # Add two users and give one a model with session_scope(Session) as session: user = User() user.name = 'user1' user2 = User() user2.name = 'user2' session.add_all([user, user2]) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED) session.add(model_run) # Get the users model runs with self.assertRaises(NoResultFound, msg="Should have thrown a NoResultFound exception"): self.model_run_service.get_model_by_id(user2, model_run.id) def test_GIVEN_no_defining_model_run_WHEN_get_defining_model_run_THEN_error_returned(self): user = self.login() with self.assertRaises(NoResultFound, msg="Should have thrown a NoResultFound exception"):self.model_run_service.get_parameters_for_model_being_created(user) def test_GIVEN_incomplete_model_WHEN_publish_model_THEN_ServiceException_raised(self): # Add a user and give them a model with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_FAILED) session.add(model_run) # Get the users model runs with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"): self.model_run_service.publish_model(user, model_run.id) def test_GIVEN_model_belongs_to_another_user_WHEN_publish_model_THEN_ServiceException_raised(self): # Add two users and give one a model with session_scope(Session) as session: user = User() user.name = 'user1' user2 = User() user2.name = 'user2' session.add_all([user, user2]) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED) session.add(model_run) # Get the users model runs with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"): self.model_run_service.publish_model(user2, model_run.id) def test_GIVEN_nonexistent_model_id_WHEN_publish_model_THEN_ServiceException_raised(self): # Add a user with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"): self.model_run_service.publish_model(user, -100) def test_GIVEN_user_has_completed_model_WHEN_publish_model_THEN_model_published(self): # Add a user and give them a model with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED) session.add(model_run) # Get the users model runs self.model_run_service.publish_model(user, model_run.id) with session_scope(Session) as session: updated_model_run = session.query(ModelRun).join(ModelRunStatus).filter(ModelRun.id == model_run.id).one() assert_that(updated_model_run.status.name, is_(constants.MODEL_RUN_STATUS_PUBLISHED)) def test_GIVEN_complete_model_WHEN_make_public_model_THEN_ServiceException_raised(self): # Add a user and give them a model with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_COMPLETED) session.add(model_run) # Get the users model runs with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"): self.model_run_service.make_public_model(user, model_run.id) def test_GIVEN_model_belongs_to_another_user_WHEN_make_public_model_THEN_ServiceException_raised(self): # Add two users and give one a model with session_scope(Session) as session: user = User() user.name = 'user1' user2 = User() user2.name = 'user2' session.add_all([user, user2]) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED) session.add(model_run) # Get the users model runs with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"): self.model_run_service.make_public_model(user2, model_run.id) def test_GIVEN_nonexistent_model_id_WHEN_make_public_model_THEN_ServiceException_raised(self): # Add a user with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) with self.assertRaises(ServiceException, msg="Should have raised a ServiceException"): self.model_run_service.make_public_model(user, -100) def test_GIVEN_user_has_published_model_WHEN_make_public_model_THEN_model_public(self): # Add a user and give them a model with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) session.commit() # Give them a model model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_PUBLISHED) session.add(model_run) # Get the users model runs self.model_run_service.make_public_model(user, model_run.id) with session_scope(Session) as session: updated_model_run = session.query(ModelRun).join(ModelRunStatus).filter(ModelRun.id == model_run.id).one() assert_that(updated_model_run.status.name, is_(constants.MODEL_RUN_STATUS_PUBLIC)) def test_GIVEN_default_science_configurations_THEN_all_configurations_returned(self): results = self.model_run_service.get_scientific_configurations() assert_that(len(results), greater_than(0), "at least one default configuration") assert_that(results[0]['id'], not_none(), "Configuration has an id") assert_that(results[0]['name'], not_none(), "Configuration has a name") assert_that(results[0]['description'], not_none(), "Configuration has a description") def test_GIVEN_model_with_parameters_WHEN_get_parameter_value_THEN_parameter_value_returned(self): with session_scope(Session) as session: user = User() user.name = 'user1' session.add(user) session.commit() param = session.query(Parameter).first() param_id = param.id param_name = param.name param_namelist = param.namelist.name model_run = ModelRun() model_run.name = "MR1" model_run.user_id = user.id model_run.status = self._status(constants.MODEL_RUN_STATUS_CREATED) session.add(model_run) session.commit() parameter1 = ParameterValue() parameter1.parameter_id = param_id parameter1.set_value_from_python("param1 value") parameter1.model_run_id = model_run.id session.add(parameter1) model_run_returned = self.model_run_service.get_model_being_created_with_non_default_parameter_values(user) param_value_returned = model_run_returned.get_python_parameter_value([param_namelist, param_name]) assert_that(param_value_returned, is_("param1 value")) def test_GIVEN_no_run_model_WHEN_create_run_model_with_science_config_THEN_model_created_with_parameter_values_copied(self): user = self.login() expected_name = "model run name" expected_description = "some slightly long winded description" self.model_run_service.update_model_run( user, expected_name, constants.DEFAULT_SCIENCE_CONFIGURATION, expected_description) parameter_values_count = self.count_parameter_values_in_model_being_created(user) assert_that(parameter_values_count, is_not(0), "parameter values have been set") def test_GIVEN_run_model_WHEN_create_run_model_with_same_science_config_THEN_model_updated_with_new_parameter_values_copied(self): user = self.login() expected_name = "model run name" expected_description = "some slightly long winded description" self.model_run_service.update_model_run( user, expected_name, constants.DEFAULT_SCIENCE_CONFIGURATION, expected_description) expected_parameters_count = self.count_parameter_values_in_model_being_created(user) self.model_run_service.update_model_run( user, expected_name, constants.DEFAULT_SCIENCE_CONFIGURATION, expected_description) parameter_values_count = self.count_parameter_values_in_model_being_created(user) assert_that(parameter_values_count, is_(expected_parameters_count), "parameter values have been set and old ones removed") def test_GIVEN_run_model_with_time_extent_WHEN_create_run_model_with_same_science_config_THEN_model_updated_with_new_parameter_values_copied_and_has_time_extent(self): user = self.login() expected_name = "model run name" expected_description = "some slightly long winded description" self.model_run_service.update_model_run( user, expected_name, constants.DEFAULT_SCIENCE_CONFIGURATION, expected_description) self.model_run_service.save_parameter(constants.JULES_PARAM_LATLON_REGION, True, user) expected_parameters_count = self.count_parameter_values_in_model_being_created(user) self.model_run_service.update_model_run( user, expected_name, constants.DEFAULT_SCIENCE_CONFIGURATION, expected_description) parameter_values_count = self.count_parameter_values_in_model_being_created(user) assert_that(parameter_values_count, is_(expected_parameters_count), "parameter values have been set and old ones removed") def count_parameter_values_in_model_being_created(self, user): """ Count the number of parameter values that this model has """ with session_scope(Session) as session: parameter_values = session \ .query(ParameterValue) \ .join(ModelRun) \ .join(ModelRunStatus) \ .filter(ModelRunStatus.name == constants.MODEL_RUN_STATUS_CREATED) \ .filter(ModelRun.user == user) \ .count() return parameter_values
class TestModelRunExtents(TestController): def setUp(self): super(TestModelRunExtents, self).setUp() self.clean_database() self.user = self.login() self.model_run_service = ModelRunService() with session_scope(Session) as session: self.driving_data = DrivingDataset() self.driving_data.name = "d1" self.driving_data.boundary_lat_north = 47.5 self.driving_data.boundary_lat_south = 13.8 self.driving_data.boundary_lon_east = 123.1 self.driving_data.boundary_lon_west = -15.0 self.driving_data.time_start = datetime.datetime(1901, 1, 1, 0, 0, 0) self.driving_data.time_end = datetime.datetime(2001, 1, 1, 0, 0, 0) session.add(self.driving_data) session.commit() self.model_run = ModelRun() self.model_run.name = "MR1" self.model_run.status = self._status(MODEL_RUN_STATUS_CREATED) self.model_run.driving_dataset_id = self.driving_data.id self.model_run.user = self.user self.model_run.science_configuration_id = 2 param1 = self.model_run_service.get_parameter_by_constant(JULES_PARAM_DRIVE_INTERP) pv1 = ParameterValue() pv1.parameter_id = param1.id pv1.set_value_from_python(8 * ['nf']) param2 = self.model_run_service.get_parameter_by_constant(JULES_PARAM_DRIVE_DATA_PERIOD) pv2 = ParameterValue() pv2.parameter_id = param2.id pv2.set_value_from_python(60 * 60) self.model_run.parameter_values = [pv1, pv2] session.add(self.model_run) def set_up_single_cell_user_driving_data(self): self.clean_database() del self.driving_data self.user = self.login() user_upload_id = DatasetService().get_id_for_user_upload_driving_dataset() with session_scope(Session) as session: self.model_run = ModelRun() self.model_run.name = "MR1" self.model_run.status = self._status(MODEL_RUN_STATUS_CREATED) self.model_run.driving_dataset_id = user_upload_id self.model_run.user = self.user self.model_run.driving_data_lat = 25 self.model_run.driving_data_lon = 40 self.model_run.driving_data_rows = 248 self.model_run.science_configuration_id = 2 param1 = self.model_run_service.get_parameter_by_constant(JULES_PARAM_DRIVE_INTERP) pv1 = ParameterValue() pv1.parameter_id = param1.id pv1.set_value_from_python(8 * ['nf']) param2 = self.model_run_service.get_parameter_by_constant(JULES_PARAM_DRIVE_DATA_PERIOD) pv2 = ParameterValue() pv2.parameter_id = param2.id pv2.set_value_from_python(60 * 60) param3 = self.model_run_service.get_parameter_by_constant(JULES_PARAM_DRIVE_DATA_START) pv3 = ParameterValue() pv3.parameter_id = param3.id pv3.value = "'1901-01-01 00:00:00'" param4 = self.model_run_service.get_parameter_by_constant(JULES_PARAM_DRIVE_DATA_END) pv4 = ParameterValue() pv4.parameter_id = param4.id pv4.value = "'1901-01-31 21:00:00'" self.model_run.parameter_values = [pv1, pv2, pv3, pv4] session.add(self.model_run) def test_GIVEN_no_created_model_WHEN_page_get_THEN_redirect_to_create(self): self.clean_database() self.login() response = self.app.get( url(controller='model_run', action='extents')) 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_no_driving_dataset_selected_for_model_WHEN_page_get_THEN_redirect_to_driving_data(self): model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) with session_scope(Session) as session: model_run.driving_dataset_id = None session.add(model_run) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='driving_data')), "url") def test_GIVEN_nothing_WHEN_page_get_THEN_extents_page_rendered(self): response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("Specify Model Run Extents")) self.assert_model_run_creation_action(self.user, 'extents') def test_GIVEN_driving_dataset_selected_for_model_WHEN_page_get_THEN_driving_data_spatial_extents_rendered(self): response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string(str(self.driving_data.boundary_lat_north))) assert_that(response.normal_body, contains_string(str(self.driving_data.boundary_lat_south))) assert_that(response.normal_body, contains_string(str(self.driving_data.boundary_lon_east))) assert_that(response.normal_body, contains_string(str(self.driving_data.boundary_lon_west))) def test_GIVEN_driving_dataset_selected_for_model_WHEN_page_get_THEN_driving_data_temporal_extents_rendered(self): response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("1901-01-01")) assert_that(response.normal_body, contains_string("1911-01-01")) def test_GIVEN_multi_cell_spatial_extents_already_chosen_WHEN_page_get_THEN_existing_extents_rendered(self): self.model_run_service.save_parameter(JULES_PARAM_LON_BOUNDS, [12.3, 35.5], self.user) self.model_run_service.save_parameter(JULES_PARAM_LAT_BOUNDS, [50, 70], self.user) self.model_run_service.save_parameter(JULES_PARAM_USE_SUBGRID, True, self.user) self.model_run_service.save_parameter(JULES_PARAM_LATLON_REGION, True, self.user) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("12.3")) assert_that(response.normal_body, contains_string("35.5")) assert_that(response.normal_body, contains_string("50")) assert_that(response.normal_body, contains_string("70")) def test_GIVEN_single_cell_spatial_extents_already_chosen_WHEN_page_get_THEN_existing_extents_rendered(self): self.model_run_service.save_parameter(JULES_PARAM_USE_SUBGRID, True, self.user) self.model_run_service.save_parameter(JULES_PARAM_LATLON_REGION, False, self.user) self.model_run_service.save_parameter(JULES_PARAM_NPOINTS, 1, self.user) self.model_run_service.save_parameter(JULES_PARAM_POINTS_FILE, [55, 12.3], self.user) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("55")) assert_that(response.normal_body, contains_string("12.3")) def test_GIVEN_temporal_extents_already_chosen_WHEN_page_get_THEN_existing_extents_rendered(self): self.model_run_service.save_parameter(JULES_PARAM_LON_BOUNDS, [12.3, 35.5], self.user) self.model_run_service.save_parameter(JULES_PARAM_LAT_BOUNDS, [50, 70], self.user) self.model_run_service.save_parameter(JULES_PARAM_USE_SUBGRID, True, self.user) self.model_run_service.save_parameter(JULES_PARAM_LATLON_REGION, True, self.user) start_time = datetime.datetime(1935, 3, 5, 17, 12, 11) end_time = datetime.datetime(1969, 7, 21, 20, 17, 00) self.model_run_service.save_parameter(JULES_PARAM_RUN_START, start_time, self.user) self.model_run_service.save_parameter(JULES_PARAM_RUN_END, end_time, self.user) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string(start_time.strftime("%Y-%m-%d"))) assert_that(response.normal_body, contains_string(end_time.strftime("%Y-%m-%d"))) def test_GIVEN_invalid_multi_cell_spatial_extents_already_chosen_WHEN_page_get_THEN_errors_shown(self): self.model_run_service.save_parameter(JULES_PARAM_LON_BOUNDS, [40, 500], self.user) self.model_run_service.save_parameter(JULES_PARAM_LAT_BOUNDS, [20, 25], self.user) self.model_run_service.save_parameter(JULES_PARAM_USE_SUBGRID, True, self.user) self.model_run_service.save_parameter(JULES_PARAM_LATLON_REGION, True, self.user) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("Longitude must be between -180 and 180")) def test_GIVEN_invalid_single_cell_spatial_extents_already_chosen_WHEN_page_get_THEN_errors_shown(self): self.model_run_service.save_parameter(JULES_PARAM_USE_SUBGRID, True, self.user) self.model_run_service.save_parameter(JULES_PARAM_LATLON_REGION, False, self.user) self.model_run_service.save_parameter(JULES_PARAM_NPOINTS, 1, self.user) self.model_run_service.save_parameter(JULES_PARAM_POINTS_FILE, [55, 593], self.user) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("Longitude must be between -180 and 180")) def test_GIVEN_invalid_temporal_extents_already_chosen_WHEN_page_get_THEN_errors_shown(self): self.model_run_service.save_parameter(JULES_PARAM_LON_BOUNDS, [12.3, 35.5], self.user) self.model_run_service.save_parameter(JULES_PARAM_LAT_BOUNDS, [50, 70], self.user) self.model_run_service.save_parameter(JULES_PARAM_USE_SUBGRID, True, self.user) self.model_run_service.save_parameter(JULES_PARAM_LATLON_REGION, True, self.user) start_time = datetime.datetime(1900, 10, 14, 17, 12, 11) end_time = datetime.datetime(1969, 7, 21, 20, 17, 00) self.model_run_service.save_parameter(JULES_PARAM_RUN_START, start_time, self.user) self.model_run_service.save_parameter(JULES_PARAM_RUN_END, end_time, self.user) response = self.app.get( url(controller='model_run', action='extents')) assert_that(response.normal_body, contains_string("Start date cannot be earlier than 1901-01-01")) def test_GIVEN_invalid_multi_cell_spatial_extents_WHEN_post_THEN_errors_rendered(self): response = self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'multi', 'lat_n': 25, 'lat_s': 20, 'lon_e': 40, 'lon_w': 500, 'start_date': '1940-10-13', 'end_date': '1950-10-13' }) assert_that(response.normal_body, contains_string("Longitude must be between -180 and 180")) def test_GIVEN_invalid_single_cell_spatial_extents_WHEN_post_THEN_errors_rendered(self): response = self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'single', 'lat': -88, 'lon': 40, 'start_date': '1940-10-13', 'end_date': '1950-10-13' }) assert_that(response.normal_body, contains_string("Latitude (-88 deg N) cannot be south of 13.8 deg N")) def test_GIVEN_invalid_temporal_extents_WHEN_post_THEN_errors_rendered(self): response = self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'multi', 'lat_n': 25, 'lat_s': 20, 'lon_e': 40, 'lon_w': 35, 'start_date': '1900-10-14', 'end_date': '1950-10-13' }) assert_that(response.normal_body, contains_string("Start date cannot be earlier than 1901-01-01")) def test_GIVEN_valid_multi_cell_extents_WHEN_post_THEN_extents_saved(self): self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'multi', 'lat_n': 25, 'lat_s': 20, 'lon_e': 40, 'lon_w': 35, 'start_date': '1940-10-13', 'end_date': '1950-10-13' }) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) lat_bounds = model_run.get_parameter_values(JULES_PARAM_LAT_BOUNDS)[0].value lon_bounds = model_run.get_parameter_values(JULES_PARAM_LON_BOUNDS)[0].value use_subgrid = model_run.get_parameter_values(JULES_PARAM_USE_SUBGRID)[0].value latlon_region = model_run.get_parameter_values(JULES_PARAM_LATLON_REGION)[0].value start_run = model_run.get_parameter_values(JULES_PARAM_RUN_START)[0].value end_run = model_run.get_parameter_values(JULES_PARAM_RUN_END)[0].value assert_that(lat_bounds, is_("20 25")) assert_that(lon_bounds, is_("35 40")) assert_that(use_subgrid, is_(".true.")) assert_that(latlon_region, is_(".true.")) assert_that(str(start_run), is_("'1940-10-13 00:00:00'")) assert_that(str(end_run), is_("'1950-10-13 23:00:00'")) # Time is moved forward to match the acceptable end def test_GIVEN_valid_single_cell_extents_WHEN_post_THEN_extents_saved(self): self.set_up_single_cell_user_driving_data() self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'single', 'lat': 25, 'lon': 40, 'start_date': '1901-01-4', 'end_date': '1901-01-13', 'average_over_cell': 1 }) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) pointfile = model_run.get_parameter_values(JULES_PARAM_POINTS_FILE)[0].value n_points = model_run.get_parameter_values(JULES_PARAM_NPOINTS)[0].value use_subgrid = model_run.get_parameter_values(JULES_PARAM_USE_SUBGRID)[0].value latlon_region = model_run.get_parameter_values(JULES_PARAM_LATLON_REGION)[0].value l_point_data = model_run.get_parameter_values(JULES_PARAM_SWITCHES_L_POINT_DATA)[0].value start_run = model_run.get_parameter_values(JULES_PARAM_RUN_START)[0].value end_run = model_run.get_parameter_values(JULES_PARAM_RUN_END)[0].value assert_that(pointfile, is_("25 40")) assert_that(n_points, is_("1")) assert_that(use_subgrid, is_(".true.")) assert_that(latlon_region, is_(".false.")) assert_that(l_point_data, is_(".false.")) assert_that(str(start_run), is_("'1901-01-04 00:00:00'")) assert_that(str(end_run), is_("'1901-01-13 20:00:00'")) # Interpolation flag brings us forward an hour def test_GIVEN_valid_extents_WHEN_post_THEN_redirect_to_output(self): response = self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'multi', 'lat_n': 25, 'lat_s': 20, 'lon_e': 40, 'lon_w': 35, 'start_date': '1940-10-13', 'end_date': '1950-10-13' }) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='land_cover')), "url") def test_GIVEN_valid_extents_and_user_over_quota_WHEN_post_THEN_redirect_to_catalogue(self): self.create_run_model(storage_in_mb=self.user.storage_quota_in_gb * 1024 + 1, name="big_run", user=self.user) response = self.app.post( url(controller='model_run', action='extents'), params={ 'submit': u'Next', 'site': u'multi', 'lat_n': 25, 'lat_s': 20, 'lon_e': 40, 'lon_w': 35, 'start_date': '1940-10-13', 'end_date': '1950-10-13' }) 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") def test_GIVEN_bng_WHEN_bng_to_latlon_service_called_THEN_correct_latlon_returned(self): # BNG test values are from http://www.bgs.ac.uk/data/webservices/convertForm.cfm bng_easting = 429157 bng_northing = 623009 lat = 55.5 lon = -1.54 delta = 0.00001 response = self.app.post( url(controller='model_run', action='bng_to_latlon'), params={ 'bng_easting': bng_easting, 'bng_northing': bng_northing }) json = response.json_body assert_that(json['lat'], is_(close_to(lat, delta))) assert_that(json['lon'], is_(close_to(lon, delta))) def test_GIVEN_invalid_bng_WHEN_bng_to_latlon_service_called_THEN_error_returned(self): response = self.app.post( url(controller='model_run', action='bng_to_latlon'), params={ 'bng_easting': u'asd', 'bng_northing': u'1.1,23413' }) json = response.json_body assert_that(json['is_error'], is_(True))
class TestModelRunOutput(TestController): def setUp(self): super(TestModelRunOutput, self).setUp() self.clean_database() self.user = self.login() self.valid_params = { 'submit': u'Next', 'ov_select_1': 1, 'ov_yearly_1': 1, 'ov_monthly_1': 1, 'ov_select_2': 1, 'ov_hourly_2': 1, 'ov_daily_2': 1, 'ov_select_3': 1, 'ov_monthly_3': 1 } self.model_run_service = ModelRunService() param_run_start = self.model_run_service.get_parameter_by_constant(JULES_PARAM_RUN_START) param_run_end = self.model_run_service.get_parameter_by_constant(JULES_PARAM_RUN_END) with session_scope(Session) as session: self.model_run = self.model_run_service._create_new_model_run(session, self.user) self.model_run.name = "MR1" pv_run_start = ParameterValue() pv_run_start.parameter_id = param_run_start.id pv_run_start.value = "'1901-01-01 00:00:00'" pv_run_end = ParameterValue() pv_run_end.parameter_id = param_run_end.id pv_run_end.value = "'1902-01-01 00:00:00'" self.model_run.parameter_values = [pv_run_start, pv_run_end] session.add(self.model_run) def test_GIVEN_no_model_being_created_WHEN_page_get_THEN_redirect_to_create_model_run(self): self.clean_database() self.user = self.login() response = self.app.get( url(controller='model_run', action='output')) 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_model_run_created_WHEN_page_get_THEN_page_loaded(self): response = self.app.get( url(controller='model_run', action='output')) assert_that(response.normal_body, contains_string("Select Output Variables")) self.assert_model_run_creation_action(self.user, 'output') def test_GIVEN_output_parameters_already_chosen_WHEN_page_get_THEN_parameters_rendered(self): self.app.post( url(controller='model_run', action='output'), params=self.valid_params) response = self.app.get(url(controller='model_run', action='output')) doc = html.fromstring(response.normal_body) # Check that the first output variable is displayed, selected and the yearly and monthly period boxes selected: output_row_1 = doc.xpath('//div[@id="output_row_1"]/@style') assert_that(output_row_1, is_not(contains_string('display:none'))) ov_select_1 = doc.xpath('//input[@name="ov_select_1"]/@checked') ov_yearly_1 = doc.xpath('//input[@name="ov_yearly_1"]/@checked') ov_monthly_1 = doc.xpath('//input[@name="ov_monthly_1"]/@checked') ov_daily_1 = doc.xpath('//input[@name="ov_daily_1"]/@checked') ov_hourly_1 = doc.xpath('//input[@name="ov_hourly_1"]/@checked') assert_that(len(ov_select_1), is_(1)) assert_that(len(ov_yearly_1), is_(1)) assert_that(len(ov_monthly_1), is_(1)) assert_that(len(ov_daily_1), is_(0)) assert_that(len(ov_hourly_1), is_(0)) # For the second we expect the same but with the hourly period box selected output_row_2 = doc.xpath('//div[@id="output_row_2"]/@style') assert_that(output_row_2, is_not(contains_string('display:none'))) ov_select_2 = doc.xpath('//input[@name="ov_select_2"]/@checked') ov_yearly_2 = doc.xpath('//input[@name="ov_yearly_2"]/@checked') ov_monthly_2 = doc.xpath('//input[@name="ov_monthly_2"]/@checked') ov_daily_2 = doc.xpath('//input[@name="ov_daily_2"]/@checked') ov_hourly_2 = doc.xpath('//input[@name="ov_hourly_2"]/@checked') assert_that(len(ov_select_2), is_(1)) assert_that(len(ov_yearly_2), is_(0)) assert_that(len(ov_monthly_2), is_(0)) assert_that(len(ov_daily_2), is_(1)) assert_that(len(ov_hourly_2), is_(1)) # For the third we expect the monthly box selected output_row_3 = doc.xpath('//div[@id="output_row_3"]/@style') assert_that(output_row_3, is_not(contains_string('display:none'))) ov_select_3 = doc.xpath('//input[@name="ov_select_3"]/@checked') ov_yearly_3 = doc.xpath('//input[@name="ov_yearly_3"]/@checked') ov_monthly_3 = doc.xpath('//input[@name="ov_monthly_3"]/@checked') ov_daily_3 = doc.xpath('//input[@name="ov_daily_3"]/@checked') ov_hourly_3 = doc.xpath('//input[@name="ov_hourly_3"]/@checked') assert_that(len(ov_select_3), is_(1)) assert_that(len(ov_yearly_3), is_(0)) assert_that(len(ov_monthly_3), is_(1)) assert_that(len(ov_daily_3), is_(0)) assert_that(len(ov_hourly_3), is_(0)) # Finally we check that no other output parameters are selected or visible ov_selects = doc.xpath('//input[contains(@name, "ov_select_") and not(@checked)]') n_output_params = len(self.model_run_service.get_output_variables(include_depends_on_nsmax=False)) assert_that(len(ov_selects), is_(n_output_params - 3)) invisible_rows = doc.xpath('//div[contains(@id, "output_row_") and contains(@style, "display:none")]') assert_that(len(invisible_rows), is_(n_output_params - 3)) def test_GIVEN_selected_outputs_WHEN_post_THEN_redirected_to_next_page(self): response = self.app.post( url(controller='model_run', action='output'), params=self.valid_params) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='submit')), "url") def test_GIVEN_selected_outputs_and_user_over_quota_WHEN_post_THEN_redirected_to_catalogue(self): self.create_run_model(storage_in_mb=self.user.storage_quota_in_gb * 1024 + 1, name="big_run", user=self.user) response = self.app.post( url(controller='model_run', action='output'), params=self.valid_params) 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") def test_GIVEN_selected_outputs_WHEN_post_THEN_params_stored_in_database(self): self.app.post( url(controller='model_run', action='output'), params=self.valid_params) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) # Check we have the right number of profiles n_expected = 5 p_output_nprofiles = model_run.get_parameter_values(JULES_PARAM_OUTPUT_NPROFILES)[0].get_value_as_python() assert_that(p_output_nprofiles, is_(n_expected)) pv_var = model_run.get_parameter_values(JULES_PARAM_OUTPUT_VAR) pv_nvars = model_run.get_parameter_values(JULES_PARAM_OUTPUT_NVARS) pv_profile_name = model_run.get_parameter_values(JULES_PARAM_OUTPUT_PROFILE_NAME) pv_output_main_run = model_run.get_parameter_values(JULES_PARAM_OUTPUT_MAIN_RUN) pv_output_period = model_run.get_parameter_values(JULES_PARAM_OUTPUT_PERIOD) pv_output_types = model_run.get_parameter_values(JULES_PARAM_OUTPUT_TYPE) # Check they are all 5 assert_that(len(pv_var), is_(n_expected)) assert_that(len(pv_nvars), is_(n_expected)) assert_that(len(pv_profile_name), is_(n_expected)) assert_that(len(pv_output_main_run), is_(n_expected)) assert_that(len(pv_output_period), is_(n_expected)) assert_that(len(pv_output_types), is_(n_expected)) # Create list of values so we can check they are correct (easier than checking ParameterValue objects) var_vals = [pv.get_value_as_python() for pv in pv_var] nvars_vals = [pv.get_value_as_python() for pv in pv_nvars] profile_name_vals = [pv.get_value_as_python() for pv in pv_profile_name] output_main_run_vals = [pv.get_value_as_python() for pv in pv_output_main_run] output_period_vals = [pv.get_value_as_python() for pv in pv_output_period] output_types = [pv.get_value_as_python() for pv in pv_output_types] # Check that we got the correct values # The order is assured consistent by the sort in model_run.get_parameter_values() output_var_1 = self.model_run_service.get_output_variable_by_id(1) output_var_2 = self.model_run_service.get_output_variable_by_id(2) output_var_3 = self.model_run_service.get_output_variable_by_id(3) assert_that(var_vals, is_([output_var_1.name, output_var_1.name, output_var_2.name, output_var_2.name, output_var_3.name])) assert_that(nvars_vals, is_([1, 1, 1, 1, 1])) assert_that(profile_name_vals, is_([output_var_1.name + '_yearly', output_var_1.name + '_monthly', output_var_2.name + '_daily', output_var_2.name + '_hourly', output_var_3.name + '_monthly'])) assert_that(output_main_run_vals, is_([True, True, True, True, True])) assert_that(output_period_vals, is_([-2, -1, JULES_DAILY_PERIOD, JULES_HOURLY_PERIOD, -1])) assert_that(output_types, is_(['M', 'M', 'M', 'M', 'M'])) def test_GIVEN_selected_outputs_WHEN_go_back_THEN_previous_paged_rendered(self): params = self.valid_params params['submit'] = u'Back' response = self.app.post( url(controller='model_run', action='output'), params=params) assert_that(response.status_code, is_(302), "Response is redirect") assert_that(urlparse(response.response.location).path, is_(url(controller='model_run', action='land_cover')), "url") def test_GIVEN_nsmax_set_zero_WHEN_page_get_THEN_nsmax_parameters_not_available_on_page(self): self.model_run_service.save_parameter(JULES_PARAM_NSMAX, 0, self.user) response = self.app.get( url(controller='model_run', action='output')) for output_name in DEPENDS_ON_NSMAX: assert_that(response.normal_body, is_not(contains_string(output_name))) def test_GIVEN_nsmax_set_three_WHEN_page_get_THEN_nsmax_parameters_shown_on_page(self): self.model_run_service.save_parameter(JULES_PARAM_NSMAX, 3, self.user) response = self.app.get( url(controller='model_run', action='output')) output_variables = self.model_run_service.get_output_variables(include_depends_on_nsmax=True) for output_variable in output_variables: if output_variable.depends_on_nsmax: assert_that(response.normal_body, contains_string(str(output_variable.name))) def test_GIVEN_page_already_run_once_WHEN_page_post_THEN_new_parameters_overwrite_old_parameters(self): self.app.post( url(controller='model_run', action='output'), params=self.valid_params) new_params = { 'submit': u'Next', 'ov_select_1': 1, 'ov_hourly_1': 1, 'ov_select_10': 1, 'ov_yearly_10': 1, 'ov_monthly_10': 1 } self.app.post( url(controller='model_run', action='output'), params=new_params) model_run = self.model_run_service.get_model_being_created_with_non_default_parameter_values(self.user) # Check we have the right number of profiles n_expected = 3 p_output_nprofiles = model_run.get_parameter_values(JULES_PARAM_OUTPUT_NPROFILES)[0].get_value_as_python() assert_that(p_output_nprofiles, is_(n_expected)) pv_var = model_run.get_parameter_values(JULES_PARAM_OUTPUT_VAR) pv_nvars = model_run.get_parameter_values(JULES_PARAM_OUTPUT_NVARS) pv_profile_name = model_run.get_parameter_values(JULES_PARAM_OUTPUT_PROFILE_NAME) pv_output_main_run = model_run.get_parameter_values(JULES_PARAM_OUTPUT_MAIN_RUN) pv_output_period = model_run.get_parameter_values(JULES_PARAM_OUTPUT_PERIOD) pv_output_types = model_run.get_parameter_values(JULES_PARAM_OUTPUT_TYPE) # Check they are all 3 (not 4 anymore) assert_that(len(pv_var), is_(n_expected)) assert_that(len(pv_nvars), is_(n_expected)) assert_that(len(pv_profile_name), is_(n_expected)) assert_that(len(pv_output_main_run), is_(n_expected)) assert_that(len(pv_output_period), is_(n_expected)) assert_that(len(pv_output_types), is_(n_expected)) # Create list of values so we can check they are correct (easier than checking ParameterValue objects) var_vals = [pv.get_value_as_python() for pv in pv_var] nvars_vals = [pv.get_value_as_python() for pv in pv_nvars] profile_name_vals = [pv.get_value_as_python() for pv in pv_profile_name] output_main_run_vals = [pv.get_value_as_python() for pv in pv_output_main_run] output_period_vals = [pv.get_value_as_python() for pv in pv_output_period] output_types = [pv.get_value_as_python() for pv in pv_output_types] # Check that we got the correct values # The order is assured consistent by the sort in model_run.get_parameter_values() output_var_1 = self.model_run_service.get_output_variable_by_id(1) output_var_10 = self.model_run_service.get_output_variable_by_id(10) assert_that(var_vals, is_([output_var_1.name, output_var_10.name, output_var_10.name])) assert_that(nvars_vals, is_([1, 1, 1])) assert_that(profile_name_vals, is_([output_var_1.name + '_hourly', output_var_10.name + '_yearly', output_var_10.name + '_monthly'])) assert_that(output_main_run_vals, is_([True, True, True])) assert_that(output_period_vals, is_([JULES_HOURLY_PERIOD, -2, -1])) assert_that(output_types, is_(['M', 'M', 'M']))