Skip to content

Commit

Permalink
add tests for Box implmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
boblat committed Jul 12, 2024
1 parent 8611266 commit 70a632a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/algopy_testing/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
self._type = type_

# if key parameter is empty string, generate a random 32 bytes for key
if isinstance(key, str) and key == "":
if not bool(key):
self._key = algopy.Bytes(secrets.token_bytes(32))
else:
self._key = (
Expand Down
76 changes: 76 additions & 0 deletions tests/test_box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from collections.abc import Generator

import pytest
from algopy_testing import arc4
from algopy_testing.box import Box
from algopy_testing.context import AlgopyTestContext, algopy_testing_context
from algopy_testing.primitives.biguint import BigUInt
from algopy_testing.primitives.bytes import Bytes
from algopy_testing.primitives.string import String
from algopy_testing.primitives.uint64 import UInt64
from algopy_testing.utils import as_bytes, as_string


@pytest.fixture()
def context() -> Generator[AlgopyTestContext, None, None]:
with algopy_testing_context() as ctx:
yield ctx
ctx.reset()


@pytest.mark.parametrize(
("value_type", "key"),
[
(UInt64, ""),
(Bytes, b""),
(String, Bytes()),
(BigUInt, String()),
(arc4.String, ""),
(arc4.DynamicArray, b""),
],
)
def test_box_init_without_key(
context: AlgopyTestContext,
value_type: type,
key: bytes | str | Bytes | String,
) -> None:
box = Box(value_type, key=key)
assert not bool(box)
assert len(box.key) > 0
with pytest.raises(ValueError, match="Box has not been created"):
_ = box.value

with pytest.raises(ValueError, match="Box has not been created"):
_ = box.length


@pytest.mark.parametrize(
("value_type", "key"),
[
(UInt64, "key"),
(Bytes, b"key"),
(String, Bytes(b"key")),
(BigUInt, String("key")),
(arc4.String, "Key"),
(arc4.DynamicArray, b"Key"),
],
)
def test_box_init_with_key(
context: AlgopyTestContext,
value_type: type,
key: bytes | str | Bytes | String,
) -> None:
box = Box(value_type, key=key)
assert not bool(box)
assert len(box.key) > 0

key_bytes = (
String(as_string(key)).bytes if isinstance(key, str | String) else Bytes(as_bytes(key))
)
assert box.key == key_bytes

with pytest.raises(ValueError, match="Box has not been created"):
_ = box.value

with pytest.raises(ValueError, match="Box has not been created"):
_ = box.length

0 comments on commit 70a632a

Please sign in to comment.