def __schema__(self): schema = self['__schema__'] if isinstance(schema, flask_marshmallow.Schema): return fields2jsonschema(schema.fields) elif isinstance(schema, flask_marshmallow.base_fields.FieldABC): return field2property(schema) raise NotImplementedError()
def test_formatted_field_translates_to_array(self): field = fields.List(fields.String) res = swagger.field2property(field) assert res['type'] == 'array' assert res['items'] == swagger.field2property(fields.String())
def test_field2property_type(self, FieldClass, jsontype): field = FieldClass() res = swagger.field2property(field) assert res['type'] == jsontype
def test_field_with_additional_metadata(self): field = fields.Str(minLength=6, maxLength=100) res = swagger.field2property(field) assert res['maxLength'] == 100 assert res['minLength'] == 6
def test_field_with_default_load(self): field = fields.Str(default='foo', missing='bar') res = swagger.field2property(field, dump=False) assert res['default'] == 'bar'
def test_field_with_missing(self): field = fields.Str(default='foo', missing='bar') res = swagger.field2property(field) assert res['default'] == 'bar'
def test_field2property_nested_self_without_name_raises_error(self, spec): self_nesting = fields.Nested('self') with pytest.raises(ValueError): swagger.field2property(self_nesting)
def test_field2property_nested_many_spec(self, spec): spec.definition('Category', schema=CategorySchema) category = fields.Nested(CategorySchema, many=True) ret = swagger.field2property(category, spec=spec) assert ret['type'] == 'array' assert ret['items'] == {'$ref': '#/definitions/Category'}
def test_field2property_nested_self_many(self, spec): self_nesting = fields.Nested('self', many=True) res = swagger.field2property(self_nesting, name='Foo') assert res == {'type': 'array', 'items': {'$ref': '#/definitions/Foo'}}
def test_field_with_choices(self): field = fields.Str( validate=validate.OneOf(['freddie', 'brian', 'john'])) res = swagger.field2property(field) assert set(res['enum']) == {'freddie', 'brian', 'john'}
def test_field_with_boolean_false_default_load(self): field = fields.Boolean(default=None, missing=False) res = swagger.field2property(field, dump=False) assert res['default'] is False
def test_nested_field_with_property(self, spec): category_1 = fields.Nested(CategorySchema) category_2 = fields.Nested(CategorySchema, ref='#/definitions/Category') category_3 = fields.Nested(CategorySchema, dump_only=True) category_4 = fields.Nested(CategorySchema, dump_only=True, ref='#/definitions/Category') category_5 = fields.Nested(CategorySchema, many=True) category_6 = fields.Nested(CategorySchema, many=True, ref='#/definitions/Category') category_7 = fields.Nested(CategorySchema, many=True, dump_only=True) category_8 = fields.Nested(CategorySchema, many=True, dump_only=True, ref='#/definitions/Category') spec.definition('Category', schema=CategorySchema) assert swagger.field2property(category_1, spec=spec) == { '$ref': '#/definitions/Category' } assert swagger.field2property(category_2, spec=spec) == { '$ref': '#/definitions/Category' } assert swagger.field2property(category_3, spec=spec) == { 'allOf': [{ '$ref': '#/definitions/Category' }], 'readOnly': True } assert swagger.field2property(category_4, spec=spec) == { 'allOf': [{ '$ref': '#/definitions/Category' }], 'readOnly': True } assert swagger.field2property(category_5, spec=spec) == { 'items': { '$ref': '#/definitions/Category' }, 'type': 'array' } assert swagger.field2property(category_6, spec=spec) == { 'items': { '$ref': '#/definitions/Category' }, 'type': 'array' } assert swagger.field2property(category_7, spec=spec) == { 'items': { '$ref': '#/definitions/Category' }, 'readOnly': True, 'type': 'array' } assert swagger.field2property(category_8, spec=spec) == { 'items': { '$ref': '#/definitions/Category' }, 'readOnly': True, 'type': 'array' }
def test_field_with_allow_none(self): field = fields.Str(allow_none=True) res = swagger.field2property(field) assert res['x-nullable'] is True
def test_field_with_equal(self): field = fields.Str(validate=validate.Equal('only choice')) res = swagger.field2property(field) assert res['enum'] == ['only choice']
def test_field2property_nested_spec_metadatas(self, spec): spec.definition('Category', schema=CategorySchema) category = fields.Nested(CategorySchema, description="A category") result = swagger.field2property(category, spec=spec) assert result == {'$ref': '#/definitions/Category', 'description': 'A category'}
def test_field2property_nested_spec(self, spec): spec.definition('Category', schema=CategorySchema) category = fields.Nested(CategorySchema) assert swagger.field2property(category, spec=spec) == {'$ref': '#/definitions/Category'}
def test_field2property_formats(self, FieldClass, expected_format): field = FieldClass() res = swagger.field2property(field) assert res['format'] == expected_format
def test_field2property_nested_ref(self, spec): 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_field2property_nested_dump_only(self, spec): category = fields.Nested(CategorySchema) res = swagger.field2property(category, name='Foo', dump=False) props = res['properties'] assert 'breed' not in props
def test_field2property_nested_self(self, spec): self_nesting = fields.Nested('self') res = swagger.field2property(self_nesting, name='Foo') assert res == {'$ref': '#/definitions/Foo'}
def test_field_with_description(self): field = fields.Str(description='a username') res = swagger.field2property(field) assert res['description'] == 'a username'