Esempio n. 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
Esempio n. 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
Esempio n. 3
0
def test_jitted_unmarshal_method(schema):
    context = JitContext()
    unmarshal_method = generate_unmarshall_method(schema, context=context)
    result = unmarshal_method({'foo': 32, 'bar': 'Hello', 'meh': 'Foo'})
    expected = {'bar': u'Hello', '@#': 32, 'meh': 'Foo'}
    assert expected == result

    assert not hasattr(unmarshal_method, 'proxy')
Esempio n. 4
0
def test_non_primitive_num_type_schema(non_primitive_num_type_schema):
    context = JitContext()
    marshall_method = generate_marshall_method(non_primitive_num_type_schema,
                                               threshold=1,
                                               context=context)
    result = marshall_method({})
    expected = {}
    assert expected == result
Esempio n. 5
0
def test_jitted_marshal_method(schema):
    context = JitContext()
    marshal_method = generate_marshall_method(schema,
                                              threshold=1,
                                              context=context)
    result = marshal_method({'@#': 32, 'bar': 'Hello', 'meh': 'Foo'})
    expected = {'bar': u'Hello', 'foo': 32, 'raz': 'Hello!'}
    assert expected == result
    # Test specialization
    result = marshal_method({'@#': 32, 'bar': 'Hello', 'meh': 'Foo'})
    assert expected == result
    assert marshal_method.proxy._call == marshal_method.proxy.dict_serializer
Esempio n. 6
0
def test_generate_unmarshall_method_bodies_required():
    class OneFieldSchema(Schema):
        foo = fields.Integer(required=True)

    context = JitContext(is_serializing=False, use_inliners=False)
    result = str(
        generate_transform_method_body(OneFieldSchema(),
                                       DictSerializer(context), context))
    expected = '''\
def DictSerializer(obj):
    res = {}
    __res_get = res.get
    res["foo"] = _field_foo__deserialize(obj["foo"], "foo", obj)
    if "foo" not in res:
        raise ValueError()
    if __res_get("foo", res) is None:
        raise ValueError()
    return res'''
    assert expected == result
Esempio n. 7
0
def test_generate_unmarshall_method_bodies_with_load_from():
    class OneFieldSchema(Schema):
        foo = fields.Integer(data_key='bar', allow_none=True)

    context = JitContext(is_serializing=False, use_inliners=False)
    result = str(
        generate_transform_method_body(OneFieldSchema(),
                                       DictSerializer(context), context))
    expected = '''\
def DictSerializer(obj):
    res = {}
    __res_get = res.get
    if "foo" in obj:
        res["foo"] = _field_foo__deserialize(obj["foo"], "bar", obj)
    if "foo" not in res:
        if "bar" in obj:
            res["foo"] = _field_foo__deserialize(obj["bar"], "bar", obj)
    return res'''
    assert expected == result
Esempio n. 8
0
def test_generate_marshall_method_body(schema):
    expected_start = '''\
def InstanceSerializer(obj):
    res = dict_class()
'''
    raz_assignment = ('value = None; '
                      'value = value() if callable(value) else value; '
                      'res["raz"] = _field_raz__serialize(value, "raz", obj)')

    foo_assignment = ('if "@#" in obj:\n'
                      '        value = obj["@#"]; '
                      'value = value() if callable(value) else value; '
                      'value = int(value) if value is not None else None; '
                      'res["foo"] = value')
    bar_assignment = (
        'value = obj.bar; '
        'value = value() if callable(value) else value; '
        'value = {text_type}(value) if value is not None else None; '
        'res["bar"] = value').format(text_type=text_type.__name__)
    blargh_assignment = (
        'value = obj.blargh; '
        'value = value() if callable(value) else value; '
        'value = ((value in __blargh_truthy) or '
        '(False if value in __blargh_falsy else bool(value))) '
        'if value is not None else None; '
        'res["blargh"] = value')

    context = JitContext()
    result = str(
        generate_marshall_method_body(schema, InstanceSerializer(), context))
    assert result.startswith(expected_start)
    assert raz_assignment in result
    assert foo_assignment in result
    assert bar_assignment in result
    assert blargh_assignment in result
    assert 'meh' not in result
    assert result.endswith('return res')