Skip to content

Commit

Permalink
add init test for recursive canisters
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 30, 2023
1 parent 8534ee2 commit 6f5cd4d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
12 changes: 6 additions & 6 deletions examples/recursion/src/recursion/index.did
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ type rec_337 = record {myVar:variant {num:int8; varRec:rec_337}};
type rec_289 = record {myVecRecords:vec rec_289};
type rec_292 = record {myVecRecords:vec rec_292};
type rec_333 = record {myVecRecords:vec rec_333};
type rec_385 = service {myQuery: (rec_385) -> (rec_385) query;};
type rec_388 = service {myQuery: (rec_388) -> (rec_388) query;};
type rec_397 = service {myQuery: (rec_397) -> (rec_397) query;};
type rec_400 = service {myQuery: (rec_400) -> (rec_400) query;};
type rec_393 = service {myQuery: (rec_393) -> (rec_393) query;};
type rec_385 = service {getMessage: () -> (text) query; myQuery: (rec_385) -> (rec_385) query;};
type rec_388 = service {getMessage: () -> (text) query; myQuery: (rec_388) -> (rec_388) query;};
type rec_397 = service {getMessage: () -> (text) query; myQuery: (rec_397) -> (rec_397) query;};
type rec_400 = service {getMessage: () -> (text) query; myQuery: (rec_400) -> (rec_400) query;};
type rec_393 = service {getMessage: () -> (text) query; myQuery: (rec_393) -> (rec_393) query;};
type rec_377 = service {myQuery: (rec_377) -> (rec_377) query;};
type rec_380 = service {myQuery: (rec_380) -> (rec_380) query;};
type rec_313 = record {opt rec_313; opt rec_313};
Expand All @@ -39,7 +39,7 @@ service: () -> {
testRecRecordWithVec: (rec_289) -> (rec_292) query;
testRecRecordWithVecReturn: () -> (rec_333) query;
testRecService: (rec_385) -> (rec_388) query;
testRecServiceCall: (rec_397) -> (rec_400) ;
testRecServiceCall: (rec_397) -> (rec_400);
testRecServiceReturn: () -> (rec_393) query;
testRecServiceSimple: (rec_377) -> (rec_380) query;
testRecTupleWithOpt: (rec_313) -> (rec_316) query;
Expand Down
9 changes: 5 additions & 4 deletions examples/recursion/src/recursive_canister/index.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type rec_20 = service {myQuery: (rec_20) -> (rec_20) query;};
type rec_23 = service {myQuery: (rec_23) -> (rec_23) query;};
service: () -> {
myQuery: (rec_20) -> (rec_23) query;
type rec_18 = service {getMessage: () -> (text) query; myQuery: (rec_18) -> (rec_18) query;};
type rec_21 = service {getMessage: () -> (text) query; myQuery: (rec_21) -> (rec_21) query;};
service: (text) -> {
getMessage: () -> (text) query;
myQuery: (rec_18) -> (rec_21) query;
}
10 changes: 8 additions & 2 deletions examples/recursion/src/recursive_canister/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { query, Canister, Recursive } from 'azle';
import { query, Canister, Recursive, text, init } from 'azle';

let myMessage = '';

const MyCanister = Recursive(() =>
Canister({
myQuery: query([MyCanister], MyCanister, (param) => param)
init: init([text], (message) => {
myMessage = message;
}),
myQuery: query([MyCanister], MyCanister, (param) => param),
getMessage: query([], text, () => myMessage)
})
);

Expand Down
2 changes: 1 addition & 1 deletion examples/recursion/test/pretest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function pretest() {
stdio: 'inherit'
});

execSync(`dfx deploy recursive_canister`, {
execSync(`dfx deploy recursive_canister --argument '("hello")'`, {
stdio: 'inherit'
});

Expand Down
17 changes: 15 additions & 2 deletions examples/recursion/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { getCanisterId, runTests } from 'azle/test';
import { createActor } from './dfx_generated/recursion';
import { getTests } from './tests';
import { createActor as createRecursiveActor } from './dfx_generated/recursive_canister';
import { getRecursiveCanisterTests, getTests } from './tests';

const recursionCanister = createActor(getCanisterId('recursion'), {
agentOptions: {
host: 'http://127.0.0.1:8000'
}
});

runTests(getTests(recursionCanister));
const recursiveCanister = createRecursiveActor(
getCanisterId('recursive_canister'),
{
agentOptions: {
host: 'http://127.0.0.1:8000'
}
}
);

runTests([
...getTests(recursionCanister),
...getRecursiveCanisterTests(recursiveCanister)
]);
17 changes: 17 additions & 0 deletions examples/recursion/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@ import {
rec_313,
rec_321
} from './dfx_generated/recursion/recursion.did';
import { _SERVICE as _REC_SERVICE } from './dfx_generated/recursive_canister/recursive_canister.did';
import { ActorSubclass } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';
import { execSync } from 'child_process';

// TODO these tests should be rewritten to use @dfinity/agent once this issue is resolved: https://github.com/dfinity/agent-js/issues/702
// TODO this issue also needs to be resolved: https://forum.dfinity.org/t/services-wont-deserialize-properly-if-functions-arent-in-alphabetical-order/20885
export function getRecursiveCanisterTests(
recursive_canister: ActorSubclass<_REC_SERVICE>
): Test[] {
return [
{
name: 'test recursive canister init method',
test: async () => {
const result = await recursive_canister.getMessage();

return {
Ok: result === 'hello'
};
}
}
];
}
export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] {
return [
{
Expand Down

0 comments on commit 6f5cd4d

Please sign in to comment.