Beispiel #1
0
def test_e_duplicate_field_id_or_name():
    with pytest.raises(ThriftGrammerError) as excinfo:
        load('parser-cases/e_duplicate_field_id.thrift')
    assert 'field identifier/name has already been used' in str(excinfo.value)
    with pytest.raises(ThriftGrammerError) as excinfo:
        load('parser-cases/e_duplicate_field_name.thrift')
    assert 'field identifier/name has already been used' in str(excinfo.value)
Beispiel #2
0
def test_e_duplicate_field_id_or_name():
    with pytest.raises(ThriftGrammerError) as excinfo:
        load('parser-cases/e_duplicate_field_id.thrift')
    assert 'field identifier/name has already been used' in str(excinfo.value)
    with pytest.raises(ThriftGrammerError) as excinfo:
        load('parser-cases/e_duplicate_field_name.thrift')
    assert 'field identifier/name has already been used' in str(excinfo.value)
Beispiel #3
0
def test_e_structs():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_structs_0.thrift')
    assert str(excinfo.value) == \
        'Field \'name\' was required to create constant for type \'User\''

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_structs_1.thrift')
    assert str(excinfo.value) == \
        'No field named \'avatar\' was found in struct of type \'User\''
Beispiel #4
0
def test_e_structs():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_structs_0.thrift')
    assert str(excinfo.value) == \
        'Field \'name\' was required to create constant for type \'User\''

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_structs_1.thrift')
    assert str(excinfo.value) == \
        'No field named \'avatar\' was found in struct of type \'User\''
Beispiel #5
0
def test_e_structs():
    try:
        load('parser-cases/e_structs_0.thrift')
    except ThriftParserError as e:
        assert str(e) == \
            'Field \'name\' was required to create constant for type \'User\''

    try:
        load('parser-cases/e_structs_1.thrift')
    except ThriftParserError as e:
        assert str(e) == \
            'No field named \'avatar\' was found in struct of type \'User\''
Beispiel #6
0
def test_structs():
    thrift = load('parser-cases/structs.thrift')
    assert thrift.Person.thrift_spec == {
        1: (TType.STRING, 'name', False),
        2: (TType.STRING, 'address', False)
    }
    assert thrift.Person.default_spec == [
        ('name', None), ('address', None)
    ]
    assert thrift.Email.thrift_spec == {
        1: (TType.STRING, 'subject', False),
        2: (TType.STRING, 'content', False),
        3: (TType.STRUCT, 'sender', thrift.Person, False),
        4: (TType.STRUCT, 'recver', thrift.Person, True),
    }
    assert thrift.Email.default_spec == [
        ('subject', 'Subject'), ('content', None),
        ('sender', None), ('recver', None)
    ]
    assert thrift.email == thrift.Email(
        subject='Hello',
        content='Long time no see',
        sender=thrift.Person(name='jack', address='*****@*****.**'),
        recver=thrift.Person(name='chao', address='*****@*****.**')
    )
Beispiel #7
0
def test_service():
    thrift = load('parser-cases/service.thrift')
    assert thrift.EmailService.thrift_services == ['ping', 'send']
    assert thrift.EmailService.ping_args.thrift_spec == {}
    assert thrift.EmailService.ping_args.default_spec == []
    assert thrift.EmailService.ping_result.thrift_spec == {
        1: (TType.STRUCT, 'network_error', thrift.NetworkError, False)
    }
    assert thrift.EmailService.ping_result.default_spec == [
        ('network_error', None)
    ]
    assert thrift.EmailService.send_args.thrift_spec == {
        1: (TType.STRUCT, 'recver', thrift.User, False),
        2: (TType.STRUCT, 'sender', thrift.User, False),
        3: (TType.STRUCT, 'email', thrift.Email, False),
    }
    assert thrift.EmailService.send_args.default_spec == [
        ('recver', None), ('sender', None), ('email', None)
    ]
    assert thrift.EmailService.send_result.thrift_spec == {
        0: (TType.BOOL, 'success', False),
        1: (TType.STRUCT, 'network_error', thrift.NetworkError, False)
    }
    assert thrift.EmailService.send_result.default_spec == [
        ('success', None), ('network_error', None)
    ]
Beispiel #8
0
def test_service():
    thrift = load('parser-cases/service.thrift')
    assert thrift.EmailService.thrift_services == ['ping', 'send']
    assert thrift.EmailService.ping_args.thrift_spec == {}
    assert thrift.EmailService.ping_args.default_spec == []
    assert thrift.EmailService.ping_result.thrift_spec == {
        1: (TType.STRUCT, 'network_error', thrift.NetworkError, False)
    }
    assert thrift.EmailService.ping_result.default_spec == [
        ('network_error', None)
    ]
    assert thrift.EmailService.send_args.thrift_spec == {
        1: (TType.STRUCT, 'recver', thrift.User, False),
        2: (TType.STRUCT, 'sender', thrift.User, False),
        3: (TType.STRUCT, 'email', thrift.Email, False),
    }
    assert thrift.EmailService.send_args.default_spec == [
        ('recver', None), ('sender', None), ('email', None)
    ]
    assert thrift.EmailService.send_result.thrift_spec == {
        0: (TType.BOOL, 'success', False),
        1: (TType.STRUCT, 'network_error', thrift.NetworkError, False)
    }
    assert thrift.EmailService.send_result.default_spec == [
        ('success', None), ('network_error', None)
    ]
Beispiel #9
0
def test_structs():
    thrift = load('parser-cases/structs.thrift')
    assert thrift.Person.thrift_spec == {
        1: (TType.STRING, 'name', False),
        2: (TType.STRING, 'address', False)
    }
    assert thrift.Person.default_spec == [
        ('name', None), ('address', None)
    ]
    assert thrift.Email.thrift_spec == {
        1: (TType.STRING, 'subject', False),
        2: (TType.STRING, 'content', False),
        3: (TType.STRUCT, 'sender', thrift.Person, False),
        4: (TType.STRUCT, 'recver', thrift.Person, True),
    }
    assert thrift.Email.default_spec == [
        ('subject', 'Subject'), ('content', None),
        ('sender', None), ('recver', None)
    ]
    assert thrift.email == thrift.Email(
        subject='Hello',
        content='Long time no see',
        sender=thrift.Person(name='jack', address='*****@*****.**'),
        recver=thrift.Person(name='chao', address='*****@*****.**')
    )
Beispiel #10
0
def test_thrift_meta():
    thrift = load('parser-cases/tutorial.thrift')
    meta = thrift.__thrift_meta__
    assert meta['consts'] == [thrift.INT32CONSTANT, thrift.MAPCONSTANT]
    assert meta['enums'] == [thrift.Operation]
    assert meta['structs'] == [thrift.Work]
    assert meta['exceptions'] == [thrift.InvalidOperation]
    assert meta['services'] == [thrift.Calculator]
    assert meta['includes'] == [thrift.shared]
Beispiel #11
0
def test_thrift_meta():
    thrift = load('parser-cases/tutorial.thrift')
    meta = thrift.__thrift_meta__
    assert meta['consts'] == [thrift.INT32CONSTANT, thrift.MAPCONSTANT]
    assert meta['enums'] == [thrift.Operation]
    assert meta['structs'] == [thrift.Work]
    assert meta['exceptions'] == [thrift.InvalidOperation]
    assert meta['services'] == [thrift.Calculator]
    assert meta['includes'] == [thrift.shared]
Beispiel #12
0
def test_doubles():
    thrift = load('parser-cases/doubles.thrift')
    book = thrift.Book()
    assert book.price == 1
    assert isinstance(book.price, float)
    assert isinstance(thrift.value1, float) and thrift.value1 == 3
    assert isinstance(thrift.value2, float) and thrift.value2 == 3.1
    assert isinstance(thrift.value3, float) and thrift.value3 == 100000.0
    assert isinstance(thrift.value4, float) and thrift.value4 == -1.5e-05
    assert isinstance(thrift.value5, float) and thrift.value5 == 150000.0
Beispiel #13
0
def test_recursive_union():
    thrift = load('parser-cases/recursive_union.thrift')
    assert thrift.Dynamic.thrift_spec == {
        1: (TType.BOOL, 'boolean', False),
        2: (TType.I64, 'integer', False),
        3: (TType.DOUBLE, 'doubl', False),
        4: (TType.STRING, 'str', False),
        5: (TType.LIST, 'arr', (TType.STRUCT, thrift.Dynamic), False),
        6: (TType.MAP, 'object', (TType.STRING, (TType.STRUCT,
                                                 thrift.Dynamic)), False)
    }
Beispiel #14
0
def test_tutorial():
    thrift = load('parser-cases/tutorial.thrift', include_dir='./parser-cases')
    assert thrift.INT32CONSTANT == 9853
    assert thrift.MAPCONSTANT == {'hello': 'world', 'goodnight': 'moon'}
    assert thrift.Operation.ADD == 1 and thrift.Operation.SUBTRACT == 2 \
        and thrift.Operation.MULTIPLY == 3 and thrift.Operation.DIVIDE == 4
    work = thrift.Work()
    assert work.num1 == 0 and work.num2 is None and work.op is None \
        and work.comment is None
    assert set(thrift.Calculator.thrift_services) == set([
        'ping', 'add', 'calculate', 'zip', 'getStruct'])
Beispiel #15
0
def test_tutorial():
    thrift = load('parser-cases/tutorial.thrift', include_dir='./parser-cases')
    assert thrift.INT32CONSTANT == 9853
    assert thrift.MAPCONSTANT == {'hello': 'world', 'goodnight': 'moon'}
    assert thrift.Operation.ADD == 1 and thrift.Operation.SUBTRACT == 2 \
        and thrift.Operation.MULTIPLY == 3 and thrift.Operation.DIVIDE == 4
    work = thrift.Work()
    assert work.num1 == 0 and work.num2 is None and work.op is None \
        and work.comment is None
    assert set(thrift.Calculator.thrift_services) == set([
        'ping', 'add', 'calculate', 'zip', 'getStruct'])
Beispiel #16
0
def test_recursive_union():
    thrift = load('parser-cases/recursive_union.thrift')
    assert thrift.Dynamic.thrift_spec == {
        1: (TType.BOOL, 'boolean', False),
        2: (TType.I64, 'integer', False),
        3: (TType.DOUBLE, 'doubl', False),
        4: (TType.STRING, 'str', False),
        5: (TType.LIST, 'arr', (TType.STRUCT, thrift.Dynamic), False),
        6: (TType.MAP, 'object', (TType.STRING, (TType.STRUCT,
                                                 thrift.Dynamic)), False)
    }
Beispiel #17
0
def test_constants():
    thrift = load('parser-cases/constants.thrift')
    assert thrift.int16 == 3
    assert thrift.int32 == 800
    assert thrift.int64 == 123456789
    assert thrift.tstr == 'hello world'
    assert thrift.integer32 == 900
    assert thrift.tdouble == 1.3
    assert thrift.tlist == [1, 2, 3]
    assert thrift.tset == set([1, 2, 3])
    assert thrift.tmap1 == {'key': 'val'}
    assert thrift.tmap2 == {'key': 32}
    assert thrift.my_country == 4
    assert thrift.tom == thrift.Person(name='tom')
    assert thrift.country_map == {1: 'US', 2: 'UK', 3: 'CA', 4: 'CN'}
Beispiel #18
0
def test_constants():
    thrift = load('parser-cases/constants.thrift')
    assert thrift.int16 == 3
    assert thrift.int32 == 800
    assert thrift.int64 == 123456789
    assert thrift.tstr == 'hello world'
    assert thrift.integer32 == 900
    assert thrift.tdouble == 1.3
    assert thrift.tlist == [1, 2, 3]
    assert thrift.tset == set([1, 2, 3])
    assert thrift.tmap1 == {'key': 'val'}
    assert thrift.tmap2 == {'key': 32}
    assert thrift.my_country == 4
    assert thrift.tom == thrift.Person(name='tom')
    assert thrift.country_map == {1: 'US', 2: 'UK', 3: 'CA', 4: 'CN'}
Beispiel #19
0
def test_enums():
    thrift = load('parser-cases/enums.thrift')
    assert thrift.Lang.C == 0
    assert thrift.Lang.Go == 1
    assert thrift.Lang.Java == 2
    assert thrift.Lang.Javascript == 3
    assert thrift.Lang.PHP == 4
    assert thrift.Lang.Python == 5
    assert thrift.Lang.Ruby == 6
    assert thrift.Country.US == 1
    assert thrift.Country.UK == 2
    assert thrift.Country.CN == 3
    assert thrift.OS.OSX == 0
    assert thrift.OS.Win == 3
    assert thrift.OS.Linux == 4
Beispiel #20
0
def test_enums():
    thrift = load('parser-cases/enums.thrift')
    assert thrift.Lang.C == 0
    assert thrift.Lang.Go == 1
    assert thrift.Lang.Java == 2
    assert thrift.Lang.Javascript == 3
    assert thrift.Lang.PHP == 4
    assert thrift.Lang.Python == 5
    assert thrift.Lang.Ruby == 6
    assert thrift.Country.US == 1
    assert thrift.Country.UK == 2
    assert thrift.Country.CN == 3
    assert thrift.OS.OSX == 0
    assert thrift.OS.Win == 3
    assert thrift.OS.Linux == 4
Beispiel #21
0
def test_e_type_error():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_type_error_0.thrift')
    assert 'Type error' in str(excinfo.value)

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_type_error_1.thrift')
    assert 'Type error' in str(excinfo.value)

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_type_error_2.thrift')
    assert 'Type error' in str(excinfo.value)
Beispiel #22
0
def test_e_type_error():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_type_error_0.thrift')
    assert 'Type error' in str(excinfo.value)

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_type_error_1.thrift')
    assert 'Type error' in str(excinfo.value)

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_type_error_2.thrift')
    assert 'Type error' in str(excinfo.value)
Beispiel #23
0
def test_e_value_ref():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_value_ref_0.thrift')
    assert excinfo.value

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_value_ref_1.thrift')
    assert str(excinfo.value) == ('Couldn\'t find a named value in enum Lang '
                                  'for value 3')
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_value_ref_2.thrift')
    assert str(excinfo.value) == \
        'No enum value or constant found named \'Cookbook\''
Beispiel #24
0
def test_e_value_ref():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_value_ref_0.thrift')
    assert excinfo.value

    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_value_ref_1.thrift')
    assert str(excinfo.value) == ('Couldn\'t find a named value in enum Lang '
                                  'for value 3')
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_value_ref_2.thrift')
    assert str(excinfo.value) == \
        'No enum value or constant found named \'Cookbook\''
Beispiel #25
0
def test_e_type_error():
    try:
        load('parser-cases/e_type_error_0.thrift')
    except ThriftParserError as e:
        assert 'Type error' in str(e)
    try:
        load('parser-cases/e_type_error_1.thrift')
    except ThriftParserError as e:
        assert 'Type error' in str(e)

    try:
        load('parser-cases/e_type_error_2.thrift')
    except ThriftParserError as e:
        assert 'Type error' in str(e)
Beispiel #26
0
def test_e_value_ref():
    try:
        load('parser-cases/e_value_ref_0.thrift')
    except ThriftParserError as e:
        pass
    try:
        load('parser-cases/e_value_ref_1.thrift')
    except ThriftParserError as e:
        assert str(e) == 'No named enum value found named \'Lang.Python\''

    try:
        load('parser-cases/e_value_ref_2.thrift')
    except ThriftParserError as e:
        assert str(e) == \
            'No named enum value or constant found named \'Cookbook\''
Beispiel #27
0
def test_e_dead_include():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_dead_include_0.thrift')
    assert 'Dead including' in str(excinfo.value)
Beispiel #28
0
def test_e_service_extends():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_service_extends_0.thrift')
    assert 'Can\'t find service' in str(excinfo.value)
Beispiel #29
0
def test_comments():
    load('parser-cases/comments.thrift')
Beispiel #30
0
def test_e_dead_include():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_dead_include_0.thrift')
    assert 'Dead including' in str(excinfo.value)
Beispiel #31
0
def test_service_extends():
    thrift = load('parser-cases/service_extends.thrift')
    assert thrift.PingService.thrift_services == ['ping', 'getStruct']
Beispiel #32
0
def test_value_ref():
    thrift = load('parser-cases/value_ref.thrift')
    assert thrift.container == {'key': [1, 2, 3]}
    assert thrift.lst == [39, 899, 123]
Beispiel #33
0
def test_cpp_include():
    load('parser-cases/cpp_include.thrift')
Beispiel #34
0
def test_e_dead_include():
    try:
        load('parser-cases/e_dead_include_0.thrift')
    except ThriftParserError as e:
        assert 'Dead including' in str(e)
Beispiel #35
0
def test_comments():
    load('parser-cases/comments.thrift')
Beispiel #36
0
def test_annotations():
    load('parser-cases/annotations.thrift')
    load('parser-cases/issue_252.thrift')
Beispiel #37
0
def test_issue_215():
    thrift = load('parser-cases/issue_215.thrift')
    assert thrift.abool is True
    assert thrift.falseValue == 123
Beispiel #38
0
    meta = tree.__thrift_meta__
    for s in meta['structs']:
        print('table {} {{'.format(s.__name__))
        for order, field in list(s.thrift_spec.items()):
            print('  {}: {};'.format(field[1], fbstype(field)))
        print('}')
        print
    for e in meta['enums']:
        print('enum {}: {} {{'.format(e.__name__, fbstype([e._ttype])))
        for field, value in list(e._NAMES_TO_VALUES.items()):
            print('  {} = {},'.format(field, value))
        print('}')
        print


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-I',
                        dest='include_paths',
                        type=str,
                        help='Path to look for included thrift files')
    parser.add_argument('--ignore-unsupported',
                        action="store_true",
                        default=True,
                        help='If true, ignore unsupported features')
    args, unknown_args = parser.parse_known_args()
    tree = load(unknown_args[0], include_dir=args.include_paths)
    if not args.ignore_unsupported:
        check_fbs_unsupported(tree)
    generate_fbs(tree)
Beispiel #39
0
def test_e_service_extends():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_service_extends_0.thrift')
    assert 'Can\'t find service' in str(excinfo.value)
Beispiel #40
0
def test_service_extends():
    thrift = load('parser-cases/service_extends.thrift')
    assert thrift.PingService.thrift_services == ['ping', 'getStruct']
Beispiel #41
0
def test_type_ref():
    thrift = load('parser-cases/type_ref.thrift')
    assert thrift.jerry == thrift.type_ref_shared.Writer(
        name='jerry', age=26, country=thrift.type_ref_shared.Country.US)
    assert thrift.book == thrift.type_ref_shared.Book(name='Hello World',
                                                      writer=thrift.jerry)
Beispiel #42
0
def test_e_service_extends():
    try:
        load('parser-cases/e_service_extends_0.thrift')
    except ThriftParserError as e:
        assert 'Can\'t find service' in str(e)
Beispiel #43
0
def test_value_ref():
    thrift = load('parser-cases/value_ref.thrift')
    assert thrift.container == {'key': [1, 2, 3]}
    assert thrift.lst == [39, 899, 123]
Beispiel #44
0
def test_include():
    thrift = load('parser-cases/include.thrift', include_dir='./parser-cases')
    assert thrift.datetime == 1422009523
Beispiel #45
0
def test_include():
    thrift = load('parser-cases/include.thrift', include_dir='./parser-cases')
    assert thrift.datetime == 1422009523
Beispiel #46
0
def test_issue_215():
    thrift = load('parser-cases/issue_215.thrift')
    assert thrift.abool is True
    assert thrift.falseValue == 123
Beispiel #47
0
def test_e_use_thrift_reserved_keywords():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_use_thrift_reserved_keywords.thrift')
    assert 'Cannot use reserved language keyword' in str(excinfo.value)
Beispiel #48
0
def test_type_ref():
    thrift = load('parser-cases/type_ref.thrift')
    assert thrift.jerry == thrift.type_ref_shared.Writer(
        name='jerry', age=26, country=thrift.type_ref_shared.Country.US)
    assert thrift.book == thrift.type_ref_shared.Book(name='Hello World',
                                                      writer=thrift.jerry)
Beispiel #49
0
def test_e_grammer_error_at_eof():
    with pytest.raises(ThriftGrammerError) as excinfo:
        load('parser-cases/e_grammer_error_at_eof.thrift')
    assert str(excinfo.value) == 'Grammer error at EOF'
Beispiel #50
0
def test_e_grammer_error_at_eof():
    with pytest.raises(ThriftGrammerError) as excinfo:
        load('parser-cases/e_grammer_error_at_eof.thrift')
    assert str(excinfo.value) == 'Grammer error at EOF'
Beispiel #51
0
def test_e_use_thrift_reserved_keywords():
    with pytest.raises(ThriftParserError) as excinfo:
        load('parser-cases/e_use_thrift_reserved_keywords.thrift')
    assert 'Cannot use reserved language keyword' in str(excinfo.value)