Пример #1
0
        class MySchema(Schema):
            foo = fields.Field()

            @post_load(pass_many=True, pass_original=True)
            def post_load(self, data, many, original):
                if many:
                    ret = []
                    for item, orig_item in zip(data, original):
                        item["_post_load"] = orig_item["sentinel"]
                        ret.append(item)
                else:
                    ret = data.copy()
                    ret["_post_load"] = original["sentinel"]
                return ret

            @post_dump(pass_many=True, pass_original=True)
            def post_dump(self, data, many, original):
                if many:
                    ret = []
                    for item, orig_item in zip(data, original):
                        item["_post_dump"] = orig_item["sentinel"]
                        ret.append(item)
                else:
                    ret = data.copy()
                    ret["_post_dump"] = original["sentinel"]
                return ret
Пример #2
0
def test_serializing_named_tuple():
    Point = namedtuple("Point", ["x", "y"])

    field = fields.Field()

    p = Point(x=4, y=2)

    assert field.serialize("x", p) == 4
Пример #3
0
 def test_repr(self):
     default = "œ∑´"
     field = fields.Field(default=default, attribute=None)
     assert repr(field) == ("<fields.Field(default={0!r}, attribute=None, "
                            "validate=None, required=False, "
                            "load_only=False, dump_only=False, "
                            "missing={missing}, allow_none=False, "
                            "error_messages={error_messages})>".format(
                                default,
                                missing=missing,
                                error_messages=field.error_messages))
     int_field = fields.Integer(validate=lambda x: True)
     assert "<fields.Integer" in repr(int_field)
Пример #4
0
        class MySchema(Schema):
            foo = fields.Field()

            @post_load(pass_original=True)
            def post_load(self, data, input_data):
                ret = data.copy()
                ret["_post_load"] = input_data["sentinel"]
                return ret

            @post_dump(pass_original=True)
            def post_dump(self, data, obj):
                ret = data.copy()
                ret["_post_dump"] = obj["sentinel"]
                return ret
Пример #5
0
 def test_error_raised_if_missing_is_set_on_required_field(self):
     with pytest.raises(
             ValueError,
             match="'missing' must not be set for required fields"):
         fields.Field(required=True, missing=42)
Пример #6
0
 def test_error_raised_if_uncallable_validator_passed(self):
     with pytest.raises(ValueError, match="must be a callable"):
         fields.Field(validate="notcallable")
Пример #7
0
 class MySchema(Schema):
     foo = fields.Field()
     bar = fields.List(fields.Str())
     baz = fields.Tuple([fields.Str(), fields.Int()])
Пример #8
0
 def test_serialize_does_not_apply_validators(self, user):
     field = fields.Field(validate=lambda x: False)
     # No validation error raised
     assert field.serialize("age", user) == user.age