Пример #1
0
def test_generate_unmarshall_method_bodies():
    class OneFieldSchema(Schema):
        foo = fields.Integer()

    context = JitContext(is_serializing=False, use_inliners=False)
    result = generate_method_bodies(OneFieldSchema(), context)
    expected = '''\
def InstanceSerializer(obj):
    res = {}
    __res_get = res.get
    res["foo"] = _field_foo__deserialize(obj.foo, "foo", obj)
    if __res_get("foo", res) is None:
        raise ValueError()
    return res
def DictSerializer(obj):
    res = {}
    __res_get = res.get
    if "foo" in obj:
        res["foo"] = _field_foo__deserialize(obj["foo"], "foo", obj)
    if __res_get("foo", res) is None:
        raise ValueError()
    return res
def HybridSerializer(obj):
    res = {}
    __res_get = res.get
    try:
        value = obj["foo"]
    except (KeyError, AttributeError, IndexError, TypeError):
        value = obj.foo
    res["foo"] = _field_foo__deserialize(value, "foo", obj)
    if __res_get("foo", res) is None:
        raise ValueError()
    return res'''
    assert expected == result
Пример #2
0
def test_generate_marshall_method_bodies():
    class OneFieldSchema(Schema):
        foo = fields.Integer()

    context = JitContext()
    result = generate_method_bodies(OneFieldSchema(), context)
    expected = '''\
def InstanceSerializer(obj):
    res = {}
    value = obj.foo; value = value() if callable(value) else value; \
res["foo"] = _field_foo__serialize(value, "foo", obj)
    return res
def DictSerializer(obj):
    res = {}
    if "foo" in obj:
        value = obj["foo"]; value = value() if callable(value) else value; \
res["foo"] = _field_foo__serialize(value, "foo", obj)
    return res
def HybridSerializer(obj):
    res = {}
    try:
        value = obj["foo"]
    except (KeyError, AttributeError, IndexError, TypeError):
        value = obj.foo
    value = value; value = value() if callable(value) else value; \
res["foo"] = _field_foo__serialize(value, "foo", obj)
    return res'''
    assert expected == result