Exemplo n.º 1
0
def test_loads_embedded_message():
    type_1 = MessageType().add_field(1, 'a', UVarint)
    type_3 = MessageType().add_field(3, 'c', EmbeddedMessage(type_1))
    msg = type_3.loads(b('\x1a\x03\x08\x96\x01'))
    assert 'c' in msg
    assert 'a' in msg.c
    assert msg.c.a == 150
Exemplo n.º 2
0
def test_dumps_embedded_message():
    type_1 = MessageType().add_field(1, 'a', UVarint)
    type_3 = MessageType().add_field(3, 'c', EmbeddedMessage(type_1))
    msg = type_3()
    msg.c = type_1()
    msg.c.a = 150
    assert msg.dumps() == b('\x1a\x03\x08\x96\x01')
Exemplo n.º 3
0
def test_loads_embedded_message():
    type_1 = MessageType().add_field(1, 'a', UVarint)
    type_3 = MessageType().add_field(3, 'c', EmbeddedMessage(type_1))
    msg = type_3.loads(b('\x1a\x03\x08\x96\x01'))
    assert 'c' in msg
    assert 'a' in msg.c
    assert msg.c.a == 150
Exemplo n.º 4
0
def test_dump_message_type():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type()
    msg.b = b('testing')
    fp = BytesIO()
    msg.dump(fp)
    assert fp.getvalue() == b('\x12\x07\x74\x65\x73\x74\x69\x6e\x67')
Exemplo n.º 5
0
def test_iter():
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    message_type.add_field(2, 'c', Bytes, flags=Flags.PACKED_REPEATED)
    assert list(message_type) == [
        (1, 'b', UVarint, Flags.REPEATED),
        (2, 'c', Bytes, Flags.PACKED_REPEATED),
    ]
Exemplo n.º 6
0
def test_hash():
    type_1 = MessageType().add_field(1, 'b', UVarint)
    type_2 = MessageType().add_field(1, 'a', UVarint)
    type_3 = MessageType().add_field(2, 'a', UVarint)
    type_4 = MessageType().add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    assert hash(type_1) == hash(type_2)
    assert hash(type_1) != hash(type_3)
    assert hash(type_1) != hash(type_4)
Exemplo n.º 7
0
def test_loads_non_repeated():
    """
    Tests that the last value from the input stream is assigned to a non-repeated field.
    """
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint)
    msg = message_type.loads(b('\x08\x01\x08\x02\x08\x03'))
    assert msg.b == 3
Exemplo n.º 8
0
def test_loads_non_repeated():
    """
    Tests that the last value from the input stream is assigned to a non-repeated field.
    """
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint)
    msg = message_type.loads(b('\x08\x01\x08\x02\x08\x03'))
    assert msg.b == 3
Exemplo n.º 9
0
def test_dump_message_type():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type()
    msg.b = b('testing')
    fp = BytesIO()
    msg.dump(fp)
    assert fp.getvalue() == b('\x12\x07\x74\x65\x73\x74\x69\x6e\x67')
Exemplo n.º 10
0
def test_loads_repeated_value():
    """
    Tests repeated value.
    """
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    msg = message_type.loads(b('\x08\x01\x08\x02\x08\x03'))
    assert 'b' in msg
    assert msg.b == [1, 2, 3]
Exemplo n.º 11
0
def test_loads_repeated_value():
    """
    Tests repeated value.
    """
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    msg = message_type.loads(b('\x08\x01\x08\x02\x08\x03'))
    assert 'b' in msg
    assert msg.b == [1, 2, 3]
Exemplo n.º 12
0
def test_dumps_loads_embedded_message():
    type_2 = MessageType().add_field(1, 'a', UVarint)
    type_1 = MessageType()
    type_1.add_field(1, 'a', UVarint)
    type_1.add_field(2, 'b', EmbeddedMessage(type_2))
    type_1.add_field(3, 'c', UVarint)
    msg = type_1()
    msg.a = 1
    msg.c = 3
    msg.b = type_2()
    msg.b.a = 2
    msg = type_1.loads(msg.dumps())
    assert msg.a == 1
    assert msg.c == 3
    assert msg.b.a == 2
Exemplo n.º 13
0
def test_dumps_loads_embedded_message():
    type_2 = MessageType().add_field(1, 'a', UVarint)
    type_1 = MessageType()
    type_1.add_field(1, 'a', UVarint)
    type_1.add_field(2, 'b', EmbeddedMessage(type_2))
    type_1.add_field(3, 'c', UVarint)
    msg = type_1()
    msg.a = 1
    msg.c = 3
    msg.b = type_2()
    msg.b.a = 2
    msg = type_1.loads(msg.dumps())
    assert msg.a == 1
    assert msg.c == 3
    assert msg.b.a == 2
Exemplo n.º 14
0
def test_iter():
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    message_type.add_field(2, 'c', Bytes, flags=Flags.PACKED_REPEATED)
    assert list(message_type) == [
        (1, 'b', UVarint, Flags.REPEATED),
        (2, 'c', Bytes, Flags.PACKED_REPEATED),
    ]
Exemplo n.º 15
0
def test_loads_packed_repeated_value():
    message_type = MessageType()
    message_type.add_field(4, 'd', UVarint, flags=Flags.PACKED_REPEATED)
    msg = message_type.loads(b('\x22\x06\x03\x8E\x02\x9E\xA7\x05'))
    assert 'd' in msg
    assert msg.d == [3, 270, 86942]
Exemplo n.º 16
0
def test_loads_missing_required_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes, flags=Flags.REQUIRED)
    with raises(ValueError):
        message_type.loads(b'')
Exemplo n.º 17
0
def test_loads_missing_optional_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type.loads(b'')
    assert 'b' not in msg
Exemplo n.º 18
0
def test_loads_missing_optional_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type.loads(b'')
    assert 'b' not in msg
Exemplo n.º 19
0
def test_loads_missing_required_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes, flags=Flags.REQUIRED)
    with raises(ValueError):
        message_type.loads(b'')
Exemplo n.º 20
0
def test_dumps_repeated_value():
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    msg = message_type()
    msg.b = (1, 2, 3)
    assert msg.dumps() == b('\x08\x01\x08\x02\x08\x03')
Exemplo n.º 21
0
def test_dumps_packed_repeated_value():
    message_type = MessageType()
    message_type.add_field(4, 'd', UVarint, flags=Flags.PACKED_REPEATED)
    msg = message_type()
    msg.d = (3, 270, 86942)
    assert msg.dumps() == b('\x22\x06\x03\x8E\x02\x9E\xA7\x05')
Exemplo n.º 22
0
def test_dumps_packed_repeated_value():
    message_type = MessageType()
    message_type.add_field(4, 'd', UVarint, flags=Flags.PACKED_REPEATED)
    msg = message_type()
    msg.d = (3, 270, 86942)
    assert msg.dumps() == b('\x22\x06\x03\x8E\x02\x9E\xA7\x05')
Exemplo n.º 23
0
def test_loads_packed_repeated_value():
    message_type = MessageType()
    message_type.add_field(4, 'd', UVarint, flags=Flags.PACKED_REPEATED)
    msg = message_type.loads(b('\x22\x06\x03\x8E\x02\x9E\xA7\x05'))
    assert 'd' in msg
    assert msg.d == [3, 270, 86942]
Exemplo n.º 24
0
def test_empty_optional_bytes():
    message_type = MessageType()
    message_type.add_field(1, 'a', Bytes)
    msg = message_type.loads(b('\n\x00'))
    assert msg.a == ''
Exemplo n.º 25
0
def test_empty_optional_bytes():
    message_type = MessageType()
    message_type.add_field(1, 'a', Bytes)
    msg = message_type.loads(b('\n\x00'))
    assert msg.a == ''
Exemplo n.º 26
0
def test_dumps_missing_required_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes, flags=Flags.REQUIRED)
    msg = message_type()
    with raises(ValueError):
        msg.dumps()
Exemplo n.º 27
0
def test_dumps_message_type():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type()
    msg.b = b('testing')
    assert msg.dumps() == b('\x12\x07\x74\x65\x73\x74\x69\x6e\x67')
Exemplo n.º 28
0
def test_dumps_missing_optional_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type()
    assert msg.dumps() == b('')
Exemplo n.º 29
0
def test_dumps_missing_optional_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type()
    assert msg.dumps() == b('')
Exemplo n.º 30
0
def test_dumps_missing_required_value():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes, flags=Flags.REQUIRED)
    msg = message_type()
    with raises(ValueError):
        msg.dumps()
Exemplo n.º 31
0
def test_dumps_message_type():
    message_type = MessageType()
    message_type.add_field(2, 'b', Bytes)
    msg = message_type()
    msg.b = b('testing')
    assert msg.dumps() == b('\x12\x07\x74\x65\x73\x74\x69\x6e\x67')
Exemplo n.º 32
0
def test_dumps_repeated_value():
    message_type = MessageType()
    message_type.add_field(1, 'b', UVarint, flags=Flags.REPEATED)
    msg = message_type()
    msg.b = (1, 2, 3)
    assert msg.dumps() == b('\x08\x01\x08\x02\x08\x03')