Skip to content

Commit

Permalink
Adding asdict() to Router's BareCallActions (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaffi committed Feb 1, 2023
1 parent f16fa75 commit 76347f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Unreleased

## Added

* Improved error handling for tuple type mismatch: added information on position and expected type. ([#655](https://github.com/algorand/pyteal/pull/655))
* Added an `asdict()` method to `ast.router.BareCallActions`. ([#656](https://github.com/algorand/pyteal/pull/656))

## Fixed

Expand Down
10 changes: 10 additions & 0 deletions pyteal/ast/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ def __post_init__(self):
"https://pyteal.readthedocs.io/en/latest/abi.html#registering-bare-app-calls"
)

def asdict(self) -> dict[str, OnCompleteAction]:
return {
"clear_state": self.clear_state,
"close_out": self.close_out,
"delete_application": self.delete_application,
"no_op": self.no_op,
"opt_in": self.opt_in,
"update_application": self.update_application,
}

def is_empty(self) -> bool:
for action_field in fields(self):
action: OnCompleteAction = getattr(self, action_field.name)
Expand Down
29 changes: 29 additions & 0 deletions pyteal/ast/router_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,35 @@ def test_bare_call_config_clear_state_failure():
assert "Attempt to construct clear state program from bare app call" in str(tie)


def test_bare_call_actions_asdict():
no_action = pt.OnCompleteAction()
del_action = pt.OnCompleteAction(action=pt.Int(1), call_config=pt.CallConfig.ALL)
close_action = pt.OnCompleteAction(action=pt.Int(2), call_config=pt.CallConfig.CALL)

bca = pt.BareCallActions(
delete_application=del_action,
close_out=close_action,
)

bcad = bca.asdict()
assert set(bcad.keys()) == {
"clear_state",
"close_out",
"delete_application",
"no_op",
"opt_in",
"update_application",
}
assert bcad == {
"clear_state": no_action,
"close_out": close_action,
"delete_application": del_action,
"no_op": no_action,
"opt_in": no_action,
"update_application": no_action,
}


def test_router_register_method_clear_state_failure():
router = pt.Router("doomedToFail")

Expand Down

0 comments on commit 76347f2

Please sign in to comment.