def test_marshal_list_of_lists(self): model = OrderedDict([('foo', fields.Raw), ('fee', fields.List(fields.List(fields.String)))]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', [['fye'], ['fum']])]) output = marshal(marshal_fields, model) expected = OrderedDict([('foo', 'bar'), ('fee', [['fye'], ['fum']])]) assert output == expected
def test_mask_error_on_list_field(self): model = { 'nested': fields.List(fields.String) } with pytest.raises(mask.MaskError): mask.apply(model, 'nested{notpossible}')
def test_list_fields_with_nested_inherited(self, app): api = Api(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'}, ]} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family), data)
def test_list_fields_with_nested(self): family_fields = {'members': fields.List(fields.Nested(person_fields))} result = mask.apply(family_fields, 'members{name}') assert set(result.keys()) == set(['members']) assert isinstance(result['members'], fields.List) assert isinstance(result['members'].container, fields.Nested) assert set(result['members'].container.nested.keys()) == set(['name']) data = { 'members': [ { 'name': 'John', 'age': 42 }, { 'name': 'Jane', 'age': 42 }, ] } expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_model_as_dict_with_list(self): model = Model( 'Person', { 'name': fields.String, 'age': fields.Integer, 'tags': fields.List(fields.String), }) assert model.__schema__ == { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'tags': { 'type': 'array', 'items': { 'type': 'string' } } }, 'type': 'object' }
def test_with_nested_field(self, api): nested_fields = api.model('NestedModel', {'name': fields.String}) field = fields.List(fields.Nested(nested_fields)) assert 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)
def test_marshal_with_honour_complex_field_mask_header(self, app, client): api = Api(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 = client.get_json('/test/', headers={ 'X-Fields': 'family{father{name},mother{age},children{name,attr},free{key-2}}' }) assert data == {'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'}] }}
def test_defaults(self): field = fields.List(fields.String) assert not field.required assert field.__schema__ == { 'type': 'array', 'items': { 'type': 'string' } }
def test_list_of_raw(self): field = fields.List(fields.Raw) data = [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}] expected = [OrderedDict([('a', 1), ('b', 1)]), OrderedDict([('a', 2), ('b', 1)]), OrderedDict([('a', 3), ('b', 1)])] self.assert_field(field, data, expected) data = [1, 2, 'a'] self.assert_field(field, data, data)
def test_marshal_list_of_nesteds(self): model = OrderedDict([('foo', fields.Raw), ('fee', fields.List(fields.Nested({'fye': fields.String})))]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', { 'fye': 'fum' })]) output = marshal(marshal_fields, model) expected = OrderedDict([('foo', 'bar'), ('fee', [OrderedDict([('fye', 'fum')])])]) assert output == expected
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 ['a' == 'b', 'c'], field.output('whatever', test_obj) assert ['a' == 'b', 'c'], field.output('whatever', test_dict)
def test_list_fields_with_raw(self): family_fields = { 'members': fields.List(fields.Raw) } result = mask.apply(family_fields, 'members{name}') data = {'members': [ {'name': 'John', 'age': 42}, {'name': 'Jane', 'age': 42}, ]} expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_list_fields_with_simple_field(self): family_fields = { 'name': fields.String, 'members': fields.List(fields.String) } result = mask.apply(family_fields, 'members') assert set(result.keys()) == set(['members']) assert isinstance(result['members'], fields.List) assert isinstance(result['members'].container, fields.String) data = {'name': 'Doe', 'members': ['John', 'Jane']} expected = {'members': ['John', 'Jane']} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_model_as_nested_dict_with_list(self): address = Model('Address', { 'road': fields.String, }) person = Model( 'Person', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, 'addresses': fields.List(fields.Nested(address)) }) assert person.__schema__ == { # 'required': ['address'], 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'addresses': { 'type': 'array', 'items': { '$ref': '#/definitions/Address', } } }, 'type': 'object' } assert address.__schema__ == { 'properties': { 'road': { 'type': 'string' }, }, 'type': 'object' }
def test_with_attribute(self): data = [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}] field = fields.List(fields.Integer(attribute='a')) self.assert_field(field, data, [1, 2, 3])
import pytest from faker import Faker from sanic_restplus import marshal, fields fake = Faker() person_fields = {'name': fields.String, 'age': fields.Integer} family_fields = { 'father': fields.Nested(person_fields), 'mother': fields.Nested(person_fields), 'children': fields.List(fields.Nested(person_fields)) } def person(): return {'name': fake.name(), 'age': fake.pyint()} def family(): return { 'father': person(), 'mother': person(), 'children': [person(), person()] } def marshal_simple(): return marshal(person(), person_fields)
def test_value(self, value, expected): self.assert_field(fields.List(fields.String()), value, expected)
def test_max_items(self): field = fields.List(fields.String, max_items=42) assert 'maxItems' in field.__schema__ assert field.__schema__['maxItems'] == 42
def test_min_items(self): field = fields.List(fields.String, min_items=5) assert 'minItems' in field.__schema__ assert field.__schema__['minItems'] == 5
def test_unique(self): field = fields.List(fields.String, unique=True) assert 'uniqueItems' in field.__schema__ assert field.__schema__['uniqueItems'] is True
def test_with_set(self): field = fields.List(fields.String) value = set(['a', 'b', 'c']) output = field.output('foo', {'foo': value}) assert set(output) == value