Skip to content

Commit

Permalink
fix: filename generation for trace files and add a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Oct 30, 2024
1 parent 7d5e5fb commit d726706
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/algokit_utils/_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,13 @@ def simulate_and_persist_response(
response = simulate_response(atc_to_simulate, algod_client)
txn_results = response.simulate_response["txn-groups"]

txn_types = [txn_result["txn-results"][0]["txn-result"]["txn"]["txn"]["type"] for txn_result in txn_results]
txn_types_count = {txn_type: txn_types.count(txn_type) for txn_type in set(txn_types)}
txn_types = [
txn["txn-result"]["txn"]["txn"]["type"] for txn_result in txn_results for txn in txn_result["txn-results"]
]
txn_types_count = {}
for txn_type in txn_types:
if txn_type not in txn_types_count:
txn_types_count[txn_type] = txn_types.count(txn_type)
txn_types_str = "_".join([f"{count}{txn_type}" for txn_type, count in txn_types_count.items()])

last_round = response.simulate_response["last-round"]
Expand Down
85 changes: 84 additions & 1 deletion tests/test_debug_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from unittest.mock import Mock

Expand All @@ -18,7 +19,7 @@
AtomicTransactionComposer,
TransactionWithSigner,
)
from algosdk.transaction import PaymentTxn
from algosdk.transaction import AssetTransferTxn, PaymentTxn

from tests.conftest import get_unique_name

Expand Down Expand Up @@ -157,3 +158,85 @@ def test_simulate_and_persist_response(
while trace_file_path.exists():
tmp_atc = atc.clone()
simulate_and_persist_response(tmp_atc, cwd, client_fixture.algod_client, buffer_size_mb=0.01)


@pytest.mark.parametrize(
("transactions", "expected_filename_part"),
[
# Single transaction types
({"pay": 1}, "1pay"),
({"axfer": 1}, "1axfer"),
({"appl": 1}, "1appl"),
# Multiple of same type
({"pay": 3}, "3pay"),
({"axfer": 2}, "2axfer"),
({"appl": 4}, "4appl"),
# Mixed combinations
({"pay": 2, "axfer": 1, "appl": 3}, "2pay_1axfer_3appl"),
({"pay": 1, "axfer": 1, "appl": 1}, "1pay_1axfer_1appl"),
],
)
def test_simulate_response_filename_generation( # noqa: PLR0913
transactions: dict[str, int],
expected_filename_part: str,
tmp_path_factory: pytest.TempPathFactory,
client_fixture: ApplicationClient,
funded_account: Account,
monkeypatch: pytest.MonkeyPatch,
) -> None:
cwd = tmp_path_factory.mktemp("cwd")
sp = client_fixture.algod_client.suggested_params()
atc = AtomicTransactionComposer()
signer = AccountTransactionSigner(funded_account.private_key)

# Add payment transactions
for i in range(transactions.get("pay", 0)):
payment = PaymentTxn(
sender=funded_account.address,
receiver=client_fixture.app_address,
amt=1_000_000 * (i + 1),
note=f"Payment{i+1}".encode(),
sp=sp,
) # type: ignore[no-untyped-call]
atc.add_transaction(TransactionWithSigner(payment, signer))

# Add asset transfer transactions
for i in range(transactions.get("axfer", 0)):
asset_transfer = AssetTransferTxn(
sender=funded_account.address,
receiver=client_fixture.app_address,
amt=1_000 * (i + 1),
index=1, # Using asset ID 1 for test
sp=sp,
) # type: ignore[no-untyped-call]
atc.add_transaction(TransactionWithSigner(asset_transfer, signer))

# Add app calls
for i in range(transactions.get("appl", 0)):
client_fixture.compose_call(atc, "hello", name=f"test{i+1}")

# Mock datetime
mock_datetime = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc)

class MockDateTime:
@classmethod
def now(cls, tz: timezone | None = None) -> datetime: # noqa: ARG003
return mock_datetime

monkeypatch.setattr("algokit_utils._debugging.datetime", MockDateTime)

response = simulate_and_persist_response(atc, cwd, client_fixture.algod_client)
last_round = response.simulate_response["last-round"]
expected_filename = f"20230101_120000_lr{last_round}_{expected_filename_part}.trace.avm.json"

# Verify file exists with expected name
output_path = cwd / "debug_traces"
files = list(output_path.iterdir())
assert len(files) == 1
assert files[0].name == expected_filename

# Verify transaction count
trace_file_content = json.loads(files[0].read_text())
txn_results = trace_file_content["txn-groups"][0]["txn-results"]
expected_total_txns = sum(transactions.values())
assert len(txn_results) == expected_total_txns

0 comments on commit d726706

Please sign in to comment.