class CharacterMapper(Mapper): __type__ = Character id = field.Integer(read_only=True) name = field.String() created_at = field.DateTime(read_only=True)
class MyMapper(Mapper): __type__ = MyObject id = field.Integer() name = field.String() __roles__ = {'name_only': ['name']}
class PlanetMapper(Mapper): __type__ = Planet id = field.Integer(read_only=True) name = field.String() description = field.String() created_at = field.DateTime(read_only=True)
def test_marshal_read_only_collection(): f = field.Collection(field.Integer(), name='post_ids', read_only=True) output = {} data = {'post_ids': [2, 1]} mapper_session = get_mapper_session(data=data, output=output) f.marshal(mapper_session) assert output == {}
def test_serialize_flat_collection(): f = field.Collection(field.Integer(), name='post_ids', source='posts') output = {} data = {'posts': [2, 1]} mapper_session = get_mapper_session(obj=data, output=output) f.serialize(mapper_session) assert output == {'post_ids': [2, 1]}
def test_marshal_collection_requires_list(): f = field.Collection(field.Integer(), name='post_ids') output = {} mapper_session = get_mapper_session(data={'post_ids': 1}, output=output) with pytest.raises(field.FieldInvalid): f.marshal(mapper_session)
class UserMapper(Mapper): __type__ = User id = field.Integer(read_only=True) name = field.String() is_admin = field.Boolean(required=False, default=False) company = field.Nested(CompanyMapper, getter=company_getter) __roles__ = { 'overview': whitelist('id', 'name') }
class SchedulableMapper(PolymorphicMapper): __type__ = TestType id = field.Integer(read_only=True) name = field.String() object_type = field.String(choices=['event', 'task'], read_only=True) __mapper_args__ = { 'polymorphic_on': object_type, 'allow_polymorphic_marshal': True, } __roles__ = { 'name_only': ['name'], 'public': whitelist('name', 'id'), }
def test_collection_proxies_name_to_wrapped_field(): f = field.Collection(field.Integer(), name='post_ids') f2 = field.Collection(field.String(), name='test') with pytest.raises(field.FieldError): field.Collection(field.String(name='foo')) class UserMapper(Mapper): __type__ = TestType id = field.String(required=True) tags = field.Collection(field.String()) assert f.name == 'post_ids' assert f2.name == 'test' mapper = UserMapper({}) assert mapper.fields['tags'].name == 'tags'
class UserMapper(Mapper): __type__ = User id = field.Integer(read_only=True) name = field.String()
class Outer(Mapper): __type__ = dict user = field.Nested(Inner, source='__self__', allow_create=True) status = field.Integer()
class BaseMapper(Mapper): id = field.Integer(read_only=True) created_at = field.DateTime() updated_at = field.DateTime()
class CompanyMapper(Mapper): __type__ = Company id = field.Integer(read_only=True) name = field.String()