def test_no_required_fields(self): class BandSchema(Schema): drummer = fields.Str() bassist = fields.Str() res = swagger.schema2jsonschema(BandSchema) assert 'required' not in res
def test_field2property_nested(self): category = fields.Nested(CategorySchema) assert swagger.field2property(category) == swagger.schema2jsonschema( CategorySchema) cat_with_ref = fields.Nested(CategorySchema, ref='Category') assert swagger.field2property(cat_with_ref) == {'$ref': 'Category'}
def test_required_fields(self): class BandSchema(Schema): drummer = fields.Str(required=True) bassist = fields.Str() res = swagger.schema2jsonschema(BandSchema) assert res['required'] == ['drummer']
def _schema_or_ref(schema): schema_class = _get_class(schema) ref = next( (ref_name for ref_schema, ref_name in spec.plugins['smore.ext.marshmallow'] ['refs'].items() if schema_class is _get_class(ref_schema)), None, ) return _format_ref(ref) if ref else swagger.schema2jsonschema(schema)
def test_title_and_description_may_be_added(self): class UserSchema(Schema): class Meta: title = 'User' description = 'A registered user' res = swagger.schema2jsonschema(UserSchema) assert res['description'] == 'A registered user' assert res['title'] == 'User'
def _schema_or_ref(schema): schema_class = _get_class(schema) ref = next( ( ref_name for ref_schema, ref_name in spec.plugins['smore.ext.marshmallow']['refs'].items() if schema_class is _get_class(ref_schema) ), None, ) return _format_ref(ref) if ref else swagger.schema2jsonschema(schema)
def test_excluded_fields(self): class WhiteStripesSchema(Schema): class Meta: exclude = ('bassist', ) guitarist = fields.Str() drummer = fields.Str() bassist = fields.Str() res = swagger.schema2jsonschema(WhiteStripesSchema) assert set(res['properties'].keys()) == set(['guitarist', 'drummer'])
def test_field2property_nested_many(self): categories = fields.Nested(CategorySchema, many=True) res = swagger.field2property(categories) assert res['type'] == 'array' assert res['items'] == swagger.schema2jsonschema(CategorySchema) cats_with_ref = fields.Nested(CategorySchema, many=True, ref='Category') res = swagger.field2property(cats_with_ref) assert res['type'] == 'array' assert res['items'] == {'$ref': 'Category'}
def test_schema2jsonschema_with_explicit_fields(self): class UserSchema(Schema): _id = fields.Int() email = fields.Email(description='email address of the user') name = fields.Str() class Meta: title = 'User' res = swagger.schema2jsonschema(UserSchema) assert res['title'] == 'User' props = res['properties'] assert props['_id']['type'] == 'integer' assert props['email']['type'] == 'string' assert props['email']['format'] == 'email' assert props['email']['description'] == 'email address of the user'
def test_only_explicitly_declared_fields_are_translated(self, recwarn): class UserSchema(Schema): _id = fields.Int() class Meta: title = 'User' fields = ( '_id', 'email', ) res = swagger.schema2jsonschema(UserSchema) props = res['properties'] assert '_id' in props assert 'email' not in props warning = recwarn.pop() expected_msg = 'Only explicitly-declared fields will be included in the Schema Object.' assert expected_msg in str(warning.message) assert issubclass(warning.category, UserWarning)
def test_schema_in_docstring(self, spec): def pet_view(): """Not much to see here. --- get: responses: 200: schema: smore.apispec.tests.schemas.PetSchema description: successful operation """ return '...' spec.add_path(path='/pet', view=pet_view) p = spec._paths['/pet'] assert 'get' in p op = p['get'] assert 'responses' in op assert op['responses'][200]['schema'] == swagger.schema2jsonschema( PetSchema) assert op['responses'][200]['description'] == 'successful operation'
def test_responses(self, schemas, path): response = path['get']['responses']['default'] assert response['description'] == 'a band' assert response['schema'] == swagger.schema2jsonschema(schemas.BandSchema)
def test_schema2jsonschema_with_nested_fields(self): res = swagger.schema2jsonschema(PetSchema, use_refs=False) props = res['properties'] assert props['category']['items'] == swagger.schema2jsonschema( CategorySchema)
spec = APISpec( title='Pets', version='0.1', plugins=['smore.ext.marshmallow'], ) spec.definition('Category', schema=CategorySchema) spec.add_path( view=None, path='/category/{category_id}', operations={ 'get': { 'responses': { 200: { 'schema': swagger.schema2jsonschema(PetSchema), 'description': 'A pet', }, }, 'parameters': [ { 'name': 'q', 'in': 'query', 'type': 'string' }, { 'name': 'category_id', 'in': 'path', 'type': 'string' }, arg2parameter(Arg(str, multiple=True, location='querystring')),