I have a test
function using ic-test-state-machine
:
pub fn suggections(
env: &StateMachine,
canister_id: Principal,
offset: Option<i32>,
limit: Option<i64>,
) -> Result<Vec<Suggection>, ic_test_state_machine_client::CallError> {
let res: Result<(Vec<Suggection>,), CallError> =
query_candid(env, canister_id, "suggections", (offset, limit));
assert!(res.is_ok(), "should okkkkkkkkkkkkkkk pls");
res.map(|r| r.0)
}
Here is my Suggection
’s definition, it had implment CandidType
:
#[derive(
Queryable,
Debug,
CandidType,
Serialize,
Deserialize,
Selectable,
Insertable,
Default,
Clone,
Identifiable,
PartialEq,
)]
#[diesel(primary_key(id))]
#[diesel(table_name = crate::schema::suggections)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct Suggection {
pub id: i32,
pub title: String,
pub content: Option<String>,
pub principal: String,
pub created_at: time::OffsetDateTime,
}
The time::OffsetDateTime
also implmented CandidType
:
#[cfg(feature = "icp")]
impl CandidType for OffsetDateTime {
fn _ty() -> Type {
Type::Nat
}
fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where
S: Serializer,
{
let res = self.unix_timestamp_nanos();
let res: u128 = res.abs().try_into().unwrap();
let nat = candid::Nat::from(res);
let res = serializer.serialize_nat(&nat);
res
}
}
But when i run
cargo test --package suggection_box --target x86_64-unknown-linux-musl -- --nocapture
I got:
thread 'get_suggections_should_work' panicked at 'Failed to decode response as candid type (alloc::vec::Vec<suggection_box::models::Suggection>,):
error: Fail to decode argument 0 from table0 to vec record {
id : int32;
title : text;
"principal" : text;
content : opt text;
created_at : nat;
}
bytes: [68, 73, 68, 76, 3, 109, 1, 108, 5, 219, 183, 1, 117, 152, 171, 236, 129, 1, 113, 174, 157, 177, 144, 1, 113, 185, 154, 222, 203, 1, 2, 170, 172, 217, 208, 6, 125, 110, 113, 1, 0, 1, 1, 0, 0, 0, 7, 116, 105, 116, 108, 101, 95, 49, 63, 115, 54, 109, 53, 119, 45, 117, 51, 55, 99, 107, 45, 119, 102, 121, 52, 102, 45, 120, 103, 108, 108, 116, 45, 106, 101, 114, 104, 117, 45, 97, 120, 51, 51, 109, 45, 102, 114, 110, 53, 115, 45, 115, 97, 114, 105, 53, 45, 115, 106, 99, 53, 100, 45, 55, 108, 55, 97, 110, 45, 120, 97, 101, 1, 9, 99, 111, 110, 116, 101, 110, 116, 95, 50, 128, 160, 240, 150, 224, 181, 240, 2]
utf8: DIDLml۷u���q����q��������}nqtitl