示例#1
0
    def test_val_finished(self):
        test_datasets = [Dataset.objects.get(short_name=globals.C3S),
                         Dataset.objects.get(short_name=globals.ASCAT),
                         Dataset.objects.get(short_name=globals.SMAP),]

        run = ValidationRun()
        run.start_time = datetime.now(tzlocal())
        run.end_time = datetime.now(tzlocal())
        run.user = self.testuser
        run.save()

        for ds in test_datasets:
            data_c = DatasetConfiguration()
            data_c.validation = run
            data_c.dataset = ds
            data_c.version = ds.versions.first()
            data_c.variable = ds.variables.first()
            data_c.save()

        ref_c = DatasetConfiguration()
        ref_c.validation = run
        ref_c.dataset = Dataset.objects.get(short_name='ISMN')
        ref_c.version = DatasetVersion.objects.get(short_name='ISMN_V20180712_MINI')
        ref_c.variable = DataVariable.objects.get(short_name='ISMN_soil_moisture')
        ref_c.save()

        run.reference_configuration = ref_c
        run.scaling_ref = ref_c
        run.save()

        val_mail.send_val_done_notification(run)
        self.check_outbox()
示例#2
0
    def test_val_expired(self):
        run = ValidationRun()
        now = datetime.now(tzlocal())
        run.start_time = now - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS)
        run.end_time = run.start_time + timedelta(days=1)
        run.user = self.testuser
        run.save()
        assert not run.expiry_notified

        val_mail.send_val_expiry_notification([run])
        self.check_outbox()

        assert ValidationRun.objects.get(pk=run.id).expiry_notified

        # multiple validations in one email:
        run = ValidationRun()
        now = datetime.now(tzlocal())
        run.start_time = now - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS)
        run.end_time = run.start_time + timedelta(days=1)
        run.user = self.testuser
        run.save()
        assert not run.expiry_notified

        run_2 = ValidationRun()
        now = datetime.now(tzlocal())
        run_2.start_time = now - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS)
        run_2.end_time = run_2.start_time + timedelta(days=1)
        run_2.user = self.testuser
        run_2.save()
        assert not run_2.expiry_notified

        val_mail.send_val_expiry_notification([run, run_2])

        assert ValidationRun.objects.get(pk=run.id).expiry_notified
        assert ValidationRun.objects.get(pk=run_2.id).expiry_notified
示例#3
0
    def test_doi(self):
        infile = 'testdata/output_data/c3s_era5land.nc'

        ## generate test validation
        val = ValidationRun()
        val.start_time = timezone.now() - timedelta(days=1)
        val.end_time = timezone.now()
        val.user = self.testuser
        val.save()

        data_c = DatasetConfiguration()
        data_c.validation = val
        data_c.dataset = Dataset.objects.get(short_name='C3S')
        data_c.version = DatasetVersion.objects.get(short_name='C3S_V201812')
        data_c.variable = DataVariable.objects.get(short_name='C3S_sm')
        data_c.save()

        ref_c = DatasetConfiguration()
        ref_c.validation = val
        ref_c.dataset = Dataset.objects.get(short_name='ISMN')
        ref_c.version = DatasetVersion.objects.get(short_name='ISMN_V20191211')
        ref_c.variable = DataVariable.objects.get(
            short_name='ISMN_soil_moisture')
        ref_c.save()

        val.reference_configuration = ref_c
        val.scaling_ref = ref_c
        val.save()

        ## set valid output file for validation
        run_dir = path.join(OUTPUT_FOLDER, str(val.id))
        mkdir_if_not_exists(run_dir)
        shutil.copy(infile, path.join(run_dir, 'results.nc'))
        set_outfile(val, run_dir)
        val.save()

        ## test the publishing form

        # no name given
        val.user.first_name = None
        val.user.last_name = None
        form = PublishingForm(validation=val)
        assert form

        # only first name given
        val.user.first_name = self.user_data['first_name']
        form = PublishingForm(validation=val)
        assert form
        val.user.first_name = None

        # only last name given
        val.user.last_name = self.user_data['last_name']
        form = PublishingForm(validation=val)
        assert form

        # first and last name given but not a real orcid id
        val.user.first_name = self.user_data['first_name']
        val.user.orcid = 'not a real orcid'
        form = PublishingForm(validation=val)

        caught_orcid_error = False
        try:
            assert form.pub_metadata
        except:
            caught_orcid_error = True

        assert caught_orcid_error

        # fix orcid
        val.user.orcid = self.user_data['orcid']

        ## finally everything should be ok and we can use the form to generate the necessary metadata
        form = PublishingForm(validation=val)
        metadata = form.pub_metadata

        ## create a test doi on zenodo's sandbox service
        get_doi_for_validation(val, metadata)

        val = ValidationRun.objects.get(pk=val.id)
        self.__logger.debug(val.doi)
        assert val.doi
        firstdoi = val.doi

        ## check that the DOI was correctly stored in the netcdf file
        with netCDF4.Dataset(val.output_file.path, mode='r') as ds:
            assert val.doi in ds.doi

        form = PublishingForm(validation=val)
        metadata = form.pub_metadata

        ## try to upload the same data with the same title again - it should work but yield a different doi
        get_doi_for_validation(val, metadata)

        val = ValidationRun.objects.get(pk=val.id)
        self.__logger.debug(val.doi)
        assert val.doi
        assert val.doi != firstdoi

        ## check that the DOI was correctly stored in the netcdf file
        with netCDF4.Dataset(val.output_file.path, mode='r') as ds:
            assert val.doi in ds.doi
示例#4
0
    def test_validation_run_expiry(self):
        assert settings.VALIDATION_EXPIRY_DAYS >= 1
        assert settings.VALIDATION_EXPIRY_WARNING_DAYS >= 1
        assert settings.VALIDATION_EXPIRY_WARNING_DAYS < settings.VALIDATION_EXPIRY_DAYS

        ## while the validation is running, we don't have an expiry date
        run = ValidationRun()
        assert run.expiry_date is None
        assert not run.is_expired
        assert not run.is_near_expiry

        ## once the validation has finished, it can expire
        run.end_time = timezone.now()
        assert run.expiry_date == (run.end_time + timedelta(days=settings.VALIDATION_EXPIRY_DAYS))
        assert not run.is_expired
        assert not run.is_near_expiry

        ## move us to the warning period
        run.end_time = timezone.now() - timedelta(days=settings.VALIDATION_EXPIRY_DAYS-settings.VALIDATION_EXPIRY_WARNING_DAYS+1)
        assert not run.is_expired
        assert run.is_near_expiry

        ## make the validation expire
        run.end_time = timezone.now() - timedelta(days=settings.VALIDATION_EXPIRY_DAYS+1)
        assert run.is_expired
        assert run.is_near_expiry

        ## once the validation has been extended, it expires based on the extension date
        run.end_time = timezone.now() - timedelta(days=7)
        run.last_extended = timezone.now()
        assert run.expiry_date == (run.last_extended + timedelta(days=settings.VALIDATION_EXPIRY_DAYS))

        ## move us to the warning period
        run.last_extended = timezone.now() - timedelta(days=settings.VALIDATION_EXPIRY_DAYS - settings.VALIDATION_EXPIRY_WARNING_DAYS+1)
        assert not run.is_expired
        assert run.is_near_expiry

        ## make the validation expire
        run.last_extended = timezone.now() - timedelta(days=settings.VALIDATION_EXPIRY_DAYS+1)
        assert run.is_expired
        assert run.is_near_expiry

        ## if a validation is archived, it does not expire
        run.archive(commit=False)
        assert run.is_archived
        assert run.expiry_date is None
        assert not run.is_expired
        assert not run.is_near_expiry

        ## if validation is un-archived, it should be extended automatically
        run.archive(unarchive=True, commit=False)
        assert not run.is_archived
        assert not run.expiry_notified
        assert run.last_extended is not None
        assert not run.is_expired
        assert not run.is_near_expiry

        ## extend again, for good measure
        run.extend_lifespan(commit=False)
        assert not run.expiry_notified
        assert run.last_extended is not None
        assert not run.is_expired
        assert not run.is_near_expiry
示例#5
0
    def test_autocleanupvalidations(self):

        ended_vals = ValidationRun.objects.filter(
            end_time__isnull=False).count()

        ## unexpired validation
        run1 = ValidationRun()
        run1.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run1.end_time = timezone.now()
        run1.user = self.testuser
        run1.save()
        runid1 = run1.id

        ## 20% of warning period has passed
        run2 = ValidationRun()
        run2.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run2.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS * 0.8)
        run2.user = self.testuser
        run2.save()
        runid2 = run2.id

        ## 80% of warning period has passed
        run3 = ValidationRun()
        run3.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run3.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS * 0.2)
        run3.user = self.testuser
        run3.save()
        runid3 = run3.id

        ## just expired validation
        run4 = ValidationRun()
        run4.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run4.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS)
        run4.user = self.testuser
        run4.save()
        runid4 = run4.id

        ## long expired validation
        run5 = ValidationRun()
        run5.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run5.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 2)
        run5.user = self.testuser
        run5.save()
        runid5 = run5.id

        # test what happens if there is no user assigned to a validation
        no_user_run = ValidationRun()
        no_user_run.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        no_user_run.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS)
        no_user_run.user = None
        no_user_run.save()
        no_user_run_id = no_user_run.id

        # test what happens if there is no user assigned to a validation, but validation has been published
        no_user_run_published = ValidationRun()
        no_user_run_published.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        no_user_run_published.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS)
        no_user_run_published.user = None
        no_user_run_published.doi = '10101/101.010'
        no_user_run_published.save()
        no_user_run_published_id = no_user_run_published.id

        ended_vals2 = ValidationRun.objects.filter(
            end_time__isnull=False).count()
        assert ended_vals + 7 == ended_vals2
        assert runid1
        assert runid2
        assert runid3
        assert runid4
        assert runid5
        assert no_user_run_id
        assert no_user_run_published_id

        # run the command
        args = []
        opts = {}
        call_command('autocleanupvalidations', *args, **opts)

        ## reload from db because the validations have been changed.
        run1 = ValidationRun.objects.get(pk=runid1)
        run2 = ValidationRun.objects.get(pk=runid2)
        run3 = ValidationRun.objects.get(pk=runid3)
        run4 = ValidationRun.objects.get(pk=runid4)
        run5 = ValidationRun.objects.get(pk=runid5)
        non_user_val = ValidationRun.objects.filter(pk=no_user_run_id)
        no_user_run_published = ValidationRun.objects.get(
            pk=no_user_run_published_id)

        ## with the last command call, the user should have been notified about most of our test validations
        ## but the validations should not have been deleted yet
        assert not run1.expiry_notified
        assert run2.expiry_notified
        assert run3.expiry_notified
        assert run4.expiry_notified
        assert run5.expiry_notified
        assert len(
            non_user_val
        ) == 0  # there should be no validation anymore, because it was already removed
        assert not no_user_run_published.expiry_notified  # no notification sent

        ## the validations may have been extended in the previous step, undo that to get them really deleted in the next call
        run1.last_extended = None
        run1.save()
        run2.last_extended = None
        run2.save()
        run3.last_extended = None
        run3.save()
        run4.last_extended = None
        run4.save()
        run5.last_extended = None
        run5.save()

        call_command('autocleanupvalidations', *args, **opts)

        ## the two expired validations should be have been deleted now
        ended_vals3 = ValidationRun.objects.filter(
            end_time__isnull=False).count()
        assert ended_vals + 4 == ended_vals3
示例#6
0
    def test_autocleanupvalidations(self):

        ended_vals = ValidationRun.objects.filter(
            end_time__isnull=False).count()

        ## unexpired validation
        run1 = ValidationRun()
        run1.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run1.end_time = timezone.now()
        run1.user = self.testuser
        run1.save()
        runid1 = run1.id

        ## 20% of warning period has passed
        run2 = ValidationRun()
        run2.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run2.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS * 0.8)
        run2.user = self.testuser
        run2.save()
        runid2 = run2.id

        ## 80% of warning period has passed
        run3 = ValidationRun()
        run3.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run3.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS -
            settings.VALIDATION_EXPIRY_WARNING_DAYS * 0.2)
        run3.user = self.testuser
        run3.save()
        runid3 = run3.id

        ## just expired validation
        run4 = ValidationRun()
        run4.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run4.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS)
        run4.user = self.testuser
        run4.save()
        runid4 = run4.id

        ## long expired validation
        run5 = ValidationRun()
        run5.start_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 4)
        run5.end_time = timezone.now() - timedelta(
            days=settings.VALIDATION_EXPIRY_DAYS * 2)
        run5.user = self.testuser
        run5.save()
        runid5 = run5.id

        ended_vals2 = ValidationRun.objects.filter(
            end_time__isnull=False).count()
        assert ended_vals + 5 == ended_vals2
        assert runid1
        assert runid2
        assert runid3
        assert runid4
        assert runid5

        # run the command
        args = []
        opts = {}
        call_command('autocleanupvalidations', *args, **opts)

        ## reload from db because the validations have been changed.
        run1 = ValidationRun.objects.get(pk=runid1)
        run2 = ValidationRun.objects.get(pk=runid2)
        run3 = ValidationRun.objects.get(pk=runid3)
        run4 = ValidationRun.objects.get(pk=runid4)
        run5 = ValidationRun.objects.get(pk=runid5)

        ## with the last command call, the user should have been notified about most of our test validations
        ## but the validations should not have been deleted yet
        assert not run1.expiry_notified
        assert run2.expiry_notified
        assert run3.expiry_notified
        assert run4.expiry_notified
        assert run5.expiry_notified

        ## the validations may have been extended in the previous step, undo that to get them really deleted in the next call
        run1.last_extended = None
        run1.save()
        run2.last_extended = None
        run2.save()
        run3.last_extended = None
        run3.save()
        run4.last_extended = None
        run4.save()
        run5.last_extended = None
        run5.save()

        call_command('autocleanupvalidations', *args, **opts)

        ## the two expired validations should be have been deleted now
        ended_vals3 = ValidationRun.objects.filter(
            end_time__isnull=False).count()
        assert ended_vals + 3 == ended_vals3