Skip to content

Commit e4f9457

Browse files
committed
tests: add additional tests for Global op
1 parent a30d85a commit e4f9457

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/_algopy_testing/op/global_values.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class GlobalFields(TypedDict, total=False):
2525
logic_sig_version: algopy.UInt64
2626
round: algopy.UInt64
2727
latest_timestamp: algopy.UInt64
28-
group_id: algopy.Bytes # TODO: mock/infer from active txn group?
29-
caller_application_id: algopy.Application
28+
group_id: algopy.Bytes
3029
caller_application_id: algopy.UInt64
3130
asset_create_min_balance: algopy.UInt64
3231
asset_opt_in_min_balance: algopy.UInt64
@@ -75,6 +74,26 @@ def group_size(self) -> algopy.UInt64:
7574
group = lazy_context.active_group
7675
return UInt64(len(group.txns))
7776

77+
@property
78+
def group_id(self) -> algopy.Bytes:
79+
from _algopy_testing import op
80+
81+
try:
82+
return self._fields["group_id"]
83+
except KeyError:
84+
group_hash = hash(lazy_context.active_group)
85+
group_hash_bytes = group_hash.to_bytes((group_hash.bit_length() + 7) // 8)
86+
return op.sha256(group_hash_bytes)
87+
88+
@property
89+
def round(self) -> algopy.UInt64:
90+
try:
91+
return self._fields["round"]
92+
except KeyError:
93+
# size of groups will suffice as an increasing integer similar to ground
94+
num_groups = len(lazy_context.txn._groups) + 1
95+
return UInt64(num_groups)
96+
7897
@property
7998
def zero_address(self) -> algopy.Account:
8099
try:

tests/test_op.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,3 +1008,36 @@ def test_contains() -> None:
10081008
contract = MyContract()
10091009

10101010
contract.approval_program()
1011+
1012+
1013+
def test_globals(context: AlgopyTestContext) -> None:
1014+
creator = context.any.account()
1015+
app = context.any.application(creator=creator)
1016+
txn1 = context.any.txn.application_call(app_id=app)
1017+
with context.txn.create_group(gtxns=[txn1]):
1018+
first_group_id = algopy.Global.group_id
1019+
first_timestamp = algopy.Global.latest_timestamp
1020+
assert first_group_id.length == 32
1021+
assert first_timestamp != 0
1022+
assert algopy.Global.group_size == 1
1023+
assert algopy.Global.round == 1
1024+
assert algopy.Global.caller_application_id == 0
1025+
assert algopy.Global.creator_address == creator
1026+
assert algopy.Global.current_application_id == app
1027+
assert algopy.Global.current_application_address == app.address
1028+
1029+
txn2 = context.any.txn.payment()
1030+
txn3 = context.any.txn.application_call()
1031+
caller = context.any.application()
1032+
context.ledger.patch_global_fields(caller_application_id=caller.id)
1033+
with context.txn.create_group(gtxns=[txn2, txn3]):
1034+
second_group_id = algopy.Global.group_id
1035+
second_timestamp = algopy.Global.latest_timestamp
1036+
assert second_group_id.length == 32
1037+
assert algopy.Global.group_size == 2
1038+
assert algopy.Global.round == 2
1039+
assert algopy.Global.caller_application_id == caller.id
1040+
assert algopy.Global.caller_application_address == caller.address
1041+
1042+
assert first_group_id != second_group_id, "expected unique group ids"
1043+
assert first_timestamp <= second_timestamp, "expected unique group ids"

0 commit comments

Comments
 (0)