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_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))
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_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) )
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, ), ]
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), ]
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 ), ]
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
def test_compile_non_strict_missing_requiredness(parse): struct_ast = parse('struct Foo { 1: string param }') assert StructTypeSpec.compile(struct_ast, require_requiredness=False)