Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: algorand-python-testing preview integration #28

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ audit = { commands = [
], description = 'Audit with pip-audit' }
lint = { commands = [
'poetry run black --check .',
'poetry run ruff .',
'poetry run ruff check .',
'poetry run mypy',
], description = 'Perform linting' }
audit-teal = { commands = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ node_modules

# AlgoKit
debug_traces/

.algokit/static-analysis/tealer/
.algokit/sources
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
entry: poetry run ruff
language: system
types: [ python ]
args: [ --fix ]
args: [ "check", "--fix" ]
require_serial: false
additional_dependencies: [ ]
minimum_pre_commit_version: '0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ For pull requests and pushes to `main` branch against this repository the follow
- Code formatting is checked using [Black](https://github.com/psf/black)
- Linting is checked using [Ruff](https://github.com/charliermarsh/ruff)
- Types are checked using [mypy](https://mypy-lang.org/)
- Python tests are executed using [pytest](https://docs.pytest.org/)
- The base framework for testing is [pytest](https://docs.pytest.org/), and the project includes two separate kinds of tests:
- - `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
- - Python `ApplicationClient` tests that are run against `algokit localnet` and test the behaviour in a real network enviornment
- Smart contract artifacts are built
- Smart contract artifacts are checked for [output stability](https://github.com/algorandfoundation/algokit-cli/blob/main/docs/articles/output_stability.md)
- Smart contract is deployed to a AlgoKit LocalNet instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ readme = "README.md"

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

[tool.poetry.group.dev.dependencies]
algokit-client-generator = "^1.1.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import algokit_utils
import pytest
from algokit_utils import get_localnet_default_account
from algokit_utils.config import config
from algosdk.v2client.algod import AlgodClient
from algosdk.v2client.indexer import IndexerClient

from smart_contracts.artifacts.hello_world.client import HelloWorldClient


@pytest.fixture(scope="session")
def hello_world_client(
algod_client: AlgodClient, indexer_client: IndexerClient
) -> HelloWorldClient:
config.configure(
debug=True,
# trace_all=True,
)

client = HelloWorldClient(
algod_client,
creator=get_localnet_default_account(algod_client),
indexer_client=indexer_client,
)

client.deploy(
on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
on_update=algokit_utils.OnUpdate.AppendApp,
)
return client


def test_says_hello(hello_world_client: HelloWorldClient) -> None:
result = hello_world_client.hello(name="World")

assert result.return_value == "Hello, World"


def test_simulate_says_hello_with_correct_budget_consumed(
hello_world_client: HelloWorldClient, algod_client: AlgodClient
) -> None:
result = (
hello_world_client.compose().hello(name="World").hello(name="Jane").simulate()
)

assert result.abi_results[0].return_value == "Hello, World"
assert result.abi_results[1].return_value == "Hello, Jane"
assert result.simulate_response["txn-groups"][0]["app-budget-consumed"] < 100
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
import algokit_utils
import pytest
from algokit_utils import get_localnet_default_account
from algokit_utils.config import config
from algosdk.v2client.algod import AlgodClient
from algosdk.v2client.indexer import IndexerClient

from smart_contracts.artifacts.hello_world.client import HelloWorldClient

from collections.abc import Generator

@pytest.fixture(scope="session")
def hello_world_client(
algod_client: AlgodClient, indexer_client: IndexerClient
) -> HelloWorldClient:
config.configure(
debug=True,
# trace_all=True,
)

client = HelloWorldClient(
algod_client,
creator=get_localnet_default_account(algod_client),
indexer_client=indexer_client,
)
import pytest
from algopy_testing import AlgopyTestContext, algopy_testing_context

client.deploy(
on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
on_update=algokit_utils.OnUpdate.AppendApp,
)
return client
from smart_contracts.hello_world.contract import HelloWorld


def test_says_hello(hello_world_client: HelloWorldClient) -> None:
result = hello_world_client.hello(name="World")
@pytest.fixture()
def context() -> Generator[AlgopyTestContext, None, None]:
with algopy_testing_context() as ctx:
yield ctx
ctx.reset()

assert result.return_value == "Hello, World"

def test_hello(context: AlgopyTestContext) -> None:
# Arrange
dummy_input = context.any_string()
contract = HelloWorld()

def test_simulate_says_hello_with_correct_budget_consumed(
hello_world_client: HelloWorldClient, algod_client: AlgodClient
) -> None:
result = (
hello_world_client.compose().hello(name="World").hello(name="Jane").simulate()
)
# Act
output = contract.hello(dummy_input)

assert result.abi_results[0].return_value == "Hello, World"
assert result.abi_results[1].return_value == "Hello, Jane"
assert result.simulate_response["txn-groups"][0]["app-budget-consumed"] < 100
# Assert
assert output == f"Hello, {dummy_input}"
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ audit = { commands = [
], description = 'Audit with pip-audit' }
lint = { commands = [
'poetry run black --check .',
'poetry run ruff .',
'poetry run ruff check .',
'poetry run mypy',
], description = 'Perform linting' }
audit-teal = { commands = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ node_modules

# AlgoKit
debug_traces/

.algokit/static-analysis/tealer/
.algokit/sources
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
entry: poetry run ruff
language: system
types: [ python ]
args: [ --fix ]
args: [ "check", "--fix" ]
require_serial: false
additional_dependencies: [ ]
minimum_pre_commit_version: '0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ For pull requests and pushes to `main` branch against this repository the follow
- Code formatting is checked using [Black](https://github.com/psf/black)
- Linting is checked using [Ruff](https://github.com/charliermarsh/ruff)
- Types are checked using [mypy](https://mypy-lang.org/)
- Typescript `ApplicationClient` tests against `algokit localnet` are executed using [jest](https://jestjs.io/)
- Smart contract artifacts are built
- Smart contract artifacts are checked for [output stability](https://github.com/algorandfoundation/algokit-cli/blob/main/docs/articles/output_stability.md)
- Smart contract is deployed to a AlgoKit LocalNet instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ readme = "README.md"

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

[tool.poetry.group.dev.dependencies]
black = {extras = ["d"], version = "*"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ node_modules

# AlgoKit
debug_traces/

.algokit/static-analysis/tealer/
.algokit/sources
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ readme = "README.md"

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

[tool.poetry.group.dev.dependencies]
algokit-client-generator = "^1.1.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ node_modules

# AlgoKit
debug_traces/

.algokit/static-analysis/tealer/
.algokit/sources
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ readme = "README.md"

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

[tool.poetry.group.dev.dependencies]
puyapy = "*"
Expand Down
2 changes: 1 addition & 1 deletion examples/production_python/.algokit.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ audit = { commands = [
], description = 'Audit with pip-audit' }
lint = { commands = [
'poetry run black --check .',
'poetry run ruff .',
'poetry run ruff check .',
'poetry run mypy',
], description = 'Perform linting' }
audit-teal = { commands = [
Expand Down
2 changes: 1 addition & 1 deletion examples/production_python/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ node_modules

# AlgoKit
debug_traces/

.algokit/static-analysis/tealer/
.algokit/sources
2 changes: 1 addition & 1 deletion examples/production_python/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
entry: poetry run ruff
language: system
types: [ python ]
args: [ --fix ]
args: [ "check", "--fix" ]
require_serial: false
additional_dependencies: [ ]
minimum_pre_commit_version: '0'
Expand Down
4 changes: 3 additions & 1 deletion examples/production_python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ For pull requests and pushes to `main` branch against this repository the follow
- Code formatting is checked using [Black](https://github.com/psf/black)
- Linting is checked using [Ruff](https://github.com/charliermarsh/ruff)
- Types are checked using [mypy](https://mypy-lang.org/)
- Python tests are executed using [pytest](https://docs.pytest.org/)
- The base framework for testing is [pytest](https://docs.pytest.org/), and the project includes two separate kinds of tests:
- - `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
- - Python `ApplicationClient` tests that are run against `algokit localnet` and test the behaviour in a real network enviornment
- Smart contract artifacts are built
- Smart contract artifacts are checked for [output stability](https://github.com/algorandfoundation/algokit-cli/blob/main/docs/articles/output_stability.md)
- Smart contract is deployed to a AlgoKit LocalNet instance
Expand Down
3 changes: 2 additions & 1 deletion examples/production_python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ readme = "README.md"

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

[tool.poetry.group.dev.dependencies]
algokit-client-generator = "^1.1.3"
Expand Down
48 changes: 48 additions & 0 deletions examples/production_python/tests/hello_world_client_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import algokit_utils
import pytest
from algokit_utils import get_localnet_default_account
from algokit_utils.config import config
from algosdk.v2client.algod import AlgodClient
from algosdk.v2client.indexer import IndexerClient

from smart_contracts.artifacts.hello_world.client import HelloWorldClient


@pytest.fixture(scope="session")
def hello_world_client(
algod_client: AlgodClient, indexer_client: IndexerClient
) -> HelloWorldClient:
config.configure(
debug=True,
# trace_all=True,
)

client = HelloWorldClient(
algod_client,
creator=get_localnet_default_account(algod_client),
indexer_client=indexer_client,
)

client.deploy(
on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
on_update=algokit_utils.OnUpdate.AppendApp,
)
return client


def test_says_hello(hello_world_client: HelloWorldClient) -> None:
result = hello_world_client.hello(name="World")

assert result.return_value == "Hello, World"


def test_simulate_says_hello_with_correct_budget_consumed(
hello_world_client: HelloWorldClient, algod_client: AlgodClient
) -> None:
result = (
hello_world_client.compose().hello(name="World").hello(name="Jane").simulate()
)

assert result.abi_results[0].return_value == "Hello, World"
assert result.abi_results[1].return_value == "Hello, Jane"
assert result.simulate_response["txn-groups"][0]["app-budget-consumed"] < 100
Loading