From 213aa57bbc3b05f5795445bf6f6152dff4692375 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Tue, 12 Sep 2023 15:50:32 -0600 Subject: [PATCH] finish composite query example --- .../canisters/canister1/index.did | 1 + .../canisters/canister1/index.ts | 32 +++++----- .../canisters/canister2/index.ts | 2 +- examples/composite_queries/dfx.json | 6 +- examples/composite_queries/test/tests.ts | 63 +++++++++++-------- 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/examples/composite_queries/canisters/canister1/index.did b/examples/composite_queries/canisters/canister1/index.did index fb3e022565..c62ac41a3b 100644 --- a/examples/composite_queries/canisters/canister1/index.did +++ b/examples/composite_queries/canisters/canister1/index.did @@ -7,5 +7,6 @@ service: () -> { simpleQuery: () -> (text) query; simpleUpdate: () -> (text); incCounter: () -> (nat) query; + incCanister1: () -> (nat) query; incCanister2: () -> (nat) query; } diff --git a/examples/composite_queries/canisters/canister1/index.ts b/examples/composite_queries/canisters/canister1/index.ts index 60f11dc438..6f30a65fdd 100644 --- a/examples/composite_queries/canisters/canister1/index.ts +++ b/examples/composite_queries/canisters/canister1/index.ts @@ -3,13 +3,6 @@ import Canister2 from '../canister2'; // Composite query calling a query class Canister1 extends Service { - // canister1 = new Canister1( - // Principal.fromText( - // process.env.CANISTER1_PRINCIPAL ?? - // ic.trap('process.env.CANISTER1_PRINCIPAL is undefined') - // ) - // ); - canister2 = new Canister2( Principal.fromText( process.env.CANISTER2_PRINCIPAL ?? @@ -31,7 +24,7 @@ class Canister1 extends Service { } // Manual composite query calling a manual query - @query([], text) + @query([], text, { manual: true }) async totallyManualQuery(): Promise> { ic.reply(await ic.call(this.canister2.manualQuery), text); } @@ -64,23 +57,32 @@ class Canister1 extends Service { @query([], nat) async incCounter(): Promise { this.counter += 1n; + return this.counter; } - // Composite query calling queries on the same canister. SHOULDN'T WORK - // @query([], nat) - // async incCanister1(): Promise { - // this.counter += 1n; + // Composite query calling queries on the same canister + @query([], nat) + async incCanister1(): Promise { + this.counter += 1n; + + const canister1AResult = await ic.call(this.incCounter); + + const canister1BResult = await ic.call(this.incCounter); - // return await ic.call(this.canister1.incCounter); - // } + return this.counter + canister1AResult + canister1BResult; + } // Composite query calling queries that modify the state @query([], nat) async incCanister2(): Promise { this.counter += 1n; - return await ic.call(this.canister2.incCounter); + const canister2AResult = await ic.call(this.canister2.incCounter); + + const canister2BResult = await ic.call(this.canister2.incCounter); + + return this.counter + canister2AResult + canister2BResult; } } diff --git a/examples/composite_queries/canisters/canister2/index.ts b/examples/composite_queries/canisters/canister2/index.ts index ead7951851..0c23bd210d 100644 --- a/examples/composite_queries/canisters/canister2/index.ts +++ b/examples/composite_queries/canisters/canister2/index.ts @@ -28,7 +28,7 @@ export default class extends Service { return 'Hello from a Canister 2 update'; } - @query([], text) + @query([], text, { manual: true }) manualQuery(): Manual { ic.reply('Hello from Canister 2 manual query', text); } diff --git a/examples/composite_queries/dfx.json b/examples/composite_queries/dfx.json index 27b6da5daa..7fb3b3400a 100644 --- a/examples/composite_queries/dfx.json +++ b/examples/composite_queries/dfx.json @@ -11,7 +11,11 @@ "output": "test/dfx_generated/canister1", "node_compatibility": true }, - "env": ["CANISTER1_PRINCIPAL", "CANISTER2_PRINCIPAL"] + "env": [ + "CANISTER1_PRINCIPAL", + "CANISTER2_PRINCIPAL", + "CANISTER3_PRINCIPAL" + ] }, "canister2": { "type": "custom", diff --git a/examples/composite_queries/test/tests.ts b/examples/composite_queries/test/tests.ts index de43de06cd..c050655e41 100644 --- a/examples/composite_queries/test/tests.ts +++ b/examples/composite_queries/test/tests.ts @@ -9,7 +9,7 @@ export function get_tests(canister1: ActorSubclass<_SERVICE>): Test[] { test: async () => { const result = await canister1.simpleCompositeQuery(); return { - Ok: 'Ok' in result && result.Ok === 'Hello from Canister 2' + Ok: result === 'Hello from Canister 2' }; } }, @@ -18,9 +18,7 @@ export function get_tests(canister1: ActorSubclass<_SERVICE>): Test[] { test: async () => { const result = await canister1.manualQuery(); return { - Ok: - 'Ok' in result && - result.Ok === 'Hello from Canister 2 manual query' + Ok: result === 'Hello from Canister 2 manual query' }; } }, @@ -29,9 +27,7 @@ export function get_tests(canister1: ActorSubclass<_SERVICE>): Test[] { test: async () => { const result = await canister1.totallyManualQuery(); return { - Ok: - 'Ok' in result && - result.Ok === 'Hello from Canister 2 manual query' + Ok: result === 'Hello from Canister 2 manual query' }; } }, @@ -40,35 +36,48 @@ export function get_tests(canister1: ActorSubclass<_SERVICE>): Test[] { test: async () => { const result = await canister1.deepQuery(); return { - Ok: 'Ok' in result && result.Ok === 'Hello from Canister 3' + Ok: result === 'Hello from Canister 3' }; } }, { name: 'update_query test', test: async () => { - const result = await canister1.updateQuery(); - return { - Ok: - 'Err' in result && - result.Err.includes( - `Rejection code 3, Canister ${getCanisterId( - 'canister2' - )} has no query method` - ) - }; + try { + await canister1.updateQuery(); + return { + Ok: false + }; + } catch (error: any) { + return { + Ok: error + .toString() + .includes( + `Rejection code 3, Canister ${getCanisterId( + 'canister2' + )} has no query method` + ) + }; + } } }, { name: 'simple_update test', test: async () => { - const result = await canister1.simpleUpdate(); - return { - Ok: - 'Err' in result && - result.Err === - 'Rejection code 5, IC0527: Composite query cannot be called in replicated mode' - }; + try { + await canister1.simpleUpdate(); + return { + Ok: false + }; + } catch (error: any) { + return { + Ok: error + .toString() + .includes( + 'Rejection code 5, IC0527: Composite query cannot be called in replicated mode' + ) + }; + } } }, { @@ -77,7 +86,7 @@ export function get_tests(canister1: ActorSubclass<_SERVICE>): Test[] { const result = await canister1.incCanister1(); return { - Ok: 'Ok' in result && result.Ok === 3n + Ok: result === 3n }; } }, @@ -86,7 +95,7 @@ export function get_tests(canister1: ActorSubclass<_SERVICE>): Test[] { test: async () => { const result = await canister1.incCanister2(); return { - Ok: 'Ok' in result && result.Ok === 3n + Ok: result === 3n }; } }