Beispiel #1
0
def test_compile_missing_field_id(parse):
    struct_ast = parse('struct Foo { required string param }')

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert 'Please specify the numeric ID for the field' in str(exc_info)
def test_compile_missing_field_id(parse):
    struct_ast = parse('struct Foo { required string param }')

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert 'Please specify the numeric ID for the field' in str(exc_info)
Beispiel #3
0
def test_link_simple(scope):
    spec = StructTypeSpec('SimpleStruct',
                          fields=[
                              FieldSpec(1,
                                        'a',
                                        spec=prim_spec.TextTypeSpec,
                                        required=True),
                              FieldSpec(2,
                                        'b',
                                        spec=prim_spec.BinaryTypeSpec,
                                        required=False),
                              FieldSpec(3,
                                        'c',
                                        spec=prim_spec.I32TypeSpec,
                                        required=True),
                              FieldSpec(4,
                                        'd',
                                        spec=prim_spec.I64TypeSpec,
                                        required=False),
                          ])
    spec = spec.link(scope)

    SimpleStruct = spec.surface
    assert SimpleStruct.type_spec is spec
    assert 'SimpleStruct(a, c, b=None, d=None)' in SimpleStruct.__doc__
Beispiel #4
0
def test_compile_missing_requiredness(parse):
    struct_ast = parse('struct Foo { 1: string param }')

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert ('Please specify whether the field is optional or required'
            in str(exc_info))
Beispiel #5
0
def test_compile_duplicate_field_ids(parse):
    struct_ast = parse(
        'struct Foo { 1: required string x; 1: optional i32 y; }')

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert 'Field ID "1" of struct "Foo"' in str(exc_info)
    assert 'has already been used' in str(exc_info)
Beispiel #6
0
def test_compile_duplicate_fields(parse):
    struct_ast = parse(
        'struct Foo { 1: required string x; 2: optional i32 x }')

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert 'Field "x" of struct "Foo"' in str(exc_info)
    assert 'has duplicates' in str(exc_info)
def test_compile_duplicate_field_ids(parse):
    struct_ast = parse(
        'struct Foo { 1: required string x; 1: optional i32 y; }'
    )

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert 'Field ID "1" of struct "Foo"' in str(exc_info)
    assert 'has already been used' in str(exc_info)
def test_compile_duplicate_fields(parse):
    struct_ast = parse(
        'struct Foo { 1: required string x; 2: optional i32 x }'
    )

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert 'Field "x" of struct "Foo"' in str(exc_info)
    assert 'has duplicates' in str(exc_info)
def test_compile_missing_requiredness(parse):
    struct_ast = parse('struct Foo { 1: string param }')

    with pytest.raises(ThriftCompilerError) as exc_info:
        StructTypeSpec.compile(struct_ast)

    assert (
        'Please specify whether the field is optional or required'
        in str(exc_info)
    )
Beispiel #10
0
def test_link_simple(scope):
    spec = StructTypeSpec('SimpleStruct', fields=[
        FieldSpec(1, 'a', spec=prim_spec.TextTypeSpec, required=True),
        FieldSpec(2, 'b', spec=prim_spec.BinaryTypeSpec, required=False),
        FieldSpec(3, 'c', spec=prim_spec.I32TypeSpec, required=True),
        FieldSpec(4, 'd', spec=prim_spec.I64TypeSpec, required=False),
    ])
    spec = spec.link(scope)

    SimpleStruct = spec.surface
    assert SimpleStruct.type_spec is spec
Beispiel #11
0
def test_compile_reference(parse):
    struct_ast = parse('''struct RefStruct {
        1: optional string x;
        2: required SomeCustomType y;
        3: optional AnotherCustomType z;
    }''')

    spec = StructTypeSpec.compile(struct_ast)

    assert spec.name == 'RefStruct'
    assert spec.fields == [
        FieldSpec(id=1, name='x', spec=prim_spec.TextTypeSpec, required=False),
        FieldSpec(
            id=2,
            name='y',
            spec=TypeReference('SomeCustomType', 3),
            required=True,
        ),
        FieldSpec(
            id=3,
            name='z',
            spec=TypeReference('AnotherCustomType', 4),
            required=False,
        ),
    ]
Beispiel #12
0
def test_compile_primitives(parse):
    struct_ast = parse('''struct PrimitiveStruct {
        1: required string x;
        2: optional i32 y;
        3: required double z;
    }''')

    spec = StructTypeSpec.compile(struct_ast)

    assert spec.name == 'PrimitiveStruct'
    assert spec.fields == [
        FieldSpec(id=1, name='x', spec=prim_spec.TextTypeSpec, required=True),
        FieldSpec(id=2, name='y', spec=prim_spec.I32TypeSpec, required=False),
        FieldSpec(id=3, name='z', spec=prim_spec.DoubleTypeSpec,
                  required=True),
    ]
Beispiel #13
0
def test_compile_primitives(parse):
    struct_ast = parse('''struct PrimitiveStruct {
        1: required string x;
        2: optional i32 y;
        3: required double z;
    }''')

    spec = StructTypeSpec.compile(struct_ast)

    assert spec.name == 'PrimitiveStruct'
    assert spec.fields == [
        FieldSpec(id=1, name='x', spec=prim_spec.TextTypeSpec, required=True),
        FieldSpec(id=2, name='y', spec=prim_spec.I32TypeSpec, required=False),
        FieldSpec(
            id=3, name='z', spec=prim_spec.DoubleTypeSpec, required=True
        ),
    ]
Beispiel #14
0
def test_compile_reference(parse):
    struct_ast = parse('''struct RefStruct {
        1: optional string x;
        2: required SomeCustomType y;
        3: optional AnotherCustomType z;
    }''')

    spec = StructTypeSpec.compile(struct_ast)

    assert spec.name == 'RefStruct'
    assert spec.fields == [
        FieldSpec(id=1, name='x', spec=prim_spec.TextTypeSpec, required=False),
        FieldSpec(
            id=2, name='y', spec=TypeReference('SomeCustomType', 3),
            required=True,
        ),
        FieldSpec(
            id=3, name='z', spec=TypeReference('AnotherCustomType', 4),
            required=False,
        ),
    ]
Beispiel #15
0
def sstruct(fields=""):
    """This helper creates a mock spec for sstruct """
    struct_ast = parse_struct('struct RefStruct { %s }' % fields)
    spec = StructTypeSpec.compile(struct_ast)
    spec.link(Scope("test"))
    return spec
Beispiel #16
0
def sstruct(fields=""):
    """This helper creates a mock spec for sstruct """
    struct_ast = parse_struct('struct RefStruct { %s }' % fields)
    spec = StructTypeSpec.compile(struct_ast)
    spec.link(Scope("test"))
    return spec
Beispiel #17
0
def test_compile_non_strict_missing_requiredness(parse):
    struct_ast = parse('struct Foo { 1: string param }')

    assert StructTypeSpec.compile(struct_ast, require_requiredness=False)
Beispiel #18
0
def test_compile_non_strict_missing_requiredness(parse):
    struct_ast = parse('struct Foo { 1: string param }')

    assert StructTypeSpec.compile(struct_ast, require_requiredness=False)