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')
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
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')
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
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]
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')
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')
def test_dumps_missing_optional_value(): message_type = MessageType() message_type.add_field(2, 'b', Bytes) msg = message_type() assert msg.dumps() == b('')
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')
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]
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 == ''
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):
def dump(self, fp, value): fp.write(b('\x01' if value else '\x00')) # similarly to UVarint