-
Notifications
You must be signed in to change notification settings - Fork 2
(add): inter-canister-calls example added #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mzurs
wants to merge
3
commits into
dfinity:main
Choose a base branch
from
mzurs:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| type StubbornSetResult = variant { | ||
| Ok : null; | ||
| Err : text; | ||
| }; | ||
|
|
||
| type IncrementResult = variant { | ||
| Ok : null; | ||
| Err : text; | ||
| }; | ||
|
|
||
| type GetResult = variant { | ||
| Ok : nat; | ||
| Err : text; | ||
| }; | ||
|
|
||
| type SignMessageResult = variant { | ||
| Ok : text; | ||
| Err : text; | ||
| }; | ||
|
|
||
| service : { | ||
| call_get_and_set : ( principal, nat ) -> (nat); | ||
| set_then_get : (principal, nat) -> (nat); | ||
| call_get : (principal) -> (GetResult); | ||
| call_increment : (principal) -> (IncrementResult); | ||
| stubborn_set : (principal, nat) -> (StubbornSetResult); | ||
| sign_message : (text) -> (SignMessageResult); | ||
| } | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| service : { | ||
| get : () -> (nat) query; | ||
| set : (nat) -> (); | ||
| increment : () -> (); | ||
| get_and_set : (nat) -> (nat); | ||
| } |
Binary file not shown.
90 changes: 90 additions & 0 deletions
90
examples/inter_canister_calls/inter_canister_calls_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import sys | ||
| import os | ||
| import unittest | ||
| import ic | ||
|
|
||
| # The example needs to have the module in its sys path, so we traverse | ||
| # up until we find the pocket_ic package. | ||
| script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| sys.path.append(os.path.dirname(os.path.dirname(script_dir))) | ||
|
|
||
| from pocket_ic import PocketIC, SubnetConfig | ||
|
|
||
| # This test suite is for testing inter-canister calls between a caller canister and a counter canister. | ||
|
|
||
| # Note: The inter-canister-calls example take from https://github.com/dfinity/examples/tree/master/rust/inter-canister-calls | ||
|
|
||
| class InterCanisterCallsTest(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| # This is run for every test individually. | ||
| # We create a new PocketIC with a single NNS subnet. | ||
| self.pic = PocketIC(SubnetConfig(nns=True)) | ||
|
|
||
| with open( | ||
| os.path.join(script_dir, "caller.did"), "r", encoding="utf-8" | ||
| ) as candid_file: | ||
| candid = candid_file.read() | ||
|
|
||
| with open(os.path.join(script_dir, "caller.wasm"), "rb") as wasm_file: | ||
| wasm_module = wasm_file.read() | ||
|
|
||
| # Install the caller canister on the NNS subnet. | ||
| self.caller: ic.Canister = self.pic.create_and_install_canister_with_candid( | ||
| candid, bytes(wasm_module) | ||
| ) | ||
|
|
||
| with open( | ||
| os.path.join(script_dir, "counter.did"), "r", encoding="utf-8" | ||
| ) as candid_file: | ||
| candid = candid_file.read() | ||
|
|
||
| with open(os.path.join(script_dir, "counter.wasm"), "rb") as wasm_file: | ||
| wasm_module = wasm_file.read() | ||
|
|
||
| # Install the counter canister on the NNS subnet. | ||
| self.counter: ic.Canister = self.pic.create_and_install_canister_with_candid( | ||
| candid, bytes(wasm_module) | ||
| ) | ||
|
|
||
| return super().setUp() | ||
|
|
||
| def test_call_get_and_set(self): | ||
| # Test setting the counter to a new value and getting the old value | ||
| new_value = 42 | ||
| old_value = self.caller.call_get_and_set(self.counter.canister_id.to_str(), new_value) | ||
| self.assertEqual(old_value, [0]) # Assuming the initial value is 0 | ||
|
|
||
| def test_set_then_get(self): | ||
| # Test setting the counter and then getting the current value | ||
| new_value = 7 | ||
| self.caller.set_then_get(self.counter.canister_id.to_str(), new_value) | ||
| current_value = self.counter.get() | ||
| self.assertEqual(current_value, [7]) | ||
|
|
||
| def test_call_increment(self): | ||
| self.caller.set_then_get(self.counter.canister_id.to_str(), 7) | ||
| # Test incrementing the counter | ||
| self.caller.call_increment(self.counter.canister_id.to_str()) | ||
| current_value = self.counter.get() | ||
| # Assuming the previous value was 7 | ||
| self.assertEqual(current_value, [8]) | ||
|
|
||
| def test_call_get(self): | ||
| self.caller.set_then_get(self.counter.canister_id.to_str(), 7) | ||
| self.caller.call_increment(self.counter.canister_id.to_str()) | ||
|
|
||
| # Test getting the current counter value | ||
| current_value = self.caller.call_get(self.counter.canister_id.to_str()) | ||
| # Assuming the previous value was 8 | ||
| self.assertEqual(current_value, [{'Ok':8}]) | ||
|
|
||
| def test_stubborn_set(self): | ||
| # Test setting the counter value with retries | ||
| new_value = 42 | ||
| result = self.caller.stubborn_set(self.counter.canister_id.to_str(), new_value) | ||
| self.assertEqual(result[0], {'Ok':None}) | ||
| current_value = self.counter.get() | ||
| self.assertEqual(current_value, [42]) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.