Beispiel #1
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')
def test_instance_serializer():
    serializer = InstanceSerializer()
    field = fields.Integer()
    assert 'result["foo"] = obj.foo' == str(
        serializer.serialize('foo', 'bar', 'result["foo"] = {0}', field))