Exemplo n.º 1
0
def test_get_latest_version_of_schema(db):
    latest_schema = Schema(name='my-schema', version='2.4.3')
    db.session.add(Schema(name='my-schema', version='0.6.6'))
    db.session.add(Schema(name='my-schema', version='1.5.5'))
    db.session.add(Schema(name='my-schema', version='2.3.4'))
    db.session.add(Schema(name='my-schema', version='2.4.0'))
    db.session.add(Schema(name='different-schema', version='3.5.4'))
    db.session.add(latest_schema)
    db.session.commit()

    assert Schema.get_latest('my-schema') == latest_schema
def build_cadi_deposit(cadi_id, cadi_info):
    """Build CMS analysis deposit, based on CADI entry."""
    schema = Schema.get_latest('deposits/records/cms-analysis').fullpath

    return {
        '$schema': schema,
        'cadi_info': cadi_info,
        'general_title': cadi_id,
        'basic_info': {
            'cadi_id': cadi_id
        }
    }
def test_get_latest_version_of_schema(db):
    schemas = [
        {'name': 'name', 'major': 0, 'minor': 6, 'patch': 6},
        {'name': 'name', 'major': 1, 'minor': 5, 'patch': 5},
        {'name': 'name', 'major': 2, 'minor': 3, 'patch': 4},
        {'name': 'name', 'major': 2, 'minor': 4, 'patch': 0},
        {'name': 'name', 'major': 2, 'minor': 4, 'patch': 3},
    ]

    for x in schemas:
        db.session.add(Schema(**x))
    db.session.commit()
    latest = Schema.get_latest(name='name')

    assert latest.version == "2.4.3"
Exemplo n.º 4
0
    def _preprocess_data(cls, data):

        # data can be sent without specifying particular version of schema,
        # but just with a type, e.g. cms-analysis
        # this be resolved to the last version of deposit schema of this type
        if '$ana_type' in data:
            try:
                schema = Schema.get_latest('deposits/records/{}'.format(
                    data['$ana_type']))
            except JSONSchemaNotFound:
                raise DepositValidationError(
                    'Schema {} is not a valid deposit schema.'.format(
                        data['$ana_type']))

            data['$schema'] = schema.fullpath
            data.pop('$ana_type')

        return data
Exemplo n.º 5
0
def test_add_schemas_with_url_with_replace(app, db, cli_runner):
    schema = Schema(
        name='test',
        experiment='CMS',
        version='1.0.0',
        deposit_schema={
            'type': 'object',
            'properties': {
                'title': {'type': 'string'}
            }
        })
    db.session.add(schema)
    db.session.commit()

    responses.add(
        responses.GET,
        'http://schemas.org/my-schema.json',
        json={
            'version': '1.0.0',
            'name': 'test',
            'use_deposit_as_record': True,
            'deposit_schema': {
                'properties': {
                    '_buckets': {},
                    '_deposit': {},
                    '_files': {},
                    '_experiment': {},
                    '_fetched_from': {},
                    '_user_edited': {},
                    '_access': {},
                    'title': {'type': 'string'},
                    'abstract': {'type': 'string'}
                }
            }
        },
        status=200
    )

    res = cli_runner('fixtures schemas --url http://schemas.org/my-schema.json -r')
    assert res.exit_code == 0
    assert 'test has been added.' in res.output

    schema = Schema.get_latest('test')
    assert 'abstract' in schema.deposit_schema['properties'].keys()
    def _preprocess_data(cls, data):

        # data can be sent without specifying particular version of schema,
        # but just with a type, e.g. cms-analysis
        # this be resolved to the last version of deposit schema of this type
        if '$ana_type' in data:
            try:
                schema = Schema.get_latest(
                    'deposits/records/{}'.format(data['$ana_type'])
                )
            except JSONSchemaNotFound:
                raise DepositValidationError(
                    'Schema {} is not a valid deposit schema.'
                    .format(data['$ana_type']))

            data['$schema'] = schema.fullpath
            data.pop('$ana_type')

        return data
def add_schema_from_json(data, replace=None, force_version=None):
    """Add or update schema, using a json file."""
    allow_all = data.pop("allow_all", False)
    version = data['version']
    name = data['name']

    try:
        with db.session.begin_nested():
            try:
                _schema = Schema.get(name=name, version=version)
                if replace:
                    _schema.update(**data)
                    db.session.add(_schema)
                else:
                    click.secho(f'{name}-{version} already exists in the db.')
                    return
            except JSONSchemaNotFound:
                try:
                    _schema = Schema.get_latest(name=name)
                    if is_later_version(_schema.version,
                                        version) and not force_version:
                        click.secho(
                            f'A later version ({_schema.version}) of '
                            f'{name} is already in the db. Error while '
                            f'adding {name}-{version}',
                            fg='red')
                        return
                    else:
                        _schema = Schema(**data)
                        db.session.add(_schema)
                except JSONSchemaNotFound:
                    _schema = Schema(**data)
                    db.session.add(_schema)

        # throws error if you try to re-insert the same action roles
        if allow_all and not replace:
            _schema.add_read_access_for_all_users()

        db.session.commit()
        click.secho(f'{name} has been added.', fg='green')
    except IntegrityError:
        click.secho(f'An db error occurred while adding {name}.', fg='red')
Exemplo n.º 8
0
    def _get_schema_needs(self, deposit):
        """Create deposit permissions are based on schema's permissions."""
        if '$schema' in deposit:
            try:
                schema = resolve_schema_by_url(deposit['$schema'])
            except JSONSchemaNotFound:
                raise WrongJSONSchemaError('Schema {} doesnt exist.'.format(
                    deposit['$schema']))

        elif '$ana_type' in deposit:
            try:
                schema = Schema.get_latest(deposit['$ana_type'])
            except JSONSchemaNotFound:
                raise WrongJSONSchemaError(
                    'Schema with name {} doesnt exist.'.format(
                        deposit['$ana_type']))

        else:
            raise WrongJSONSchemaError(
                'You have to specify either $schema or $ana_type')

        return ReadSchemaPermission(schema).needs
    def _get_schema_needs(self, deposit):
        """Create deposit permissions are based on schema's permissions."""
        if '$schema' in deposit:
            try:
                schema = Schema.get_by_fullpath(deposit['$schema'])
            except SchemaDoesNotExist:
                raise WrongJSONSchemaError('Schema {} doesnt exist.'.
                                           format(deposit['$schema']))

        elif '$ana_type' in deposit:
            try:
                schema = Schema.get_latest(
                    'deposits/records/{}'.format(deposit['$ana_type'])
                )
            except SchemaDoesNotExist:
                raise WrongJSONSchemaError('Schema with name {} doesnt exist.'.
                                           format(deposit['$ana_type']))

        else:
            raise WrongJSONSchemaError(
                'You have to specify either $schema or $ana_type')

        return ReadSchemaPermission(schema).needs
def test_get_latest_version_of_schema_when_schema_with_given_name_doesnt_exist_raises_JSONSchemaNotFound(db):
    with raises(JSONSchemaNotFound):
        Schema.get_latest(name='non-existing')
def test_get_latest_version_of_schema_when_schema_with_given_name_doesnt_exist_raises_SchemaDoesNotExist(db):
    with raises(SchemaDoesNotExist):
        Schema.get_latest(name='non-existing')