Skip to content

Commit

Permalink
feat: include ARC4 results in log, and handle > 15 ARC4 arguments (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx authored Aug 22, 2024
1 parent 94e529f commit fd83ee8
Show file tree
Hide file tree
Showing 52 changed files with 386 additions and 302 deletions.
1 change: 0 additions & 1 deletion docs/testing-guide/arc4-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ For all `algopy.arc4` types with and without respective _value generator_, insta

```{testsetup}
import algopy
import algopy_testing
from algopy_testing import algopy_testing_context
# Create the context manager for snippets below
Expand Down
1 change: 0 additions & 1 deletion docs/testing-guide/avm-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ For 'primitive `algopy` types such as `Account`, `Application`, `Asset`, `UInt64

```{testsetup}
import algopy
import algopy_testing
from algopy_testing import algopy_testing_context
# Create the context manager for snippets below
Expand Down
51 changes: 23 additions & 28 deletions docs/testing-guide/opcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ The [coverage](coverage.md) file provides a comprehensive list of all opcodes an

```{testsetup}
import algopy
import algopy_testing
from algopy_testing import algopy_testing_context
# Create the context manager for snippets below
Expand All @@ -29,7 +28,7 @@ The following opcodes are demonstrated:
- `op.ecdsa_verify`

```{testcode}
import algopy.op as op
from algopy import op
# SHA256 hash
data = algopy.Bytes(b"Hello, World!")
Expand Down Expand Up @@ -59,7 +58,7 @@ The following opcodes are demonstrated:
- `op.setbit_uint64`

```{testcode}
import algopy.op as op
from algopy import op
# Addition with carry
result, carry = op.addw(algopy.UInt64(2**63), algopy.UInt64(2**63))
Expand All @@ -80,7 +79,7 @@ These types necessitate interaction with the transaction context:
### algopy.op.Global

```{testcode}
import algopy.op as op
from algopy import op
class MyContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand All @@ -102,12 +101,12 @@ assert result == algopy.UInt64(101000)
### algopy.op.Txn

```{testcode}
import algopy.op as op
from algopy import op
class MyContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
def check_txn_fields(self) -> algopy.Bytes:
return op.Txn.sender
def check_txn_fields(self) -> algopy.arc4.Address:
return algopy.arc4.Address(op.Txn.sender)
... # setup context (below assumes available under 'ctx' variable)
Expand All @@ -121,7 +120,7 @@ assert result == custom_sender
### algopy.op.AssetHoldingGet

```{testcode}
import algopy.op as op
from algopy import op
class AssetContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand All @@ -141,7 +140,7 @@ assert result == algopy.UInt64(5000)
### algopy.op.AppGlobal

```{testcode}
import algopy.op as op
from algopy import op
class StateContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand All @@ -162,7 +161,7 @@ assert stored_value == 42
### algopy.op.Block

```{testcode}
import algopy.op as op
from algopy import op
class BlockInfoContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand All @@ -180,7 +179,7 @@ assert seed == algopy.op.itob(123456)
### algopy.op.AcctParamsGet

```{testcode}
import algopy.op as op
from algopy import op
class AccountParamsContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand All @@ -205,7 +204,7 @@ class AppParamsContract(algopy.ARC4Contract):
def get_app_creator(self, app_id: algopy.Application) -> algopy.arc4.Address:
creator, exists = algopy.op.AppParamsGet.app_creator(app_id)
assert exists
return creator
return algopy.arc4.Address(creator)
... # setup context (below assumes available under 'ctx' variable)
Expand All @@ -218,8 +217,7 @@ assert creator == context.default_sender
### algopy.op.AssetParamsGet

```{testcode}
from algopy_testing import algopy_testing_context
import algopy.op as op
from algopy import op
class AssetParamsContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand All @@ -239,8 +237,7 @@ assert total == algopy.UInt64(1000000)
### algopy.op.Box

```{testcode}
from algopy_testing import algopy_testing_context
import algopy.op as op
from algopy import op
class BoxStorageContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
Expand Down Expand Up @@ -329,23 +326,22 @@ assert result == 11
```{testcode}
from unittest.mock import patch, MagicMock
import algopy
from algopy_testing.primitives import Bytes
def test_mock_vrf_verify():
mock_result = (Bytes(b'mock_output'), True)
mock_result = (algopy.Bytes(b'mock_output'), True)
with patch('algopy.op.vrf_verify', return_value=mock_result) as mock_vrf_verify:
result = algopy.op.vrf_verify(
algopy.op.VrfVerify.VrfAlgorand,
Bytes(b'proof'),
Bytes(b'message'),
Bytes(b'public_key')
algopy.Bytes(b'proof'),
algopy.Bytes(b'message'),
algopy.Bytes(b'public_key')
)
assert result == mock_result
mock_vrf_verify.assert_called_once_with(
algopy.op.VrfVerify.VrfAlgorand,
Bytes(b'proof'),
Bytes(b'message'),
Bytes(b'public_key')
algopy.Bytes(b'proof'),
algopy.Bytes(b'message'),
algopy.Bytes(b'public_key')
)
test_mock_vrf_verify()
Expand All @@ -356,19 +352,18 @@ test_mock_vrf_verify()
```{testcode}
from unittest.mock import patch, MagicMock
import algopy
from algopy_testing.primitives import Bytes
def test_mock_elliptic_curve_decompress():
mock_result = (Bytes(b'x_coord'), Bytes(b'y_coord'))
mock_result = (algopy.Bytes(b'x_coord'), algopy.Bytes(b'y_coord'))
with patch('algopy.op.EllipticCurve.decompress', return_value=mock_result) as mock_decompress:
result = algopy.op.EllipticCurve.decompress(
algopy.op.EC.BN254g1,
Bytes(b'compressed_point')
algopy.Bytes(b'compressed_point')
)
assert result == mock_result
mock_decompress.assert_called_once_with(
algopy.op.EC.BN254g1,
Bytes(b'compressed_point')
algopy.Bytes(b'compressed_point')
)
test_mock_elliptic_curve_decompress()
Expand Down
1 change: 0 additions & 1 deletion docs/testing-guide/signature-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Test Algorand smart signatures (LogicSigs) with ease using the Algorand Python T

```{testsetup}
import algopy
import algopy_testing
from algopy_testing import algopy_testing_context
# Create the context manager for snippets below
Expand Down
1 change: 0 additions & 1 deletion docs/testing-guide/state-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

```{testsetup}
import algopy
import algopy_testing
from algopy_testing import algopy_testing_context
# Create the context manager for snippets below
Expand Down
4 changes: 2 additions & 2 deletions docs/testing-guide/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ from algopy_testing import AlgopyTestContext, algopy_testing_context
class SimpleContract(algopy.ARC4Contract):
@algopy.arc4.abimethod
def check_sender(self) -> algopy.Bytes:
return algopy.Txn.sender
def check_sender(self) -> algopy.arc4.Address:
return algopy.arc4.Address(algopy.Txn.sender)
...
# Create a contract instance
Expand Down
17 changes: 8 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Source = "https://github.com/algorandfoundation/puya/tree/main/algopy_testing"
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/algopy", 'src/algopy_testing']
packages = ["src/algopy", 'src/algopy_testing', 'src/_algopy_testing']

[[tool.hatch.envs.all.matrix]]
python = ["3.12"]
Expand Down Expand Up @@ -126,7 +126,6 @@ clean_dist = "rm -rf dist"

# docs environment
[tool.hatch.envs.docs]
detached = true
path = ".venv.docs"
type = "virtual"
python = "3.12"
Expand All @@ -141,13 +140,13 @@ dependencies = [
"ipykernel",
"pytest",
"py-algorand-sdk",
"algokit-utils",
"pycryptodomex>=3.6.0,<4",
"pynacl>=1.4.0,<2",
"ecdsa>=0.17.0",
"coincurve>=19.0.1",
"algorand-python",
"algorand-python-testing @ {root:uri}",
]
# environment has algopy_testing included as an editable dependency
# however it also includes the package dependencies
# the stubs can't be in the same as the project when it is editable
# so explicitly remove them
post-install-commands = [
"hatch run docs:pip uninstall -y algorand-python",
]

[tool.hatch.envs.docs.scripts]
Expand Down
14 changes: 7 additions & 7 deletions src/_algopy_testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from _algopy_testing import arc4, gtxn, itxn
from _algopy_testing._context_helpers.context_storage import algopy_testing_context
from _algopy_testing._context_helpers.ledger_context import LedgerContext
from _algopy_testing._context_helpers.txn_context import TransactionContext
from _algopy_testing._itxn_loader import ITxnGroupLoader, ITxnLoader
from _algopy_testing._value_generators.arc4 import ARC4ValueGenerator
from _algopy_testing._value_generators.avm import AVMValueGenerator
from _algopy_testing._value_generators.txn import TxnValueGenerator
from _algopy_testing.context import AlgopyTestContext
from _algopy_testing.context_helpers.context_storage import algopy_testing_context
from _algopy_testing.context_helpers.ledger_context import LedgerContext
from _algopy_testing.context_helpers.txn_context import TransactionContext
from _algopy_testing.decorators.subroutine import subroutine
from _algopy_testing.enums import OnCompleteAction, TransactionType
from _algopy_testing.itxn_loader import ITxnGroupLoader, ITxnLoader
from _algopy_testing.models import (
Account,
Application,
Expand All @@ -24,6 +21,9 @@
)
from _algopy_testing.primitives import BigUInt, Bytes, String, UInt64
from _algopy_testing.state import Box, BoxMap, BoxRef, GlobalState, LocalState
from _algopy_testing.value_generators.arc4 import ARC4ValueGenerator
from _algopy_testing.value_generators.avm import AVMValueGenerator
from _algopy_testing.value_generators.txn import TxnValueGenerator

# TODO: clean up and ensure only algopy_testing namespace specific user facing abstractions
# are exposed Only keep the _value_generators, ledger_context, txn_context,
Expand Down
13 changes: 0 additions & 13 deletions src/_algopy_testing/_context_helpers/__init__.py

This file was deleted.

Loading

0 comments on commit fd83ee8

Please sign in to comment.