示例#1
0
    def _add_schema(name,
                    deposit_schema=None,
                    is_indexed=True,
                    use_deposit_as_record=True,
                    version="1.0.0",
                    **kwargs):
        """
        Add new schema into db
        """
        default_json = {'title': {'type': 'string'}}

        try:
            schema = Schema.get(name, version)
        except JSONSchemaNotFound:
            schema = Schema(name=name,
                            version=version,
                            is_indexed=is_indexed,
                            use_deposit_as_record=use_deposit_as_record,
                            deposit_schema=deposit_schema or default_json,
                            **kwargs)
            db.session.add(schema)
            db.session.commit()

            if not schema.experiment:
                schema.add_read_access_for_all_users()
                db.session.commit()

        return schema
示例#2
0
def add_schema_from_fixture(data=None):
    """Add or update schema."""
    allow_all = data.pop("allow_all", False)
    name = data['name']

    try:
        with db.session.begin_nested():
            with db.session.begin_nested():
                try:
                    schema = Schema.get(name=data['name'],
                                        version=data['version'])
                    click.secho('{} already exist in the db.'.format(
                        str(name)))
                    return

                except JSONSchemaNotFound:
                    schema = Schema(**data)
                    db.session.add(schema)

            if allow_all:
                schema.add_read_access_for_all_users()

    except IntegrityError:
        click.secho('Error occured during adding {} to the db. \n'.format(
            str(name)),
                    fg='red')
        return

    db.session.commit()
    click.secho('{} has been added.'.format(str(name)), fg='green')
示例#3
0
def test_get(db):
    schema = Schema(name='test-schema')
    db.session.add(schema)
    db.session.add(Schema(name='test-schema', version='2.0.0'))
    db.session.add(Schema(name='another-schema', version='1.0.1'))
    db.session.commit()

    assert Schema.get('test-schema', '1.0.0') == schema
def test_add_schema_from_fixture_when_schema_does_not_exist_create_new_one(
        app, users):  # noqa
    data = dict(name='new-schema',
                version='1.2.3',
                experiment='CMS',
                fullname='New fullname',
                deposit_schema={'title': 'deposit_schema'},
                deposit_options={'title': 'deposit_options'},
                record_schema={'title': 'record_schema'},
                record_options={'title': 'record_options'},
                record_mapping={
                    'mappings': {
                        'doc': {
                            'properties': {
                                "title": {
                                    "type": "text"
                                }
                            }
                        }
                    }
                },
                deposit_mapping={
                    'mappings': {
                        'doc': {
                            'properties': {
                                "keyword": {
                                    "type": "keyword"
                                }
                            }
                        }
                    }
                },
                is_indexed=True,
                use_deposit_as_record=False)

    add_schema_from_json(data=data)

    schema = Schema.get('new-schema', version='1.2.3')
    for key, value in data.items():
        assert getattr(schema, key) == value

    with app.test_request_context():
        login_user(users['cms_user'])
        assert ReadSchemaPermission(schema).can()
        assert not AdminSchemaPermission(schema).can()

        login_user(users['lhcb_user'])
        assert not ReadSchemaPermission(schema).can()
        assert not AdminSchemaPermission(schema).can()
def test_add_schema_from_fixture_when_schema_already_exist_updates_json_for_schema(
        db):
    updated_data = dict(name='new-schema',
                        version='1.1.1',
                        experiment='LHCb',
                        fullname='New fullname',
                        deposit_schema={'title': 'deposit_schema'},
                        deposit_options={'title': 'deposit_options'},
                        record_schema={'title': 'record_schema'},
                        record_options={'title': 'record_options'},
                        record_mapping={
                            'mappings': {
                                'doc': {
                                    'properties': {
                                        "title": {
                                            "type": "text"
                                        }
                                    }
                                }
                            }
                        },
                        deposit_mapping={
                            'mappings': {
                                'doc': {
                                    'properties': {
                                        "keyword": {
                                            "type": "keyword"
                                        }
                                    }
                                }
                            }
                        },
                        is_indexed=True,
                        use_deposit_as_record=False)
    db.session.add(
        Schema(**{
            'name': 'new-schema',
            'experiment': 'CMS',
            'fullname': 'Old Schema',
        }))
    db.session.commit()

    add_schema_from_json(data=updated_data)

    schema = Schema.get('new-schema', version='1.1.1')
    for key, value in updated_data.items():
        assert getattr(schema, key) == value
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')
示例#7
0
def test_get_when_schema_doesnt_exist_raises_JSONSchemaNotFound(db):
    with raises(JSONSchemaNotFound):
        Schema.get('non-existing', '1.0.0')