PocketIC assert update or query

Is there a way to write, or does anyone have an example to share, a test that asserts whether an endpoint is set as update or query with PocketIC?

I’m looking for examples in Rust or PicJS, as I use both. I know I’m asking for a lot :smile:.

IIUC, you have a canister with a method and you want to determine whether that is a query only or can also be called as an update call?

I can’t think of a way with the current version. Why do you want to do that as an integration test as opposed to just inspecting the wasm (or .wat)?

Because I only have a suite of integration tests, but fair point—if it can be done in a unit test, that works too. Do you know if there’s an example somewhere of such a test?

Note: This question is more of a nice-to-have from my perspective, not a must, so no need to try too hard. If it’s not possible, it’s not. :wink:

I don’t have a ready example, but here is a sketch how I would do it.
Transform the wasm to textual format:

wasm2wat canister.wasm -o canister.wat

There is also a rust library, if you want to formulate the test in rust.
In the canister.wat, there is an export section, which looks something like this:

   ...
  (export "canister_query triple" (func $canister_query_triple))
  (export "canister_query const" (func $canister_query_const))
  (export "canister_update inc" (func $canister_update_inc))
  (export "canister_init" (func $canister_init))
  ...

Based on this format, you can write some filter/match to achieve your goals.
Note that the string with the whitespace, not the $blah, is what you should match for.

Also note that methods annotated with “query” can always also be called as an update, and there are some special rules for composite queries.

1 Like

You can try to add a test for a candid file (similar to this) and then a method is a query if and only if it is labeled as such in the candid file.

1 Like

You can try calling the method as a query call and if it is an update method, the error message will say so.

2 Likes

Thanks for the suggestions!

that would only work reliably though if you use ic_cdk macros to define the canister’s public endpoints, if you use #[export and #[candid_method then the candid file relies on #[candid_method while the IC relies on #[export

1 Like