Skip to content

Commit 57a8f13

Browse files
refactor: move box storage onto ApplicationContextData (#12)
* refactor: move box storage onto ApplicationContextData Require app reference when interacting with boxes * refactor: moving getters setters of global local state to ledger context * refactor: more laconic names for methods/vars referencing application -> app --------- Co-authored-by: Altynbek Orumbayev <altynbek.orumbayev@makerx.com.au>
1 parent 439b563 commit 57a8f13

File tree

22 files changed

+508
-261
lines changed

22 files changed

+508
-261
lines changed

examples/box/test_contract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ def test_enums(context: AlgopyTestContext) -> None:
2222
oca, txn = contract.read_enums()
2323

2424
# Assert
25-
assert context.ledger.get_box(b"oca") == op.itob(oca.native)
26-
assert context.ledger.get_box(b"txn") == op.itob(txn.native)
25+
assert context.ledger.get_box(contract, b"oca") == op.itob(oca.native)
26+
assert context.ledger.get_box(contract, b"txn") == op.itob(txn.native)

examples/marketplace/test_contract.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_first_deposit(
6363
nonce=test_nonce,
6464
)
6565
listing_value = ListingValue.from_bytes(
66-
context.ledger.get_box(b"listings" + listing_key.bytes)
66+
context.ledger.get_box(contract, b"listings" + listing_key.bytes)
6767
)
6868
assert listing_value.deposited == UInt64(10)
6969

@@ -100,7 +100,7 @@ def test_deposit(
100100
)
101101

102102
# Assert
103-
assert context.ledger.box_exists(b"listings" + listing_key.bytes)
103+
assert context.ledger.box_exists(contract, b"listings" + listing_key.bytes)
104104

105105

106106
def test_set_price(
@@ -129,7 +129,7 @@ def test_set_price(
129129

130130
# Assert
131131
updated_listing = ListingValue.from_bytes(
132-
context.ledger.get_box(b"listings" + listing_key.bytes)
132+
context.ledger.get_box(contract, b"listings" + listing_key.bytes)
133133
)
134134
assert updated_listing.unitary_price == test_unitary_price
135135

@@ -173,7 +173,7 @@ def test_buy(
173173

174174
# Assert
175175
updated_listing = ListingValue.from_bytes(
176-
context.ledger.get_box(b"listings" + listing_key.bytes)
176+
context.ledger.get_box(contract, b"listings" + listing_key.bytes)
177177
)
178178
assert updated_listing.deposited == initial_deposit.native - test_buy_quantity.native
179179
assert (
@@ -206,7 +206,7 @@ def test_withdraw(
206206
contract.withdraw(asset=test_asset, nonce=test_nonce)
207207

208208
# Assert
209-
assert not context.ledger.box_exists(b"listings" + listing_key.bytes)
209+
assert not context.ledger.box_exists(contract, b"listings" + listing_key.bytes)
210210
assert len(context.txn.last_group.itxn_groups) == 2
211211

212212
payment_txn = context.txn.last_group.get_itxn_group(0).payment(0)

examples/proof_of_attendance/test_contract.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def test_confirm_attendance(
5757
confirm()
5858

5959
# Assert
60-
assert context.ledger.get_box(key_prefix + context.default_sender.bytes) == algopy.op.itob(
61-
1001
62-
)
60+
assert context.ledger.get_box(
61+
contract, key_prefix + context.default_sender.bytes
62+
) == algopy.op.itob(1001)
6363

6464

6565
@pytest.mark.parametrize(
@@ -88,7 +88,9 @@ def test_claim_poa(
8888
fee=algopy.UInt64(0),
8989
asset_amount=algopy.UInt64(0),
9090
)
91-
context.ledger.set_box(key_prefix + context.default_sender.bytes, algopy.op.itob(dummy_poa.id))
91+
context.ledger.set_box(
92+
contract, key_prefix + context.default_sender.bytes, algopy.op.itob(dummy_poa.id)
93+
)
9294

9395
# Act
9496
claim = getattr(contract, claim_poa)

src/algopy_testing/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from algopy_testing import arc4, gtxn, itxn
2-
from algopy_testing._context_helpers.context_storage import (
3-
algopy_testing_context,
4-
get_test_context,
5-
)
2+
from algopy_testing._context_helpers.context_storage import algopy_testing_context
63
from algopy_testing._itxn_loader import ITxnGroupLoader, ITxnLoader
74
from algopy_testing._value_generators.arc4 import ARC4ValueGenerator
85
from algopy_testing.context import AlgopyTestContext
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from algopy_testing._context_helpers.context_storage import (
22
algopy_testing_context,
3-
get_test_context,
43
lazy_context,
54
)
65
from algopy_testing._context_helpers.ledger_context import LedgerContext
@@ -10,6 +9,5 @@
109
"LedgerContext",
1110
"TransactionContext",
1211
"algopy_testing_context",
13-
"get_test_context",
1412
"lazy_context",
1513
]

src/algopy_testing/_context_helpers/context_storage.py

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,20 @@
2020
_var: ContextVar[AlgopyTestContext] = ContextVar("_var")
2121

2222

23-
# functions for use by algopy_testing implementations that shouldn't be exposed to the user
24-
def get_test_context() -> AlgopyTestContext:
25-
try:
26-
result = _var.get()
27-
except LookupError:
28-
raise ValueError(
29-
"Test context is not initialized! Use `with algopy_testing_context()` to "
30-
"access the context manager."
31-
) from None
32-
return result
33-
34-
3523
class _InternalContext:
3624
"""For accessing implementation specific functions, with a convenient
3725
single entry point for other modules to import Also allows for a single
3826
place to check and provide."""
3927

4028
@property
4129
def value(self) -> AlgopyTestContext:
42-
return get_test_context()
30+
try:
31+
return _var.get()
32+
except LookupError:
33+
raise ValueError(
34+
"Test context is not initialized! Use `with algopy_testing_context()` to "
35+
"access the context manager."
36+
) from None
4337

4438
@property
4539
def ledger(self) -> LedgerContext:
@@ -67,32 +61,20 @@ def active_group(self) -> TransactionGroup:
6761
return group
6862

6963
@property
70-
def active_application(self) -> algopy.Application:
71-
return self.ledger.get_application(self.active_group.active_app_id)
64+
def active_app(self) -> algopy.Application:
65+
return self.ledger.get_app(self.active_group.active_app_id)
66+
67+
@property
68+
def active_app_id(self) -> int:
69+
return self.active_group.active_app_id
7270

7371
def get_app_data(
7472
self,
7573
app: algopy.Contract | algopy.Application | algopy.UInt64 | int,
7674
) -> ApplicationContextData:
77-
from algopy_testing.models import Application, Contract
78-
from algopy_testing.primitives import UInt64
79-
80-
if isinstance(app, Contract):
81-
app_id = app.__app_id__
82-
elif isinstance(app, Application):
83-
app_id = app.id.value
84-
elif isinstance(app, UInt64):
85-
app_id = app.value
86-
elif isinstance(app, int):
87-
app_id = app
88-
else:
89-
raise TypeError("invalid type")
90-
if app_id == 0:
91-
app_id = self.active_group.active_app_id
92-
try:
93-
return self.ledger.application_data[app_id]
94-
except KeyError:
95-
raise ValueError("Unknown app id, is there an active transaction?") from None
75+
if app == 0:
76+
app = self.active_group.active_app_id
77+
return self.ledger._get_app_data(app)
9678

9779
def get_asset_data(self, asset_id: int | algopy.UInt64) -> AssetFields:
9880
try:

0 commit comments

Comments
 (0)