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_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_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 parse_namelist_files_to_create_a_model_run(self, code_version, description, config_dir,
                                                   name, namelist_files, status, user, spinup_in_years):
        """
        Ceate a model run
        :param code_version: the code version object
        :param description: the description fo the model run
        :param config_dir: the configuration directory
        :param name: name of the model run
        :param namelist_files: namelist_files object
        :param status: status the model is to have
        :param user: the user creating the model run
        :param spinup_in_years: the spin up in years, for a science configuration
        :return: a model run
        """
        model_run = ModelRun()
        model_run.name = name
        model_run.user = user
        model_run.code_version = code_version
        model_run.description = description
        model_run.status = status
        model_run.science_configuration_spinup_in_years = spinup_in_years

        model_run.parameter_values = []
        log.info("Creating Model Run For: {0} ({1})".format(model_run.name, config_dir))
        for namelist_file in namelist_files:
            filename = os.path.join(config_dir, namelist_file.filename)
            if os.path.exists(filename):
                log.info("Parsing: %s" % filename)
                nml_dict = self._read_namelist(filename)
                model_run.parameter_values.extend(self._create_parameter_values(namelist_file, nml_dict))
        return model_run
    def duplicate_run_model(self, model_id, user):
        """
        Duplicate the run model and all its parameters to a new run model
        :param model_id: model run id to duplicate
        :param user: the user duplicating the model
        :return: nothing
        """

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

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

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

            new_model_run = ModelRun()

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

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

            session.add(new_model_run)

        if model_run_to_duplicate.driving_dataset_id == id_for_user_upload_driving_dataset:
            try:
                self._job_runner_client.duplicate_uploaded_driving_data(model_run_to_duplicate.id, new_model_run.id)
            except ServiceException:
                self.delete_model_run_being_created(user)
                raise ServiceException("Could not duplicate the model run because "
                                       "the user uploaded data can not be duplicated")
 def test_GIVEN_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_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_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_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 setUp(self):
        model_run = ModelRun()
        model_run.name = "Model Run"
        model_run.user_id = 10

        output_var = OutputVariable()
        output_var.name = "gpp_gb"
        output_var.description = "Gridbox gross primary productivity"

        model_run_service = MagicMock()
        model_run_service.get_model_by_id = MagicMock(return_value=model_run)
        model_run_service.get_output_variable_by_id = MagicMock(return_value=output_var)

        self.download_helper = NetcdfDatasetDownloadHelper(model_run_service)
        self.user = User()
        self.user.name = 'User Name'
        self.user.id = 10
 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_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_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_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 setUp(self):
        self.clean_database()
        self.user = self.login()
        self.create_two_driving_datasets()
        dds = DatasetService().get_driving_datasets(self.user)[0]
        with session_scope() as session:
            model_run = ModelRun()
            model_run.name = "model run"
            model_run.change_status(session, MODEL_RUN_STATUS_CREATED)
            model_run.user = self.user
            model_run.driving_dataset_id = dds.id
            session.add(model_run)
            session.commit()

            self.model_run_service = ModelRunService()
            model_run = self.model_run_service._get_model_run_being_created(session, self.user)

            parameter_val = ParameterValue()
            parameter_val.parameter = self.model_run_service.get_parameter_by_constant(JULES_PARAM_LATLON_REGION)
            parameter_val.set_value_from_python(True)
            parameter_val.model_run_id = model_run.id
            session.add(parameter_val)

        self.add_land_cover_region(model_run)