Exemplo n.º 1
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.º 2
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.º 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_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.º 5
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.º 6
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.º 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_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.º 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_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.º 12
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.º 13
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.º 14
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.º 15
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.º 16
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.º 17
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.º 18
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.º 19
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.º 20
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.º 21
0
        Bool,
        Bytes,
        EmbeddedMessage,
        Flags,
        Int32,
        Int64,
        MessageType,
        UInt64,
        Unicode,
        UVarint,
        Varint,
    )


@mark.parametrize('value, expected', [
    (0, b('\x00')),
    (3, b('\x03')),
    (270, b('\x8E\x02')),
    (86942, b('\x9E\xA7\x05')),
])
def test_dumps_uvarint(value, expected):
    assert UVarint.dumps(value) == expected


@mark.parametrize('value, expected', [
    (b('\x00'), 0),
    (b('\x03'), 3),
    (b('\x8E\x02'), 270),
    (b('\x9E\xA7\x05'), 86942),
])
def test_loads_uvarint(value, expected):
Exemplo n.º 22
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.º 23
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.º 24
0
 def dump(self, fp, value):
     fp.write(b('\x01' if value else '\x00'))  # similarly to UVarint
Exemplo n.º 25
0
        Bool,
        Bytes,
        EmbeddedMessage,
        Flags,
        Int32,
        Int64,
        MessageType,
        UInt64,
        Unicode,
        UVarint,
        Varint,
    )


@mark.parametrize('value, expected', [
    (0, b('\x00')),
    (3, b('\x03')),
    (270, b('\x8E\x02')),
    (86942, b('\x9E\xA7\x05')),
])
def test_dumps_uvarint(value, expected):
    assert UVarint.dumps(value) == expected


@mark.parametrize('value, expected', [
    (b('\x00'), 0),
    (b('\x03'), 3),
    (b('\x8E\x02'), 270),
    (b('\x9E\xA7\x05'), 86942),
])
def test_loads_uvarint(value, expected):