Skip to content

Commit

Permalink
finish composite query example
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 15, 2023
1 parent 342ca32 commit 213aa57
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/composite_queries/canisters/canister1/index.did
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ service: () -> {
simpleQuery: () -> (text) query;
simpleUpdate: () -> (text);
incCounter: () -> (nat) query;
incCanister1: () -> (nat) query;
incCanister2: () -> (nat) query;
}
32 changes: 17 additions & 15 deletions examples/composite_queries/canisters/canister1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ??
Expand All @@ -31,7 +24,7 @@ class Canister1 extends Service {
}

// Manual composite query calling a manual query
@query([], text)
@query([], text, { manual: true })
async totallyManualQuery(): Promise<Manual<text>> {
ic.reply(await ic.call(this.canister2.manualQuery), text);
}
Expand Down Expand Up @@ -64,23 +57,32 @@ class Canister1 extends Service {
@query([], nat)
async incCounter(): Promise<nat> {
this.counter += 1n;

return this.counter;
}

// Composite query calling queries on the same canister. SHOULDN'T WORK
// @query([], nat)
// async incCanister1(): Promise<nat> {
// this.counter += 1n;
// Composite query calling queries on the same canister
@query([], nat)
async incCanister1(): Promise<nat> {
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<nat> {
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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/composite_queries/canisters/canister2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class extends Service {
return 'Hello from a Canister 2 update';
}

@query([], text)
@query([], text, { manual: true })
manualQuery(): Manual<text> {
ic.reply('Hello from Canister 2 manual query', text);
}
Expand Down
6 changes: 5 additions & 1 deletion examples/composite_queries/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 36 additions & 27 deletions examples/composite_queries/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
}
},
Expand All @@ -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'
};
}
},
Expand All @@ -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'
};
}
},
Expand All @@ -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'
)
};
}
}
},
{
Expand All @@ -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
};
}
},
Expand All @@ -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
};
}
}
Expand Down

0 comments on commit 213aa57

Please sign in to comment.