Skip to content

Commit 3a70b80

Browse files
authored
feat: algorand-python-testing preview integration (#28)
* feat: algorand-python-testing preview integration * chore: patching bugs * chore: patching artifacts
1 parent 5fdabda commit 3a70b80

32 files changed

+237
-145
lines changed

examples/generators/production_python_smart_contract_python/.algokit.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ audit = { commands = [
3434
], description = 'Audit with pip-audit' }
3535
lint = { commands = [
3636
'poetry run black --check .',
37-
'poetry run ruff .',
37+
'poetry run ruff check .',
3838
'poetry run mypy',
3939
], description = 'Perform linting' }
4040
audit-teal = { commands = [

examples/generators/production_python_smart_contract_python/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,5 @@ node_modules
176176

177177
# AlgoKit
178178
debug_traces/
179-
180179
.algokit/static-analysis/tealer/
180+
.algokit/sources

examples/generators/production_python_smart_contract_python/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
entry: poetry run ruff
1919
language: system
2020
types: [ python ]
21-
args: [ --fix ]
21+
args: [ "check", "--fix" ]
2222
require_serial: false
2323
additional_dependencies: [ ]
2424
minimum_pre_commit_version: '0'

examples/generators/production_python_smart_contract_python/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ For pull requests and pushes to `main` branch against this repository the follow
102102
- Code formatting is checked using [Black](https://github.com/psf/black)
103103
- Linting is checked using [Ruff](https://github.com/charliermarsh/ruff)
104104
- Types are checked using [mypy](https://mypy-lang.org/)
105-
- Python tests are executed using [pytest](https://docs.pytest.org/)
105+
- The base framework for testing is [pytest](https://docs.pytest.org/), and the project includes two separate kinds of tests:
106+
- - `Algorand Python` smart contract unit tests, that are run using [`algorand-python-testing`](https://pypi.org/project/algorand-python-testing/), which are executed in a Python intepreter emulating major AVM behaviour
107+
- - Python `ApplicationClient` tests that are run against `algokit localnet` and test the behaviour in a real network enviornment
106108
- Smart contract artifacts are built
107109
- Smart contract artifacts are checked for [output stability](https://github.com/algorandfoundation/algokit-cli/blob/main/docs/articles/output_stability.md)
108110
- Smart contract is deployed to a AlgoKit LocalNet instance

examples/generators/production_python_smart_contract_python/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ readme = "README.md"
77

88
[tool.poetry.dependencies]
99
python = "^3.12"
10-
algokit-utils = "^2.2.0"
10+
algokit-utils = "^2.3.0"
1111
python-dotenv = "^1.0.0"
1212
algorand-python = "^1.0.0"
13+
algorand-python-testing = {git = "https://github.com/algorandfoundation/puya.git", rev = "fix/algopy_testing_refinements", subdirectory = "algopy_testing"}
1314

1415
[tool.poetry.group.dev.dependencies]
1516
algokit-client-generator = "^1.1.3"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import algokit_utils
2+
import pytest
3+
from algokit_utils import get_localnet_default_account
4+
from algokit_utils.config import config
5+
from algosdk.v2client.algod import AlgodClient
6+
from algosdk.v2client.indexer import IndexerClient
7+
8+
from smart_contracts.artifacts.hello_world.client import HelloWorldClient
9+
10+
11+
@pytest.fixture(scope="session")
12+
def hello_world_client(
13+
algod_client: AlgodClient, indexer_client: IndexerClient
14+
) -> HelloWorldClient:
15+
config.configure(
16+
debug=True,
17+
# trace_all=True,
18+
)
19+
20+
client = HelloWorldClient(
21+
algod_client,
22+
creator=get_localnet_default_account(algod_client),
23+
indexer_client=indexer_client,
24+
)
25+
26+
client.deploy(
27+
on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
28+
on_update=algokit_utils.OnUpdate.AppendApp,
29+
)
30+
return client
31+
32+
33+
def test_says_hello(hello_world_client: HelloWorldClient) -> None:
34+
result = hello_world_client.hello(name="World")
35+
36+
assert result.return_value == "Hello, World"
37+
38+
39+
def test_simulate_says_hello_with_correct_budget_consumed(
40+
hello_world_client: HelloWorldClient, algod_client: AlgodClient
41+
) -> None:
42+
result = (
43+
hello_world_client.compose().hello(name="World").hello(name="Jane").simulate()
44+
)
45+
46+
assert result.abi_results[0].return_value == "Hello, World"
47+
assert result.abi_results[1].return_value == "Hello, Jane"
48+
assert result.simulate_response["txn-groups"][0]["app-budget-consumed"] < 100
Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,25 @@
1-
import algokit_utils
2-
import pytest
3-
from algokit_utils import get_localnet_default_account
4-
from algokit_utils.config import config
5-
from algosdk.v2client.algod import AlgodClient
6-
from algosdk.v2client.indexer import IndexerClient
7-
8-
from smart_contracts.artifacts.hello_world.client import HelloWorldClient
9-
1+
from collections.abc import Generator
102

11-
@pytest.fixture(scope="session")
12-
def hello_world_client(
13-
algod_client: AlgodClient, indexer_client: IndexerClient
14-
) -> HelloWorldClient:
15-
config.configure(
16-
debug=True,
17-
# trace_all=True,
18-
)
19-
20-
client = HelloWorldClient(
21-
algod_client,
22-
creator=get_localnet_default_account(algod_client),
23-
indexer_client=indexer_client,
24-
)
3+
import pytest
4+
from algopy_testing import AlgopyTestContext, algopy_testing_context
255

26-
client.deploy(
27-
on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
28-
on_update=algokit_utils.OnUpdate.AppendApp,
29-
)
30-
return client
6+
from smart_contracts.hello_world.contract import HelloWorld
317

328

33-
def test_says_hello(hello_world_client: HelloWorldClient) -> None:
34-
result = hello_world_client.hello(name="World")
9+
@pytest.fixture()
10+
def context() -> Generator[AlgopyTestContext, None, None]:
11+
with algopy_testing_context() as ctx:
12+
yield ctx
13+
ctx.reset()
3514

36-
assert result.return_value == "Hello, World"
3715

16+
def test_hello(context: AlgopyTestContext) -> None:
17+
# Arrange
18+
dummy_input = context.any_string()
19+
contract = HelloWorld()
3820

39-
def test_simulate_says_hello_with_correct_budget_consumed(
40-
hello_world_client: HelloWorldClient, algod_client: AlgodClient
41-
) -> None:
42-
result = (
43-
hello_world_client.compose().hello(name="World").hello(name="Jane").simulate()
44-
)
21+
# Act
22+
output = contract.hello(dummy_input)
4523

46-
assert result.abi_results[0].return_value == "Hello, World"
47-
assert result.abi_results[1].return_value == "Hello, Jane"
48-
assert result.simulate_response["txn-groups"][0]["app-budget-consumed"] < 100
24+
# Assert
25+
assert output == f"Hello, {dummy_input}"

examples/generators/production_python_smart_contract_typescript/.algokit.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ audit = { commands = [
3434
], description = 'Audit with pip-audit' }
3535
lint = { commands = [
3636
'poetry run black --check .',
37-
'poetry run ruff .',
37+
'poetry run ruff check .',
3838
'poetry run mypy',
3939
], description = 'Perform linting' }
4040
audit-teal = { commands = [

examples/generators/production_python_smart_contract_typescript/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,5 @@ node_modules
176176

177177
# AlgoKit
178178
debug_traces/
179-
180179
.algokit/static-analysis/tealer/
180+
.algokit/sources

examples/generators/production_python_smart_contract_typescript/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
entry: poetry run ruff
1919
language: system
2020
types: [ python ]
21-
args: [ --fix ]
21+
args: [ "check", "--fix" ]
2222
require_serial: false
2323
additional_dependencies: [ ]
2424
minimum_pre_commit_version: '0'

0 commit comments

Comments
 (0)