Skip to content

Commit

Permalink
test: extra test cases for accessing implicit/explicit keyed local/gl…
Browse files Browse the repository at this point in the history
…obal state
  • Loading branch information
aorumbayev committed Aug 12, 2024
1 parent bcd3ee9 commit 2cda537
Show file tree
Hide file tree
Showing 16 changed files with 2,451 additions and 131 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ unfixable = [
"src/**" = [
"PT", # no pytest rules
]
"tests/**" = [
"FBT003", # allow boolean positional arguments
]
"src/algopy_testing/models/logicsig.py" = ["ARG002"]
"scripts/**/*.py" = ["T201"]
"scripts/refresh_test_artifacts.py" = ["S603"]
Expand Down
2 changes: 1 addition & 1 deletion src/algopy_testing/_context_helpers/context_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_account_data(self, account_public_key: str) -> AccountContextData:
@contextmanager
def algopy_testing_context(
*,
default_sender: algopy.Account | None = None,
default_sender: str | None = None,
) -> Generator[AlgopyTestContext, None, None]:
from algopy_testing.context import AlgopyTestContext

Expand Down
6 changes: 3 additions & 3 deletions src/algopy_testing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class AlgopyTestContext:
def __init__(
self,
*,
default_sender: algopy.Account | None = None,
default_sender: str | None = None,
template_vars: dict[str, typing.Any] | None = None,
) -> None:
import algopy

# TODO: remove direct reads of data mappings outside of context_storage
self._default_sender = default_sender or algopy.Account(
algosdk.account.generate_account()[1]
self._default_sender = algopy.Account(
default_sender or algosdk.account.generate_account()[1]
)
self._template_vars: dict[str, typing.Any] = template_vars or {}

Expand Down
4 changes: 2 additions & 2 deletions tests/arc4/test_emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_emit(get_avm_result: AVMInvoker, context: AlgopyTestContext) -> None:
MAX_UINT64,
algopy.Bytes(b"world"),
16,
False, # noqa: FBT003
False,
b"test",
"greetings",
)
Expand All @@ -78,7 +78,7 @@ def test_emit(get_avm_result: AVMInvoker, context: AlgopyTestContext) -> None:
arc4.BigUIntN[typing.Literal[256]](512),
arc4.UFixedNxM[typing.Literal[32], typing.Literal[8]]("42.94967295"),
arc4.BigUFixedNxM[typing.Literal[256], typing.Literal[16]]("25.5"),
arc4.Bool(True), # noqa: FBT003
arc4.Bool(True),
arc4.StaticArray(arc4.UInt8(1), arc4.UInt8(2), arc4.UInt8(3)),
arc4.DynamicArray(arc4.UInt16(1), arc4.UInt16(2), arc4.UInt16(3)),
arc4.Tuple((arc4.UInt32(1), arc4.UInt64(2), arc4.String("hello"))),
Expand Down
2 changes: 1 addition & 1 deletion tests/arc4/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

_arc4_string = arc4.String("hello")
_arc4_uint64 = arc4.UInt64(42)
_arc4_bool = arc4.Bool(True) # noqa: FBT003
_arc4_bool = arc4.Bool(True)


class Swapped(arc4.Struct):
Expand Down
2 changes: 1 addition & 1 deletion tests/arc4/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

_arc4_string = arc4.String("hello")
_arc4_uint8 = arc4.UInt8(42)
_arc4_bool = arc4.Bool(True) # noqa: FBT003
_arc4_bool = arc4.Bool(True)

_test_data = [
(
Expand Down
197 changes: 191 additions & 6 deletions tests/artifacts/StateOps/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,29 @@ def verify_itxn_ops(self) -> None:

class GlobalStateContract(ARC4Contract):
def __init__(self) -> None:
# Implicit key state variables
self.implicit_key_arc4_uint = GlobalState(arc4.UInt64(1337))
self.implicit_key_arc4_string = GlobalState(arc4.String("Hello"))
self.implicit_key_arc4_byte = GlobalState(arc4.Byte(0))
self.implicit_key_arc4_bool = GlobalState(arc4.Bool(True))
self.implicit_key_arc4_address = GlobalState(arc4.Address(Global.creator_address))
self.implicit_key_arc4_uint128 = GlobalState(arc4.UInt128(2**100))
self.implicit_key_arc4_dynamic_bytes = GlobalState(arc4.DynamicBytes(b"dynamic bytes"))

# Explicit key state variables
self.arc4_uint = GlobalState(arc4.UInt64(1337), key="explicit_key_arc4_uint")
self.arc4_string = GlobalState(arc4.String("Hello"), key="explicit_key_arc4_string")
self.arc4_byte = GlobalState(arc4.Byte(0), key="explicit_key_arc4_byte")
self.arc4_bool = GlobalState(arc4.Bool(True), key="explicit_key_arc4_bool")
self.arc4_address = GlobalState(
arc4.Address(Global.creator_address), key="explicit_key_arc4_address"
)
self.arc4_uint128 = GlobalState(arc4.UInt128(2**100), key="explicit_key_arc4_uint128")
self.arc4_dynamic_bytes = GlobalState(
arc4.DynamicBytes(b"dynamic bytes"), key="explicit_key_arc4_dynamic_bytes"
)

# Getter methods for implicit key state variables
@arc4.abimethod()
def get_implicit_key_arc4_uint(self) -> arc4.UInt64:
return self.implicit_key_arc4_uint.value
Expand All @@ -374,6 +392,27 @@ def get_implicit_key_arc4_uint(self) -> arc4.UInt64:
def get_implicit_key_arc4_string(self) -> arc4.String:
return self.implicit_key_arc4_string.value

@arc4.abimethod()
def get_implicit_key_arc4_byte(self) -> arc4.Byte:
return self.implicit_key_arc4_byte.value

@arc4.abimethod()
def get_implicit_key_arc4_bool(self) -> arc4.Bool:
return self.implicit_key_arc4_bool.value

@arc4.abimethod()
def get_implicit_key_arc4_address(self) -> arc4.Address:
return self.implicit_key_arc4_address.value

@arc4.abimethod()
def get_implicit_key_arc4_uint128(self) -> arc4.UInt128:
return self.implicit_key_arc4_uint128.value

@arc4.abimethod()
def get_implicit_key_arc4_dynamic_bytes(self) -> arc4.DynamicBytes:
return self.implicit_key_arc4_dynamic_bytes.value

# Getter methods for explicit key state variables
@arc4.abimethod()
def get_arc4_uint(self) -> arc4.UInt64:
return self.arc4_uint.value
Expand All @@ -382,25 +421,130 @@ def get_arc4_uint(self) -> arc4.UInt64:
def get_arc4_string(self) -> arc4.String:
return self.arc4_string.value

@arc4.abimethod()
def get_arc4_byte(self) -> arc4.Byte:
return self.arc4_byte.value

@arc4.abimethod()
def get_arc4_bool(self) -> arc4.Bool:
return self.arc4_bool.value

@arc4.abimethod()
def get_arc4_address(self) -> arc4.Address:
return self.arc4_address.value

@arc4.abimethod()
def get_arc4_uint128(self) -> arc4.UInt128:
return self.arc4_uint128.value

@arc4.abimethod()
def get_arc4_dynamic_bytes(self) -> arc4.DynamicBytes:
return self.arc4_dynamic_bytes.value

# Setter methods for implicit key state variables
@arc4.abimethod()
def set_implicit_key_arc4_uint(self, value: arc4.UInt64) -> None:
self.implicit_key_arc4_uint.value = value

@arc4.abimethod()
def set_implicit_key_arc4_string(self, value: arc4.String) -> None:
self.implicit_key_arc4_string.value = value

@arc4.abimethod()
def set_implicit_key_arc4_byte(self, value: arc4.Byte) -> None:
self.implicit_key_arc4_byte.value = value

@arc4.abimethod()
def set_implicit_key_arc4_bool(self, value: arc4.Bool) -> None:
self.implicit_key_arc4_bool.value = value

@arc4.abimethod()
def set_implicit_key_arc4_address(self, value: arc4.Address) -> None:
self.implicit_key_arc4_address.value = value

@arc4.abimethod()
def set_implicit_key_arc4_uint128(self, value: arc4.UInt128) -> None:
self.implicit_key_arc4_uint128.value = value

@arc4.abimethod()
def set_implicit_key_arc4_dynamic_bytes(self, value: arc4.DynamicBytes) -> None:
self.implicit_key_arc4_dynamic_bytes.value = value.copy()

# Setter methods for explicit key state variables
@arc4.abimethod()
def set_arc4_uint(self, value: arc4.UInt64) -> None:
self.arc4_uint.value = value

@arc4.abimethod()
def set_arc4_string(self, value: arc4.String) -> None:
self.arc4_string.value = value

@arc4.abimethod()
def set_arc4_byte(self, value: arc4.Byte) -> None:
self.arc4_byte.value = value

@arc4.abimethod()
def set_arc4_bool(self, value: arc4.Bool) -> None:
self.arc4_bool.value = value

@arc4.abimethod()
def set_arc4_address(self, value: arc4.Address) -> None:
self.arc4_address.value = value

@arc4.abimethod()
def set_arc4_uint128(self, value: arc4.UInt128) -> None:
self.arc4_uint128.value = value

@arc4.abimethod()
def set_arc4_dynamic_bytes(self, value: arc4.DynamicBytes) -> None:
self.arc4_dynamic_bytes.value = value.copy()


class LocalStateContract(ARC4Contract):
def __init__(self) -> None:
self.implicit_key_arc4_uint = LocalState(
arc4.UInt64,
)
self.implicit_key_arc4_string = LocalState(
arc4.String,
)
# Implicit key state variables
self.implicit_key_arc4_uint = LocalState(arc4.UInt64)
self.implicit_key_arc4_string = LocalState(arc4.String)
self.implicit_key_arc4_byte = LocalState(arc4.Byte)
self.implicit_key_arc4_bool = LocalState(arc4.Bool)
self.implicit_key_arc4_address = LocalState(arc4.Address)
self.implicit_key_arc4_uint128 = LocalState(arc4.UInt128)
self.implicit_key_arc4_dynamic_bytes = LocalState(arc4.DynamicBytes)

# Explicit key state variables
self.arc4_uint = LocalState(arc4.UInt64, key="explicit_key_arc4_uint")
self.arc4_string = LocalState(arc4.String, key="explicit_key_arc4_string")
self.arc4_byte = LocalState(arc4.Byte, key="explicit_key_arc4_byte")
self.arc4_bool = LocalState(arc4.Bool, key="explicit_key_arc4_bool")
self.arc4_address = LocalState(arc4.Address, key="explicit_key_arc4_address")
self.arc4_uint128 = LocalState(arc4.UInt128, key="explicit_key_arc4_uint128")
self.arc4_dynamic_bytes = LocalState(
arc4.DynamicBytes, key="explicit_key_arc4_dynamic_bytes"
)

@arc4.abimethod(allow_actions=["OptIn"])
def opt_in(self) -> None:
self.implicit_key_arc4_uint[Global.creator_address] = arc4.UInt64(1337)
self.implicit_key_arc4_string[Global.creator_address] = arc4.String("Hello")
self.implicit_key_arc4_byte[Global.creator_address] = arc4.Byte(0)
self.implicit_key_arc4_bool[Global.creator_address] = arc4.Bool(True)
self.implicit_key_arc4_address[Global.creator_address] = arc4.Address(
Global.creator_address
)
self.implicit_key_arc4_uint128[Global.creator_address] = arc4.UInt128(2**100)
self.implicit_key_arc4_dynamic_bytes[Global.creator_address] = arc4.DynamicBytes(
b"dynamic bytes"
)

self.arc4_uint[Global.creator_address] = arc4.UInt64(1337)
self.arc4_string[Global.creator_address] = arc4.String("Hello")
self.arc4_byte[Global.creator_address] = arc4.Byte(0)
self.arc4_bool[Global.creator_address] = arc4.Bool(True)
self.arc4_address[Global.creator_address] = arc4.Address(Global.creator_address)
self.arc4_uint128[Global.creator_address] = arc4.UInt128(2**100)
self.arc4_dynamic_bytes[Global.creator_address] = arc4.DynamicBytes(b"dynamic bytes")

# Getter methods for implicit key state variables
@arc4.abimethod()
def get_implicit_key_arc4_uint(self, a: Account) -> arc4.UInt64:
return self.implicit_key_arc4_uint[a]
Expand All @@ -409,10 +553,51 @@ def get_implicit_key_arc4_uint(self, a: Account) -> arc4.UInt64:
def get_implicit_key_arc4_string(self, a: Account) -> arc4.String:
return self.implicit_key_arc4_string[a]

@arc4.abimethod()
def get_implicit_key_arc4_byte(self, a: Account) -> arc4.Byte:
return self.implicit_key_arc4_byte[a]

@arc4.abimethod()
def get_implicit_key_arc4_bool(self, a: Account) -> arc4.Bool:
return self.implicit_key_arc4_bool[a]

@arc4.abimethod()
def get_implicit_key_arc4_address(self, a: Account) -> arc4.Address:
return self.implicit_key_arc4_address[a]

@arc4.abimethod()
def get_implicit_key_arc4_uint128(self, a: Account) -> arc4.UInt128:
return self.implicit_key_arc4_uint128[a]

@arc4.abimethod()
def get_implicit_key_arc4_dynamic_bytes(self, a: Account) -> arc4.DynamicBytes:
return self.implicit_key_arc4_dynamic_bytes[a]

# Getter methods for explicit key state variables
@arc4.abimethod()
def get_arc4_uint(self, a: Account) -> arc4.UInt64:
return self.arc4_uint[a]

@arc4.abimethod()
def get_arc4_string(self, a: Account) -> arc4.String:
return self.arc4_string[a]

@arc4.abimethod()
def get_arc4_byte(self, a: Account) -> arc4.Byte:
return self.arc4_byte[a]

@arc4.abimethod()
def get_arc4_bool(self, a: Account) -> arc4.Bool:
return self.arc4_bool[a]

@arc4.abimethod()
def get_arc4_address(self, a: Account) -> arc4.Address:
return self.arc4_address[a]

@arc4.abimethod()
def get_arc4_uint128(self, a: Account) -> arc4.UInt128:
return self.arc4_uint128[a]

@arc4.abimethod()
def get_arc4_dynamic_bytes(self, a: Account) -> arc4.DynamicBytes:
return self.arc4_dynamic_bytes[a]
Loading

0 comments on commit 2cda537

Please sign in to comment.