示例#1
0
def test_load_file():
    roots = [pkg_resources.resource_filename(__name__, './resources')]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test.proto',
    )
    print(DefParser(roots).parse(test_proto))
示例#2
0
def test_describe_type():
    roots = [
        pkg_resources.resource_filename(__name__, './resources')
    ]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/deeply_nested_def.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)

    raw = {
        'a': 'foo',
        'b': 'bar',
        'j': 1234567,
        'k': True,

        'd': {
            'a': {
                'a': 2,
                'b': 'spam',
                'c': 'egg',
                'd': False,
            },
            'b': {
                'a': 0,
                'b': 'ham-bar',
                'c': 'bar-ham',
                'd': True,
            },
            'c': False,
        },
        'e': {
            'b': 1,
            'c': {
                'a': 'foo-spam',
                'b': 'spam-foo',
            },
        },
        'f': {
            'd': 1,
        },
        'g': {
            'b': 0,
            'c': {
                'a': -123456,
            },
        },
    }

    values = {}

    for k, v in parser.describe_type('TopLevel').items():
        print('k: {}, v: {}'.format(k, v))
        values[k] = v(raw.get(k, None))

    ptype = parser.find_definition('TopLevel')
    result = ptype(**values)
    print(result)
示例#3
0
def test_rpc():
    roots = [pkg_resources.resource_filename(__name__, './resources')]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test_rpc.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)
示例#4
0
def test_nested_otpions():
    roots = [pkg_resources.resource_filename(__name__, './resources')]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test_nested_options.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)
示例#5
0
def test_double_line_comment():
    roots = [pkg_resources.resource_filename(__name__, './resources')]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test_double_line_comment.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)
示例#6
0
def test_nonexisting_import():
    roots = [pkg_resources.resource_filename(__name__, './resources')]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/nonexistent_import.proto',
    )
    try:
        DefParser(roots).parse(test_proto)
    except FileNotFoundError as e:
        assert 'nonexistent.proto' in str(e)
示例#7
0
def test_imported_oneof():
    roots = [
        pkg_resources.resource_filename(__name__, './'),
        pkg_resources.resource_filename(__name__, './resources')
    ]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test_imported_oneof.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)

    found = False
    for f, p in parser.files():
        if b'test_imported_oneof.proto' in f:
            found = True
    assert found
示例#8
0
def test_deep_nesting():
    roots = [pkg_resources.resource_filename(__name__, './resources')]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/deeply_nested_def.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)

    # 10 messages + 7 enums
    defcount = 10 + 7
    actual_defcount = 0
    for k, v in parser.definitions():
        print('def: {} => {}'.format(k, v))
        actual_defcount += 1

    assert defcount == actual_defcount
示例#9
0
def test_iterate_defs():
    roots = [
        pkg_resources.resource_filename(__name__, './resources'),
        pkg_resources.resource_filename(__name__, '.')
    ]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test_imported_oneof.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)

    passed = False
    for k, v in parser.definitions():
        if k == b'resources.Test.EmbeddedMessage':
            passed = True

    assert passed
示例#10
0
def test_imported_oneof():
    roots = [
        pkg_resources.resource_filename(__name__, './'),
        pkg_resources.resource_filename(__name__, './resources')
    ]
    test_proto = pkg_resources.resource_filename(
        __name__,
        './resources/test_imported_oneof.proto',
    )
    parser = DefParser(roots)
    parser.parse(test_proto)

    fields = []
    for f, p in parser.files.items():
        if b'test_imported_oneof.proto' in f:
            for r in p:
                if p[0] == 0:   # message
                    fields += p[2:]
    assert all(
        field[1].startswith(b'resources.')
        for field in fields
        if not type(field[1]) is list
    )