Exemplo n.º 1
0
def test_container_mapping(m):
    class mapping(dict):
        pass

    containers = [(MessageOfTypes.NestedType(), mapping),
                  (MessageOfTypes(), dict)]

    m.nestedRepeated.extend([MessageOfTypes.NestedType(req='1')])
    d = protobuf_to_dict(m, containers=containers)
    m = dict_to_protobuf(d, containers=containers)

    assert isinstance(d, dict)
    assert isinstance(d['nested'], mapping)
    assert isinstance(m, MessageOfTypes)
    assert isinstance(m.nested, MessageOfTypes.NestedType)
Exemplo n.º 2
0
def test_nested_repeated(m):
    m.nestedRepeated.extend(
        [MessageOfTypes.NestedType(req=str(i)) for i in range(10)])

    d = protobuf_to_dict(m)
    compare(m, d, exclude=['nestedRepeated'])
    assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)]

    m2 = dict_to_protobuf(d, MessageOfTypes)
    assert m == m2
Exemplo n.º 3
0
def test_conditional_container_mapping(m):
    class truedict(dict):
        pass

    class falsedict(dict):
        pass

    containers = [(MessageOfTypes(bol=True), truedict),
                  (MessageOfTypes(bol=False), falsedict),
                  (MessageOfTypes.NestedType(), dict)]

    m.bol = True
    d = protobuf_to_dict(m, containers=containers)
    p = dict_to_protobuf(d, containers=containers)

    assert isinstance(d, truedict)
    assert isinstance(p, MessageOfTypes)

    m.bol = False
    d = protobuf_to_dict(m, containers=containers)
    p = dict_to_protobuf(d, containers=containers)

    assert isinstance(d, falsedict)
    assert isinstance(p, MessageOfTypes)
Exemplo n.º 4
0
def m():
    m = MessageOfTypes()
    m.dubl = 1.7e+308
    m.flot = 3.4e+038
    m.i32 = 2 ** 31 - 1  # 2147483647 #
    m.i64 = 2 ** 63 - 1  # 0x7FFFFFFFFFFFFFFF
    m.ui32 = 2 ** 32 - 1
    m.ui64 = 2 ** 64 - 1
    m.si32 = -1 * m.i32
    m.si64 = -1 * m.i64
    m.f32 = m.i32
    m.f64 = m.i64
    m.sf32 = m.si32
    m.sf64 = m.si64
    m.bol = True
    m.strng = "string"
    m.byts = b'\n\x14\x1e'
    assert len(m.byts) == 3, len(m.byts)
    m.nested.req = "req"
    m.enm = MessageOfTypes.C  # @UndefinedVariable
    m.enmRepeated.extend([MessageOfTypes.A, MessageOfTypes.C])
    m.range.extend(range(10))
    return m
Exemplo n.º 5
0
def m():
    m = MessageOfTypes()
    m.dubl = 1.7e+308
    m.flot = 3.4e+038
    m.i32 = 2 ** 31 - 1  # 2147483647 #
    m.i64 = 2 ** 63 - 1  # 0x7FFFFFFFFFFFFFFF
    m.ui32 = 2 ** 32 - 1
    m.ui64 = 2 ** 64 - 1
    m.si32 = -1 * m.i32
    m.si64 = -1 * m.i64
    m.f32 = m.i32
    m.f64 = m.i64
    m.sf32 = m.si32
    m.sf64 = m.si64
    m.bol = True
    m.strng = "string"
    m.byts = b'\n\x14\x1e'
    assert len(m.byts) == 3, len(m.byts)
    m.nested.req = "req"
    m.enm = MessageOfTypes.C  # @UndefinedVariable
    m.enmRepeated.extend([MessageOfTypes.A, MessageOfTypes.C])
    m.range.extend(range(10))
    return m