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 TestWithFullModelRun(TestController):
    """
    Test class which includes a way of submiting the whole model run
    """

    def setUp(self):
        super(TestWithFullModelRun, self).setUp()
        self.running_job_client = JobRunnerClient([])
        self.email_service = EmailService()
        self.email_service.send_email = Mock()

        self.job_status_updater = JobStatusUpdaterService(
            job_runner_client=self.running_job_client,
            config=config,
            email_service=self.email_service,
            dap_client_factory=self.create_mock_dap_factory_client())

        self.clean_database()
        self.user = self.login()
        self.model_run_service = ModelRunService()

    def create_model_run_ready_for_submit(self):
        # Set up the model as if we'd gone through all the previous pages
        # The Create page
        self.model_name = u'name'
        self.model_description = u'This is a description'
        self.science_config = self.model_run_service.get_scientific_configurations()[0]
        model_science_config_id = self.science_config['id']
        response = self.app.post(url=url(controller='model_run', action='create'),
                                 params={
                                     'name': self.model_name,
                                     'science_configuration': str(model_science_config_id),
                                     'description': self.model_description
                                 })
        assert response.status_code == 302
        # The Driving Data page
        self.create_two_driving_datasets()
        dataset_service = DatasetService()
        driving_datasets = dataset_service.get_driving_datasets(self.user)
        self.driving_data = [dds for dds in driving_datasets if dds.name == "driving1"][0]
        response = self.app.post(url(controller='model_run', action='driving_data'),
                                 params={
                                     'driving_dataset': self.driving_data.id,
                                     'submit': u'Next'
                                 })
        assert response.status_code == 302
        # The Extents page
        self.lat_n, self.lat_s = 40, 0
        self.lon_w, self.lon_e = -15, 15
        self.date_start = datetime.datetime(1980, 1, 1, 0, 0, 0)
        self.date_end = datetime.datetime(1980, 1, 1, 0, 0, 0)
        response = self.app.post(url(controller='model_run', action='extents'),
                                 params={
                                     'submit': u'Next',
                                     'site': u'multi',
                                     'lat_n': self.lat_n,
                                     'lat_s': self.lat_s,
                                     'lon_e': self.lon_e,
                                     'lon_w': self.lon_w,
                                     'start_date': self.date_start.strftime("%Y-%m-%d"),
                                     'end_date': self.date_end.strftime("%Y-%m-%d")
                                 })
        assert response.status_code == 302
        # The Output Variables page
        response = self.app.post(url(controller='model_run', action='output'),
                                 params={
                                     'submit': u'Next',
                                     'ov_select_1': 1,
                                     'ov_hourly_1': 1,
                                     'ov_select_10': 1,
                                     'ov_yearly_10': 1,
                                     'ov_monthly_10': 1
                                 })
        assert response.status_code == 302

    def create_alternate_model_run(self):
        # Set up the model as if we'd gone through all the previous pages
        # (but differently to the other model)
        # The Create page
        self.model_name = u'alternate name'
        self.model_description = u'This is a description of another model_run'
        self.science_config = self.model_run_service.get_scientific_configurations()[0]
        model_science_config_id = self.science_config['id']
        response = self.app.post(url=url(controller='model_run', action='create'),
                                 params={
                                     'name': self.model_name,
                                     'science_configuration': str(model_science_config_id),
                                     'description': self.model_description
                                 })
        assert response.status_code == 302
        # The Driving Data page
        self.create_two_driving_datasets()
        dataset_service = DatasetService()
        driving_datasets = dataset_service.get_driving_datasets(self.user)
        self.driving_data = [dds for dds in driving_datasets if dds.name == "driving2"][0]
        response = self.app.post(url(controller='model_run', action='driving_data'),
                                 params={
                                     'driving_dataset': self.driving_data.id,
                                     'submit': u'Next'
                                 })
        assert response.status_code == 302
        # The Extents page
        self.lat_n, self.lat_s = 80, -75
        self.lon_w, self.lon_e = -100, 120
        self.date_start = datetime.datetime(1907, 1, 1, 0, 0, 0)
        self.date_end = datetime.datetime(1914, 1, 1, 0, 0, 0)
        response = self.app.post(url(controller='model_run', action='extents'),
                                 params={
                                     'submit': u'Next',
                                     'site': u'single',
                                     'lat': self.lat_n,
                                     'lon': self.lon_e,
                                     'start_date': self.date_start.strftime("%Y-%m-%d"),
                                     'end_date': self.date_end.strftime("%Y-%m-%d")
                                 })
        assert response.status_code == 302
        # The Output Variables page
        response = self.app.post(url(controller='model_run', action='output'),
                                 params={
                                     'submit': u'Next',
                                     'ov_select_6': 1,
                                     'ov_hourly_6': 1,
                                     'ov_monthly_6': 1,
                                     'ov_select_11': 1,
                                     'ov_yearly_11': 1,
                                     'ov_monthly_11': 1
                                 })
        assert response.status_code == 302

    def create_model_run_with_user_uploaded_driving_data(self):
        # Set up the model as if we'd gone through all the previous pages
        # and uploaded our own driving data
        # The Create page
        self.model_name = u'Run with my own driving data'
        self.model_description = u'This is a description of a model_run'
        self.science_config = self.model_run_service.get_scientific_configurations()[0]
        model_science_config_id = self.science_config['id']
        response = self.app.post(url=url(controller='model_run', action='create'),
                                 params={
                                     'name': self.model_name,
                                     'science_configuration': str(model_science_config_id),
                                     'description': self.model_description
                                 })
        assert response.status_code == 302
        # The Driving Data page
        self.sample_file_contents = "# solar   long  rain  snow    temp   wind     press      humid\n" \
                                    "# sw_down   lw_down  tot_rain  tot_snow    t   wind     pstar      q\n" \
                                    "# i   i  i  i    i   i     i      i\n" \
                                    "3.3  187.8   0.0   0.0  259.10  3.610  102400.5  1.351E-03\n" \
                                    "89.5  185.8   0.0   0.0  259.45  3.140  102401.9  1.357E-03\n" \
                                    "142.3  186.4   0.0   0.0  259.85  2.890  102401.0  1.369E-03\n" \
                                    "# ----- data for later times ----"
        data_service = DatasetService()
        self.create_two_driving_datasets()
        ds_id = data_service.get_id_for_user_upload_driving_dataset()
        response = self.app.post(
            url(controller='model_run', action='driving_data'),
            params={
                'driving_dataset': ds_id,
                'submit': u'Upload',
                'lat': u'55',
                'lon': u'45',
                'dt_start': u'2000-01-01 00:00',
                'dt_end': u'2000-01-01 02:00'},
            upload_files=[('driving-file', 'file.txt', self.sample_file_contents)]
        )
        assert response.status_code == 302
        # The Extents page
        self.lat_n, self.lat_s = 55, 55
        self.lon_w, self.lon_e = 45, 45
        self.date_start = datetime.datetime(2000, 1, 1, 0, 0, 0)
        self.date_end = datetime.datetime(2000, 1, 1, 0, 2, 0)
        response = self.app.post(url(controller='model_run', action='extents'),
                                 params={
                                     'submit': u'Next',
                                     'site': u'single',
                                     'lat': self.lat_n,
                                     'lon': self.lon_e,
                                     'start_date': self.date_start.strftime("%Y-%m-%d"),
                                     'end_date': self.date_end.strftime("%Y-%m-%d")
                                 })
        assert response.status_code == 302
        # The Output Variables page
        response = self.app.post(url(controller='model_run', action='output'),
                                 params={
                                     'submit': u'Next',
                                     'ov_select_6': 1,
                                     'ov_hourly_6': 1,
                                     'ov_monthly_6': 1,
                                     'ov_select_11': 1,
                                     'ov_yearly_11': 1,
                                     'ov_monthly_11': 1
                                 })
        assert response.status_code == 302