Skip to content

Commit 8e5dd47

Browse files
committed
Add more tests for packstream map key encoding and decoding
1 parent f4fcf70 commit 8e5dd47

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/unit/common/codec/packstream/v1/test_packstream.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,27 @@ def test_tiny_maps(self, assert_packable, size):
564564
data_out += bytearray([0x81, 64 + el, el])
565565
assert_packable(data_in, bytes(data_out))
566566

567+
@pytest.mark.parametrize("size", range(0x10))
568+
def test_tiny_maps_padded_key(self, assert_packable, size):
569+
data_in = {}
570+
data_out = bytearray([0xA0 + size])
571+
padding = b"1234567890abcdefghijklmnopqrstuvwxyz"
572+
for el in range(1, size + 1):
573+
data_in[padding.decode("ascii") + chr(64 + el)] = el
574+
data_out += bytearray([0xD0, 37, *padding, 64 + el, el])
575+
assert_packable(data_in, bytes(data_out))
576+
567577
def test_map_8(self, pack, assert_packable):
568578
d = {f"A{i}": 1 for i in range(40)}
569579
b = b"".join(pack(f"A{i}", 1) for i in range(40))
570580
assert_packable(d, b"\xd8\x28" + b)
571581

582+
def test_map_8_padded_key(self, pack, assert_packable):
583+
padding = "1234567890abcdefghijklmnopqrstuvwxyz"
584+
d = {f"{padding}-{i}": 1 for i in range(40)}
585+
b = b"".join(pack(f"{padding}-{i}", 1) for i in range(40))
586+
assert_packable(d, b"\xd8\x28" + b)
587+
572588
def test_map_16(self, pack, assert_packable):
573589
d = {f"A{i}": 1 for i in range(40000)}
574590
b = b"".join(pack(f"A{i}", 1) for i in range(40000))
@@ -579,6 +595,30 @@ def test_map_32(self, pack, assert_packable):
579595
b = b"".join(pack(f"A{i}", 1) for i in range(80000))
580596
assert_packable(d, b"\xda\x00\x01\x38\x80" + b)
581597

598+
def test_map_key_tiny_string(self, assert_packable):
599+
key = "A"
600+
d = {key: 1}
601+
data_out = b"\xa1\x81" + key.encode("utf-8") + b"\x01"
602+
assert_packable(d, bytes(data_out))
603+
604+
def test_map_key_string_8(self, assert_packable):
605+
key = "A" * 40
606+
d = {key: 1}
607+
data_out = b"\xa1\xd0\x28" + key.encode("utf-8") + b"\x01"
608+
assert_packable(d, data_out)
609+
610+
def test_map_key_string_16(self, assert_packable):
611+
key = "A" * 40000
612+
d = {key: 1}
613+
data_out = b"\xa1\xd1\x9c\x40" + key.encode("utf-8") + b"\x01"
614+
assert_packable(d, data_out)
615+
616+
def test_map_key_string_32(self, assert_packable):
617+
key = "A" * 80000
618+
d = {key: 1}
619+
data_out = b"\xa1\xd2\x00\x01\x38\x80" + key.encode("utf-8") + b"\x01"
620+
assert_packable(d, data_out)
621+
582622
def test_empty_dataframe_maps(self, assert_packable):
583623
df = pd.DataFrame()
584624
assert_packable(df, b"\xa0", {})

0 commit comments

Comments
 (0)