Skip to content

Commit 0d208b0

Browse files
committed
test: add test to ensure FixBytes > 4k can be stored in a Box
1 parent bc2d04d commit 0d208b0

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/_algopy_testing/primitives/fixed_bytes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def from_hex(cls, value: str) -> typing.Self:
279279
@classmethod
280280
def from_bytes(cls, value: Bytes | bytes) -> typing.Self:
281281
"""Construct an instance from the underlying bytes (no validation)"""
282-
bytes_value = as_bytes(value)
282+
bytes_value = value.value if isinstance(value, Bytes) else value
283283
c = cls._ensure_class_with_length(bytes_value)
284284
result = c()
285285
result.value = bytes_value

src/_algopy_testing/state/utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ def deserialize(typ: type[_TValue], value: SerializableValue) -> _TValue:
4242
if isinstance(value, bytes):
4343
raise TypeError("expected int, received bytes")
4444
return typ.from_int(value) # type: ignore[return-value]
45-
elif issubclass(typ, BytesBacked):
46-
if isinstance(value, int):
47-
raise TypeError("expected bytes, received int")
48-
return typ.from_bytes(Bytes(value)) # type: ignore[return-value]
49-
elif issubclass(typ, Serializable):
45+
elif issubclass(typ, BytesBacked) or issubclass(typ, Serializable):
5046
if isinstance(value, int):
5147
raise TypeError("expected bytes, received int")
5248
return typ.from_bytes(value) # type: ignore[return-value]
@@ -70,6 +66,7 @@ def cast_from_bytes(typ: type[_TValue], value: bytes) -> _TValue:
7066
serialized = as_int64(serialized)
7167
else:
7268
serialized = value
69+
7370
return deserialize(typ, serialized)
7471

7572

tests/primitives/test_fixed_bytes.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import typing
33

44
import pytest
5+
from _algopy_testing import algopy_testing_context
56
from _algopy_testing.constants import MAX_BYTES_SIZE
67
from _algopy_testing.primitives.bytes import Bytes
78
from _algopy_testing.primitives.fixed_bytes import FixedBytes
89
from _algopy_testing.primitives.uint64 import UInt64
10+
from _algopy_testing.state.box import Box
911

1012
from tests.util import int_to_bytes
1113

@@ -231,9 +233,6 @@ def test_fixed_bytes_from_bytes() -> None:
231233
assert len(fb32) == 32
232234
assert fb32.length == 32
233235

234-
with pytest.raises(ValueError, match="expected value length <= 4096, got: 4097"):
235-
FixedBytes[typing.Literal[64]].from_bytes(b"\x0f" * 4097)
236-
237236

238237
def test_fixed_bytes_from_bytes_with_bytes_object() -> None:
239238
"""Test FixedBytes.from_bytes class method with Bytes object."""
@@ -680,3 +679,17 @@ def test_contains_literal_bytes() -> None:
680679
assert b"\xcd\x12" in x
681680

682681
assert (b"\xcd\x12" not in x) == False # noqa: E712
682+
683+
684+
def test_big_fixed_bytes_in_box() -> None:
685+
with algopy_testing_context() as ctx: # noqa: SIM117
686+
with ctx.txn.create_group([ctx.any.txn.application_call()]):
687+
big_box = Box(FixedBytes[typing.Literal[5000]], key=b"big")
688+
big_box.create()
689+
assert big_box.value[4999] == b"\x00"
690+
691+
big_box.replace(4999, b"\xff")
692+
assert big_box.value[4999] == b"\xff"
693+
694+
big_box.splice(4990, 10, b"\x7f" * 10)
695+
assert big_box.value[4990:] == b"\x7f" * 10

0 commit comments

Comments
 (0)