def test_dynamic_schemas_against_generated(self):
     """This test checks that previously generated schemas haven't changed
     by later modifications to the Python definitions.
     When you define new schemas or new versions of schemas, you'll need
     to run data/schemas/generate.py to get them generated.
     """
     all_schemas = load_all_worker_schemas()
     test_library = BundledSchemaLibrary(self.schemas_path, __package__)
     for ref, schema in all_schemas.items():
         assert test_library.load_schema(ref) == schema
 def test_get_worker_schemas(self, accept_json):
     """Test that all worker schemas are provided in all versions through the API."""
     for schema_ref, json_schema in load_all_worker_schemas().items():
         path = api_route_for('/schemas/component_analyses/{}/{}'.format(
             schema_ref.name, schema_ref.version))
         res = self.client.get(path, headers=accept_json)
         assert res.status_code == 200
         received_schema = res.json
         self._check_schema_id(received_schema, path)
         received_schema.pop('id')
         assert received_schema == json_schema
Example #3
0
class PublishedSchemas(ResourceWithSchema):
    API_COLLECTION = 'api'
    COMPONENT_ANALYSES_COLLECTION = 'component_analyses'
    schema_collections = {
        API_COLLECTION: build_nested_schema_dict(load_all_server_schemas()),
        COMPONENT_ANALYSES_COLLECTION: build_nested_schema_dict(load_all_worker_schemas())
    }

    def __init__(self):
        super(PublishedSchemas, self).__init__()
        for collection, schemas in self.schema_collections.items():
            for name, versions in schemas.items():
                for version, schema in versions.items():
                    url = self._get_schema_url(collection, name, version)
                    schema["id"] = url

    def get(self, collection=None, name=None, version=None):
        # Boring if statement instead of clever loop because Nick is no fun
        result = self.schema_collections
        if collection is not None:
            schema_path = [collection]
            result = self.schema_collections.get(collection)
            if result is not None and name is not None:
                schema_path.append(name)
                result = result.get(name)
                if result is not None and version is not None:
                    schema_path.append(version)
                    result = result.get(version)

        if result is None:
            raise HTTPError(404, 'Schema {} does not exist'.format('/'.join(schema_path)))
        return result

    @classmethod
    def _get_schema_url(cls, collection, name, version):
        return rest_api_v1.url_for(cls, collection=collection, name=name,
                                   version=version, _external=True)

    @classmethod
    def get_api_schema_url(cls, name, version):
        return cls._get_schema_url(collection=cls.API_COLLECTION, name=name, version=version)

    @classmethod
    def get_component_analysis_schema_url(cls, name, version):
        return cls._get_schema_url(collection=cls.COMPONENT_ANALYSES_COLLECTION,
                                   name=name, version=version)
#!/usr/bin/env python3
from collections import OrderedDict
import json
import os
import sys

if sys.version_info[0] < 3:
    print(
        'Must be run under Python 3, since Python 2 adds trailing whitespaces to JSON'
    )
    sys.exit(1)

from f8a_worker.schemas import load_all_worker_schemas

here = os.path.dirname(__file__)

for ref, schema in load_all_worker_schemas().items():
    # we don't want to overwrite previously generated schemas
    fname = os.path.join(here, '{}-v{}.schema.json'.format(*ref))
    if os.path.exists(fname):
        print('{} already exists, skipping'.format(fname))
        continue
    if 'definitions' in schema:
        definitions = schema['definitions'].items()
        schema['definitions'] = OrderedDict(sorted(definitions))
    # write schema
    with open(fname, 'w') as f:
        json.dump(schema, f, indent=4)