Exemple #1
0
def test_root_schema(app):
    """Test valid usage of the RootSchema API."""
    with app.app_context():
        version = 0
        for json_schema in root_schemas_json_schemas:
            RootSchema.create_new_version(
                version=version,
                json_schema=json_schema,
            )
            version += 1
        db.session.commit()

    with app.app_context():
        version = 0
        for json_schema in root_schemas_json_schemas:
            root_schema = RootSchema.get_root_schema(version)
            assert root_schema.version == version
            assert json.loads(root_schema.json_schema) == json_schema
            version += 1
Exemple #2
0
def test_root_schemas_backward_compatibility(app):
    """Test non backward compatible root schemas."""
    with app.app_context():
        version = 0
        for json_schema in root_schemas_json_schemas:
            RootSchema.create_new_version(
                version=version,
                json_schema=json_schema,
            )
            version += 1
        # try creating a new not backward compatbile version of the root schema
        for json_schema in backward_incompatible_root_schemas_json_schemas:
            with pytest.raises(JSONSchemaCompatibilityError):
                RootSchema.create_new_version(
                    version=version,
                    json_schema=json_schema,
                )
        db.session.commit()

    # check that no invalid schema was created
    with app.app_context():
        with pytest.raises(RootSchemaDoesNotExistError):
            RootSchema.get_root_schema(len(root_schemas_json_schemas) + 1)
Exemple #3
0
def test_root_schema_errors(app):
    """Test invalid usage of the RootSchema API."""
    with app.app_context():
        # creating first root schema with a version != 0 should fail
        with pytest.raises(InvalidRootSchemaError):
            RootSchema.create_new_version(
                version=42,
                json_schema=root_schemas_json_schemas[0],
            )
        # test creating a root schema with a negative version when no schema
        # exist
        with pytest.raises(InvalidRootSchemaError):
            RootSchema.create_new_version(
                version=-1,
                json_schema=root_schemas_json_schemas[0],
            )
        # test skipping a version when creating a root schema
        RootSchema.create_new_version(
            version=0,
            json_schema=root_schemas_json_schemas[0],
        )
        with pytest.raises(InvalidRootSchemaError):
            RootSchema.create_new_version(
                version=2,
                json_schema=root_schemas_json_schemas[0],
            )
        # test creating a root schema with a negative version when one schema
        # exists
        with pytest.raises(InvalidRootSchemaError):
            RootSchema.create_new_version(
                version=-1,
                json_schema=root_schemas_json_schemas[0],
            )
        # test creating a root schema with an already used version
        with pytest.raises(InvalidRootSchemaError):
            RootSchema.create_new_version(
                version=0,
                json_schema=root_schemas_json_schemas[0],
            )
        # test creating a root schema with no JSON Schema
        with pytest.raises(InvalidJSONSchemaError):
            RootSchema.create_new_version(
                version=1,
                json_schema=None,
            )
        with pytest.raises(InvalidJSONSchemaError):
            RootSchema.create_new_version(
                version=1,
                json_schema={},
            )
        with pytest.raises(InvalidJSONSchemaError):
            RootSchema.create_new_version(
                version=1,
                json_schema={'$schema': 'invalid-url'},
            )
        with pytest.raises(InvalidJSONSchemaError):
            RootSchema.create_new_version(
                version=1,
                json_schema={'$schema': 'http://examples.com'},
            )
        # try getting a non existing root schema
        with pytest.raises(RootSchemaDoesNotExistError):
            RootSchema.get_root_schema(42)
Exemple #4
0
def test_community_schema(app, flask_http_responses):
    """Test valid usage of the CommunitySchema API."""
    with app.app_context():
        new_community = Community.create_community(**communities_metadata[0])

        db.session.commit()

        BlockSchemaRef = namedtuple('BlockSchemaRef',
                                    ['block_schema', 'versions'])
        BlockSchemaVersionRef = namedtuple('BlockSchemaVersionRef',
                                           ['version', 'url'])

        # create root schemas
        root_schemas = [
            RootSchema.create_new_version(
                version=version,
                json_schema=root_schemas_json_schemas[version],
            ) for version in range(len(root_schemas_json_schemas))
        ]
        # create block schemas
        block_schemas = []
        for block_index in range(len(block_schemas_json_schemas)):
            block_schema = BlockSchema.create_block_schema(
                community_id=new_community.id,
                name="schema {}".format(block_index))
            block_schemas.append(
                BlockSchemaRef(
                    block_schema=block_schema,
                    versions=[
                        BlockSchemaVersionRef(
                            version=block_schema.create_version(
                                json_schema=block_schemas_json_schemas[
                                    block_index][version], ),
                            url=url_for(
                                'b2share_schemas.block_schema_versions_item',
                                schema_id=block_schema.id,
                                schema_version_nb=version,
                                _external=True,
                            )) for version in range(
                                len(block_schemas_json_schemas[block_index]))
                    ]))
        community_schemas = []
        # create a community schema
        community_schema_v1_json_schema = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'type': 'object',
            'properties': {
                str(block_schemas[0].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[0].versions[0].url),
                },
                str(block_schemas[1].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[1].versions[0].url),
                },
            },
            'additionalProperties': False,
        }
        community_schemas.append(
            CommunitySchema.create_version(
                community_id=new_community.id,
                root_schema_version=root_schemas[0].version,
                community_schema=community_schema_v1_json_schema))

        # test with another block schema version
        community_schema_v2_json_schema = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'type': 'object',
            'properties': {
                str(block_schemas[0].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[0].versions[1].url),
                },
                str(block_schemas[1].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[1].versions[0].url),
                },
            },
            'additionalProperties': False,
        }
        community_schemas.append(
            CommunitySchema.create_version(
                community_id=new_community.id,
                root_schema_version=root_schemas[0].version,
                community_schema=community_schema_v2_json_schema))

        # test with another root schema
        community_schemas.append(
            CommunitySchema.create_version(
                community_id=new_community.id,
                root_schema_version=root_schemas[1].version,
                community_schema=community_schema_v2_json_schema))

        db.session.commit()
        # create a metadata blcok matching each community schema
        metadatas = [{
            'authors': ['C. Arthur', 'D. Albert'],
            'community_specific': {
                str(block_schemas[0].block_schema.id): {
                    'experiment_nb': 42,
                },
                str(block_schemas[1].block_schema.id): {
                    'analysis_result': 'success',
                },
            }
        }, {
            'authors': ['C. Arthur', 'D. Albert'],
            'community_specific': {
                str(block_schemas[0].block_schema.id): {
                    'experiment_nb': 42,
                    'experiment_date': '4242'
                }
            }
        }, {
            'authors': ['C. Arthur', 'D. Albert'],
            'files': ['/path/to/the/file.txt'],
            'community_specific': {
                str(block_schemas[0].block_schema.id): {
                    'experiment_nb': 42,
                    'experiment_date': '4242'
                }
            }
        }]

        validation_schemas = [
            schema.build_json_schema() for schema in community_schemas
        ]
        for index in range(len(community_schemas)):
            with flask_http_responses():
                # check that the community schema validates the corresponding
                # JSON metadata
                jsonschema.validate(metadatas[index],
                                    validation_schemas[index])
                for index2 in range(len(community_schemas)):
                    if index != index2:
                        with pytest.raises(
                                jsonschema.exceptions.ValidationError):
                            # check that the community schema does not validate
                            # the others JSON metadata
                            jsonschema.validate(metadatas[index2],
                                                validation_schemas[index])