def test_default_to_missing(self): u = User('Foo', email=None) marshal = fields.Marshaller() res = marshal(u, {'name': fields.String(), 'email': fields.Email(default=fields.missing)}) assert res['name'] == u.name assert 'email' not in res
def __init__(self, obj=None, extra=None, only=None, exclude=None, prefix='', strict=False, many=False, skip_missing=False, context=None): # copy declared fields from metaclass self.declared_fields = copy.deepcopy(self._declared_fields) self._data = None # the cached, serialized data self.many = many self.opts = self.OPTIONS_CLASS(self.Meta) self.only = only or () self.exclude = exclude or () self.prefix = prefix self.strict = strict or self.opts.strict self.skip_missing = skip_missing or self.opts.skip_missing self.ordered = self.opts.ordered #: Dictionary mapping field_names -> :class:`Field` objects self.fields = self.dict_class() #: Callable marshalling object self._marshal = fields.Marshaller(prefix=self.prefix) #: Callable unmarshalling object self._unmarshal = fields.Unmarshaller() self.extra = extra self.context = context or {} self._update_fields(many=many) # For backwards compatibility, allow object to be passed in. self.obj = obj self._data = None # If object is passed in, marshal it immediately so that errors are stored if self.obj is not None: warnings.warn( 'Serializing objects in the Schema constructor is a ' 'deprecated API. Use the Schema.dump method instead.', category=DeprecationWarning) self._update_fields(self.obj, many=many) self._update_data() else: self._update_fields(many=many)
def __init__(self, obj=None, extra=None, only=None, exclude=None, prefix='', strict=False, many=False, context=None): if not many and utils.is_collection(obj): warnings.warn( 'Implicit collection handling is deprecated. Set ' 'many=True to serialize a collection.', category=DeprecationWarning) # copy declared fields from metaclass self.declared_fields = copy.deepcopy(self._declared_fields) self.fields = OrderedDict() self.__data = None self.obj = obj self.many = many self.opts = self.OPTIONS_CLASS(self.Meta) self.only = only or () self.exclude = exclude or () self.prefix = prefix self.strict = strict or self.opts.strict #: Callable marshalling object self.marshal = fields.Marshaller(prefix=self.prefix, strict=self.strict) self.extra = extra self.context = context if isinstance(obj, types.GeneratorType): self.obj = list(obj) else: self.obj = obj self._update_fields(obj) # If object is passed in, marshal it immediately so that errors are stored if self.obj is not None: raw_data = self.marshal(self.obj, self.fields, many=self.many) if self.extra: raw_data.update(self.extra) self.__data = self.process_data(raw_data)
def test_marshalling_generator(self): gen = (u for u in [User("Foo"), User("Bar")]) marshal = fields.Marshaller() res = marshal(gen, {"name": fields.String()}, many=True) assert len(res) == 2
def test_prefix(self): u = User("Foo", email="*****@*****.**") marshal = fields.Marshaller(prefix='usr_') result = marshal(u, {"email": fields.Email(), 'name': fields.String()}) assert result['usr_name'] == u.name assert result['usr_email'] == u.email
def test_strict_mode_raises_errors(self): u = User("Foo", email="foobar") marshal = fields.Marshaller(strict=True) assert_raises(MarshallingError, lambda: marshal(u, {"email": fields.Email()}))
def test_stores_errors(self): u = User("Foo", email="foobar") marshal = fields.Marshaller() marshal(u, {"email": fields.Email()}) assert_in("email", marshal.errors)
def test_strict_mode_raises_errors(self): u = User("Foo", email="foobar") marshal = fields.Marshaller(strict=True) with pytest.raises(MarshallingError): marshal(u, {"email": fields.Email()})