-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test all Call struct variants and rename ArgDecoderConfig to DecoderC…
…onfig
- Loading branch information
Showing
3 changed files
with
89 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,17 +1,78 @@ | ||
use candid::Principal; | ||
use ic_cdk::api::management_canister::main::{CanisterIdRecord, CreateCanisterArgument}; | ||
use ic_cdk::prelude::*; | ||
use candid::Encode; | ||
use ic_cdk::{call::DecoderConfig, prelude::*}; | ||
|
||
/// This endpoint is to be called by the following Call struct invocation. | ||
#[update] | ||
async fn create_canister_via_struct() -> Principal { | ||
let res: (CanisterIdRecord,) = Call::new(Principal::management_canister(), "create_canister") | ||
.with_args((CreateCanisterArgument::default(),)) | ||
.with_cycles(1_000_000_000_000) | ||
async fn echo(arg: u32) -> u32 { | ||
arg | ||
} | ||
|
||
#[update] | ||
async fn call_struct() { | ||
let num = 1u32; | ||
let bytes: Vec<u8> = Encode!(&num).unwrap(); | ||
|
||
// 1. Various ways to call* | ||
// 1.1 call() | ||
let res: (u32,) = Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.call() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.0, num); | ||
// 1.2 call_raw() | ||
let res = Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.call_raw() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res, bytes); | ||
// 1.3 call_with_decoder_config() | ||
let config = DecoderConfig::default(); | ||
let res: (u32,) = Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.call_with_decoder_config(&config) | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.0, num); | ||
// 1.4 call_raw_with_decoder_config() | ||
Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.call_and_forget() | ||
.unwrap(); | ||
|
||
// 2. Various ways to config the call | ||
// 2.1 with_raw_args() | ||
let res: (u32,) = Call::new(id(), "echo") | ||
.with_raw_args(&bytes) | ||
.call() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.0, num); | ||
// 2.2 with_guaranteed_response() | ||
let res: (u32,) = Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.with_guaranteed_response() | ||
.call() | ||
.await | ||
.unwrap(); | ||
res.0.canister_id | ||
assert_eq!(res.0, num); | ||
// 2.3 change_timeout() | ||
let res: (u32,) = Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.change_timeout(5) | ||
.call() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.0, num); | ||
// 2.4 with_cycles() | ||
let res: (u32,) = Call::new(id(), "echo") | ||
.with_args((num,)) | ||
.with_cycles(100_000) | ||
.call() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.0, num); | ||
} | ||
|
||
fn main() {} |
This file contains 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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
use candid::Principal; | ||
use ic_cdk_e2e_tests::cargo_build_canister; | ||
use pocket_ic::common::rest::RawEffectivePrincipal; | ||
use pocket_ic::{call_candid, PocketIc}; | ||
use pocket_ic::{call_candid, PocketIcBuilder}; | ||
|
||
#[test] | ||
fn call_struct() { | ||
let pic = PocketIc::new(); | ||
let pic = PocketIcBuilder::new() | ||
.with_application_subnet() | ||
.with_nonmainnet_features(true) | ||
.build(); | ||
let wasm = cargo_build_canister("call_struct"); | ||
let canister_id = pic.create_canister(); | ||
pic.add_cycles(canister_id, 100_000_000_000_000); | ||
pic.install_canister(canister_id, wasm, vec![], None); | ||
let _: (Principal,) = call_candid( | ||
let _: () = call_candid( | ||
&pic, | ||
canister_id, | ||
RawEffectivePrincipal::None, | ||
"create_canister_via_struct", | ||
"call_struct", | ||
(), | ||
) | ||
.expect("Error calling create_canister_via_struct"); | ||
.expect("Error calling call_struct"); | ||
} |
This file contains 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