Here are the examples of the python api flask_restplus.fields.String taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
76 Examples
3
Example 1
Project: flask-restplus Source File: test_fields_mask.py
def test_mask_error_on_list_field(self):
model = {
'nested': fields.List(fields.String)
}
with self.assertRaises(mask.MaskError):
mask.apply(model, 'nested{notpossible}')
3
Example 2
Project: flask-restplus Source File: test_model.py
def test_model_with_discriminator(self):
model = Model('Person', {
'name': fields.String(discriminator=True),
'age': fields.Integer,
})
self.assertEqual(model.__schema__, {
'properties': {
'name': {'type': 'string'},
'age': {'type': 'integer'},
},
'discriminator': 'name',
'required': ['name'],
'type': 'object'
})
3
Example 3
Project: flask-restplus Source File: test_fields.py
def test_discriminator_output(self):
model = self.api.model('Test', {
'name': fields.String(discriminator=True),
})
data = self.api.marshal({}, model)
assert_equal(data, {'name': 'Test'})
3
Example 4
Project: flask-restplus Source File: test_fields.py
def test_as_list_is_reusable(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, as_list=True)
assert_equal(field.__schema__, {'type': 'array', 'items': {'$ref': '#/definitions/NestedModel'}})
field = fields.Nested(nested_fields)
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel'})
3
Example 5
Project: flask-restplus Source File: test_fields.py
def test_with_allow_null(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, allow_null=True)
assert not field.required
assert field.allow_null
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel'})
3
Example 6
Project: flask-restplus Source File: test_fields.py
def test_multiple_discriminator_field(self):
model = self.api.model('Test', {
'name': fields.String(discriminator=True),
'name2': fields.String(discriminator=True),
})
with assert_raises(ValueError):
self.api.marshal(object(), model)
3
Example 7
Project: flask-restplus Source File: test_fields.py
def test_with_scoped_attribute_on_dict_or_obj(self):
class Test(object):
def __init__(self, data):
self.data = data
class Nested(object):
def __init__(self, value):
self.value = value
nesteds = [Nested(i) for i in ['a', 'b', 'c']]
test_obj = Test(nesteds)
test_dict = {'data': [{'value': 'a'}, {'value': 'b'}, {'value': 'c'}]}
field = fields.List(fields.String(attribute='value'), attribute='data')
assert_equal(['a', 'b', 'c'], field.output('whatever', test_obj))
assert_equal(['a', 'b', 'c'], field.output('whatever', test_dict))
3
Example 8
Project: flask-restplus Source File: test_fields_mask.py
def test_raise_400_on_invalid_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
})
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
pass
with self.app.test_client() as client:
response = client.get('/test/', headers={'X-Fields': 'name{,missing}'})
self.assertEqual(response.status_code, 400)
self.assertEquals(response.content_type, 'application/json')
3
Example 9
Project: flask-restplus Source File: test_fields.py
def test_with_nested_field(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.List(fields.Nested(nested_fields))
assert_equal(field.__schema__, {'type': 'array', 'items': {'$ref': '#/definitions/NestedModel'}})
data = [{'name': 'John Doe', 'age': 42}, {'name': 'Jane Doe', 'age': 66}]
expected = [OrderedDict([('name', 'John Doe')]), OrderedDict([('name', 'Jane Doe')])]
self.assert_field(field, data, expected)
3
Example 10
Project: flask-restplus Source File: test_fields.py
def test_with_required(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, required=True)
assert field.required
assert not field.allow_null
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel'})
3
Example 11
Project: flask-restplus Source File: test_fields_mask.py
def test_mask_error_on_simple_fields(self):
model = {
'name': fields.String,
}
with self.assertRaises(mask.MaskError):
mask.apply(model, 'name{notpossible}')
3
Example 12
Project: flask-restplus Source File: test_reqparse.py
def test_parse_model(self):
model = Model('Todo', {
'task': fields.String(required=True)
})
parser = RequestParser()
parser.add_argument('todo', type=model, required=True)
data = {'todo': {'task': 'aaa'}}
with self.app.test_request_context('/', method='post',
data=json.dumps(data),
content_type='application/json'):
args = parser.parse_args()
self.assertEqual(args['todo'], {'task': 'aaa'})
3
Example 13
Project: flask-restplus Source File: test_model.py
def test_model_with_discriminator_override_require(self):
model = Model('Person', {
'name': fields.String(discriminator=True, required=False),
'age': fields.Integer,
})
self.assertEqual(model.__schema__, {
'properties': {
'name': {'type': 'string'},
'age': {'type': 'integer'},
},
'discriminator': 'name',
'required': ['name'],
'type': 'object'
})
3
Example 14
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_expose_default_model_mask_header(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
}, mask='{name,age}')
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
pass
specs = self.get_specs()
definition = specs['definitions']['Test']
self.assertIn('x-mask', definition)
self.assertEqual(definition['x-mask'], '{name,age}')
3
Example 15
Project: flask-restplus Source File: test_reqparse.py
def test_models(self):
# app = Flask(__name__)
# api = Api(app)
todo_fields = Model('Todo', {
'task': fields.String(required=True, description='The task details')
})
parser = RequestParser()
parser.add_argument('todo', type=todo_fields)
self.assertEqual(parser.__schema__, [{
'name': 'todo',
'type': 'Todo',
'in': 'body',
}])
3
Example 16
Project: flask-restplus Source File: test_fields_mask.py
def test_list_fields_with_simple_field(self):
family_fields = {
'name': fields.String,
'members': fields.List(fields.String)
}
result = mask.apply(family_fields, 'members')
self.assertEqual(set(result.keys()), set(['members']))
self.assertIsInstance(result['members'], fields.List)
self.assertIsInstance(result['members'].container, fields.String)
data = {'name': 'Doe', 'members': ['John', 'Jane']}
expected = {'members': ['John', 'Jane']}
self.assertDataEqual(marshal(data, result), expected)
# Should leave th original mask untouched
self.assertDataEqual(marshal(data, family_fields), data)
0
Example 17
Project: flask-restplus Source File: test_fields_mask.py
def test_list_fields_with_nested_inherited(self):
api = Api(self.app)
person = api.model('Person', {
'name': fields.String,
'age': fields.Integer
})
child = api.inherit('Child', person, {
'attr': fields.String
})
family = api.model('Family', {
'children': fields.List(fields.Nested(child))
})
result = mask.apply(family.resolved, 'children{name,attr}')
data = {'children': [
{'name': 'John', 'age': 5, 'attr': 'value-john'},
{'name': 'Jane', 'age': 42, 'attr': 'value-jane'},
]}
expected = {'children': [
{'name': 'John', 'attr': 'value-john'},
{'name': 'Jane', 'attr': 'value-jane'},
]}
self.assertDataEqual(marshal(data, result), expected)
# Should leave th original mask untouched
self.assertDataEqual(marshal(data, family), data)
0
Example 18
def test_defaults(self):
field = fields.List(fields.String)
assert not field.required
assert_equal(field.__schema__, {'type': 'array', 'items': {'type': 'string'}})
0
Example 19
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_field_mask_header(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
})
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/', headers={
'X-Fields': '{name,age}'
})
self.assertEqual(data, {
'name': 'John Doe',
'age': 42,
})
0
Example 20
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_complex_field_mask_header(self):
api = Api(self.app)
person = api.model('Person', person_fields)
child = api.inherit('Child', person, {
'attr': fields.String
})
family = api.model('Family', {
'father': fields.Nested(person),
'mother': fields.Nested(person),
'children': fields.List(fields.Nested(child)),
'free': fields.List(fields.Raw),
})
house = api.model('House', {
'family': fields.Nested(family, attribute='people')
})
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(house)
def get(self):
return {'people': {
'father': {'name': 'John', 'age': 42},
'mother': {'name': 'Jane', 'age': 42},
'children': [
{'name': 'Jack', 'age': 5, 'attr': 'value-1'},
{'name': 'Julie', 'age': 7, 'attr': 'value-2'},
],
'free': [
{'key-1': '1-1', 'key-2': '1-2'},
{'key-1': '2-1', 'key-2': '2-2'},
]
}}
data = self.get_json('/test/', headers={
'X-Fields': 'family{father{name},mother{age},children{name,attr},free{key-2}}'
})
expected = {'family': {
'father': {'name': 'John'},
'mother': {'age': 42},
'children': [{'name': 'Jack', 'attr': 'value-1'}, {'name': 'Julie', 'attr': 'value-2'}],
'free': [{'key-2': '1-2'}, {'key-2': '2-2'}]
}}
self.assertEqual(data, expected)
0
Example 21
def test_defaults(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields)
assert not field.required
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel'})
0
Example 22
Project: flask-restplus Source File: test_fields.py
def test_min_items(self):
field = fields.List(fields.String, min_items=5)
assert_in('minItems', field.__schema__)
assert_equal(field.__schema__['minItems'], 5)
0
Example 23
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_default_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
})
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model, mask='{name,age}')
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/')
self.assertEqual(data, {
'name': 'John Doe',
'age': 42,
})
0
Example 24
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_header_field_mask_with_default_model_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
}, mask='{name,age}')
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/', headers={
'X-Fields': '{name}'
})
self.assertEqual(data, {
'name': 'John Doe',
})
0
Example 25
def test_unique(self):
field = fields.List(fields.String, unique=True)
assert_in('uniqueItems', field.__schema__)
assert_equal(field.__schema__['uniqueItems'], True)
0
Example 26
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_header_field_mask_with_default_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
})
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model, mask='{name,age}')
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/', headers={
'X-Fields': '{name}'
})
self.assertEqual(data, {
'name': 'John Doe',
})
0
Example 27
Project: flask-restplus Source File: test_fields.py
def test_string_field_with_discriminator(self):
field = fields.String(discriminator=True)
assert field.discriminator
assert field.required
assert_equal(field.__schema__, {'type': 'string'})
0
Example 28
def test_with_description(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, description='A description')
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel', 'description': 'A description'})
0
Example 29
Project: flask-restplus Source File: test_fields.py
def test_with_set(self):
field = fields.List(fields.String)
value = set(['a', 'b', 'c'])
output = field.output('foo', {'foo': value})
assert_equal(set(output), value)
0
Example 30
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_header_field_mask_with_default_mask_and_default_model_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
}, mask='{name,boolean}')
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model, mask='{name,age}')
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/', headers={
'X-Fields': '{name}'
})
self.assertEqual(data, {
'name': 'John Doe',
})
0
Example 31
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_field_mask_list(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
})
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return [{
'name': 'John Doe',
'age': 42,
'boolean': True
}, {
'name': 'Jane Doe',
'age': 33,
'boolean': False
}]
data = self.get_json('/test/', headers={
'X-Fields': '{name,age}'
})
self.assertEqual(data, [{
'name': 'John Doe',
'age': 42,
}, {
'name': 'Jane Doe',
'age': 33,
}])
0
Example 32
Project: flask-restplus Source File: test_fields.py
def test_polymorph_field(self):
parent = self.api.model('Person', {
'name': fields.String,
})
child1 = self.api.inherit('Child1', parent, {
'extra1': fields.String,
})
child2 = self.api.inherit('Child2', parent, {
'extra2': fields.String,
})
class Child1(object):
name = 'child1'
extra1 = 'extra1'
class Child2(object):
name = 'child2'
extra2 = 'extra2'
mapping = {
Child1: child1,
Child2: child2
}
thing = self.api.model('Thing', {
'owner': fields.Polymorph(mapping),
})
def data(cls):
return self.api.marshal({'owner': cls()}, thing)
assert_equal(data(Child1), {'owner': {
'name': 'child1',
'extra1': 'extra1'
}})
assert_equal(data(Child2), {'owner': {
'name': 'child2',
'extra2': 'extra2'
}})
0
Example 33
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_honour_field_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
})
data = {
'name': 'John Doe',
'age': 42,
'boolean': True
}
result = api.marshal(data, model, mask='{name,age}')
self.assertEqual(result, {
'name': 'John Doe',
'age': 42,
})
0
Example 34
def test_with_default(self):
field = fields.String(default='aaa')
assert_equal(field.__schema__, {'type': 'string', 'default': 'aaa'})
0
Example 35
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_default_model_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
}, mask='{name,age}')
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/')
self.assertEqual(data, {
'name': 'John Doe',
'age': 42,
})
0
Example 36
def test_max_items(self):
field = fields.List(fields.String, max_items=42)
assert_in('maxItems', field.__schema__)
assert_equal(field.__schema__['maxItems'], 42)
0
Example 37
Project: flask-restplus Source File: test_fields_mask.py
def test_marshal_with_honour_header_default_mask_with_default_model_mask(self):
api = Api(self.app)
model = api.model('Test', {
'name': fields.String,
'age': fields.Integer,
'boolean': fields.Boolean,
}, mask='{name,boolean}')
@api.route('/test/')
class TestResource(Resource):
@api.marshal_with(model, mask='{name}')
def get(self):
return {
'name': 'John Doe',
'age': 42,
'boolean': True
}
data = self.get_json('/test/')
self.assertEqual(data, {
'name': 'John Doe',
})
0
Example 38
def test_with_title(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, title='A title')
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel', 'title': 'A title'})
0
Example 39
Project: flask-restplus Source File: test_fields.py
def test_polymorph_field_no_common_ancestor(self):
child1 = self.api.model('Child1', {
'extra1': fields.String,
})
child2 = self.api.model('Child2', {
'extra2': fields.String,
})
class Child1(object):
pass
class Child2(object):
pass
mapping = {
Child1: child1,
Child2: child2
}
with assert_raises(ValueError):
fields.Polymorph(mapping)
0
Example 40
Project: flask-restplus Source File: test_fields.py
def test_with_enum(self):
enum = ['A', 'B', 'C']
field = fields.String(enum=enum)
assert not field.required
assert_equal(field.__schema__, {'type': 'string', 'enum': enum, 'example': enum[0]})
0
Example 41
Project: flask-restplus Source File: test_fields.py
def test_string_field_with_discriminator_override_require(self):
field = fields.String(discriminator=True, required=False)
assert field.discriminator
assert field.required
assert_equal(field.__schema__, {'type': 'string'})
0
Example 42
Project: flask-restplus Source File: test_fields.py
def test_polymorph_field_unknown_class(self):
parent = self.api.model('Person', {
'name': fields.String,
})
child1 = self.api.inherit('Child1', parent, {
'extra1': fields.String,
})
child2 = self.api.inherit('Child2', parent, {
'extra2': fields.String,
})
class Child1(object):
name = 'child1'
extra1 = 'extra1'
class Child2(object):
name = 'child2'
extra2 = 'extra2'
mapping = {
Child1: child1,
Child2: child2
}
thing = self.api.model('Thing', {
'owner': fields.Polymorph(mapping),
})
with assert_raises(ValueError):
self.api.marshal({'owner': object()}, thing)
0
Example 43
Project: flask-restplus Source File: test_fields.py
def test_polymorph_field_ambiguous_mapping(self):
parent = self.api.model('Parent', {
'name': fields.String,
})
child = self.api.inherit('Child', parent, {
'extra': fields.String,
})
class Parent(object):
name = 'parent'
class Child(Parent):
extra = 'extra'
mapping = {
Parent: parent,
Child: child
}
thing = self.api.model('Thing', {
'owner': fields.Polymorph(mapping),
})
with assert_raises(ValueError):
self.api.marshal({'owner': Child()}, thing)
0
Example 44
Project: flask-restplus Source File: test_fields.py
def test_polymorph_field_required_default(self):
parent = self.api.model('Person', {
'name': fields.String,
})
child1 = self.api.inherit('Child1', parent, {
'extra1': fields.String,
})
child2 = self.api.inherit('Child2', parent, {
'extra2': fields.String,
})
class Child1(object):
name = 'child1'
extra1 = 'extra1'
class Child2(object):
name = 'child2'
extra2 = 'extra2'
mapping = {
Child1: child1,
Child2: child2
}
thing = self.api.model('Thing', {
'owner': fields.Polymorph(mapping, required=True, default={'name': 'default'}),
})
data = self.api.marshal({}, thing)
assert_equal(data, {'owner': {
'name': 'default'
}})
0
Example 45
Project: flask-restplus Source File: test_fields.py
def test_with_readonly(self):
api = Api(self.app)
nested_fields = api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, readonly=True)
assert_equal(field.__schema__, {'$ref': '#/definitions/NestedModel', 'readOnly': True})
0
Example 46
Project: flask-restplus Source File: test_fields.py
def test_polymorph_field_not_required(self):
parent = self.api.model('Person', {
'name': fields.String,
})
child1 = self.api.inherit('Child1', parent, {
'extra1': fields.String,
})
child2 = self.api.inherit('Child2', parent, {
'extra2': fields.String,
})
class Child1(object):
name = 'child1'
extra1 = 'extra1'
class Child2(object):
name = 'child2'
extra2 = 'extra2'
mapping = {
Child1: child1,
Child2: child2
}
thing = self.api.model('Thing', {
'owner': fields.Polymorph(mapping),
})
data = self.api.marshal({}, thing)
assert_equal(data, {'owner': None})
0
Example 47
def test_defaults(self):
field = fields.String()
assert not field.required
assert not field.discriminator
assert_equal(field.__schema__, {'type': 'string'})
0
Example 48
Project: flask-restplus Source File: test_fields.py
def test_with_callable_enum(self):
enum = lambda: ['A', 'B', 'C'] # noqa
field = fields.String(enum=enum)
assert not field.required
assert_equal(field.__schema__, {'type': 'string', 'enum': ['A', 'B', 'C'], 'example': 'A'})
0
Example 49
def test_as_list(self):
nested_fields = self.api.model('NestedModel', {'name': fields.String})
field = fields.Nested(nested_fields, as_list=True)
assert field.as_list
assert_equal(field.__schema__, {'type': 'array', 'items': {'$ref': '#/definitions/NestedModel'}})
0
Example 50
Project: flask-restplus Source File: test_fields.py
def test_polymorph_with_discriminator(self):
parent = self.api.model('Person', {
'name': fields.String,
'model': fields.String(discriminator=True),
})
child1 = self.api.inherit('Child1', parent, {
'extra1': fields.String,
})
child2 = self.api.inherit('Child2', parent, {
'extra2': fields.String,
})
class Child1(object):
name = 'child1'
extra1 = 'extra1'
class Child2(object):
name = 'child2'
extra2 = 'extra2'
mapping = {
Child1: child1,
Child2: child2
}
thing = self.api.model('Thing', {
'owner': fields.Polymorph(mapping),
})
def data(cls):
return self.api.marshal({'owner': cls()}, thing)
assert_equal(data(Child1), {'owner': {
'name': 'child1',
'model': 'Child1',
'extra1': 'extra1'
}})
assert_equal(data(Child2), {'owner': {
'name': 'child2',
'model': 'Child2',
'extra2': 'extra2'
}})