Skip to content

Commit 0bb5eba

Browse files
committed
fix: when generating app/asset id's skip any that might already be reserved
1 parent c033b93 commit 0bb5eba

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/algopy_testing/_context_helpers/ledger_context.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@ def __init__(self) -> None:
2727
self.blocks: dict[int, dict[str, int]] = {}
2828
self.global_fields: GlobalFields = get_default_global_fields()
2929

30-
self.asset_id = iter(range(1001, 2**64))
31-
self.app_id = iter(range(1001, 2**64))
30+
self._asset_id = iter(range(1001, 2**64))
31+
self._app_id = iter(range(1001, 2**64))
32+
33+
def get_next_asset_id(self) -> int:
34+
while True:
35+
asset_id = next(self._asset_id)
36+
if asset_id not in self.asset_data:
37+
return asset_id
38+
39+
def get_next_app_id(self) -> int:
40+
while True:
41+
app_id = next(self._app_id)
42+
if app_id not in self.app_data:
43+
return app_id
3244

3345
def get_account(self, address: str) -> algopy.Account:
3446
"""Get an account by address.

src/algopy_testing/_value_generators/avm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def asset(
118118
raise ValueError("Asset with such ID already exists in testing context!")
119119

120120
# TODO: 1.0 ensure passed fields are valid names and types
121-
new_asset = algopy.Asset(asset_id or next(lazy_context.ledger.asset_id))
121+
new_asset = algopy.Asset(asset_id or lazy_context.ledger.get_next_asset_id())
122122
default_asset_fields = {
123123
"total": lazy_context.any.uint64(),
124124
"decimals": lazy_context.any.uint64(1, 6),
@@ -162,7 +162,7 @@ def application( # type: ignore[misc]
162162
"""
163163
import algopy_testing
164164

165-
new_app_id = id if id is not None else next(lazy_context.ledger.app_id)
165+
new_app_id = id if id is not None else lazy_context.ledger.get_next_app_id()
166166

167167
if new_app_id in lazy_context.ledger.app_data:
168168
raise ValueError(

tests/test_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def test_context_reset() -> None:
118118
with pytest.raises(ValueError, match="No group transactions found"):
119119
assert context.txn.last_group
120120
assert len(context.txn._groups) == 0
121-
assert next(context.ledger.asset_id) == 1001
122-
assert next(context.ledger.app_id) == 1001
121+
assert context.ledger.get_next_asset_id() == 1001
122+
assert context.ledger.get_next_app_id() == 1001
123123

124124

125125
def test_algopy_testing_context() -> None:

0 commit comments

Comments
 (0)