Skip to content

Commit

Permalink
finish recursive test example
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 30, 2023
1 parent c09d6b8 commit 3aee75c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 21 deletions.
3 changes: 2 additions & 1 deletion examples/recursion/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"declarations": {
"output": "test/dfx_generated/recursion",
"node_compatibility": true
}
},
"env": ["MY_CANISTER_PRINCIPAL"]
},
"recursive_canister": {
"type": "custom",
Expand Down
4 changes: 2 additions & 2 deletions examples/recursion/src/recursion/index.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type rec_397 = service {myQuery: (rec_397) -> (rec_397) query;};
type rec_400 = service {myQuery: (rec_400) -> (rec_400) query;};
type rec_365 = func (rec_365) -> (rec_365) query;
type rec_368 = func (rec_368) -> (rec_368) query;
type rec_373 = func (rec_373) -> (rec_373) query;
Expand All @@ -14,6 +12,8 @@ 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_377 = service {myQuery: (rec_377) -> (rec_377) query;};
type rec_380 = service {myQuery: (rec_380) -> (rec_380) query;};
Expand Down
8 changes: 5 additions & 3 deletions examples/recursion/src/recursion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ export default Canister({
testRecService: query([MyFullCanister], MyFullCanister, (param) => param),
testRecServiceReturn: query([], MyFullCanister, () => {
return MyFullCanister(
// Principal.fromText(process.env.MY_CANISTER_PRINCIPAL) ??
Principal.fromText('asrmz-lmaaa-aaaaa-qaaeq-cai') ??
ic.trap('process.env.MY_CANISTER_PRINCIPAL is undefined')
Principal.fromText(
process.env.MY_CANISTER_PRINCIPAL ??
// Principal.fromText('asrmz-lmaaa-aaaaa-qaaeq-cai') ??
ic.trap('process.env.MY_CANISTER_PRINCIPAL is undefined')
)
);
}),
testRecServiceCall: update(
Expand Down
12 changes: 4 additions & 8 deletions examples/recursion/src/recursive_canister/index.did
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
type rec_20 = service {
myQuery: (rec_20) -> (rec_20) query;
};
type rec_23 = service {
myQuery: (rec_23) -> (rec_23) query;
};
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;
}
myQuery: (rec_20) -> (rec_23) query;
}
22 changes: 20 additions & 2 deletions examples/recursion/test/pretest.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { execSync } from 'child_process';
import { getCanisterId } from 'azle/test';

async function pretest() {
await new Promise((resolve) => setTimeout(resolve, 5000));

execSync(`dfx canister uninstall-code recursion || true`, {
execSync(`dfx canister uninstall-code recursive_canister || true`, {
stdio: 'inherit'
});

execSync(`dfx deploy recursion`, {
execSync(`dfx deploy recursive_canister`, {
stdio: 'inherit'
});

execSync(`dfx generate recursive_canister`, {
stdio: 'inherit'
});

execSync(`dfx canister uninstall-code recursion || true`, {
stdio: 'inherit'
});

execSync(
`MY_CANISTER_PRINCIPAL=${getCanisterId(
'recursive_canister'
)} dfx deploy recursion`,
{
stdio: 'inherit'
}
);

execSync(`dfx generate recursion`, {
stdio: 'inherit'
});
Expand Down
74 changes: 69 additions & 5 deletions examples/recursion/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Test } from 'azle/test';
import { Test, getCanisterId } from 'azle/test';
import {
_SERVICE,
rec_10,
rec_8
rec_313,
rec_321
} from './dfx_generated/recursion/recursion.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 getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] {
return [
{
Expand Down Expand Up @@ -179,7 +182,7 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] {
{
name: 'recursive tuples with vec',
test: async () => {
const input: rec_10 = [[[[], [[[], []]]]], []];
const input: rec_321 = [[[[], [[[], []]]]], []];
const result =
await recursion_canister.testRecTupleWithVec(input);

Expand Down Expand Up @@ -211,7 +214,7 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] {
{
name: 'recursive tuples with opt',
test: async () => {
const input: rec_8 = [[[[], [[[], []]]]], []];
const input: rec_313 = [[[[], [[[], []]]]], []];
const result =
await recursion_canister.testRecTupleWithOpt(input);

Expand Down Expand Up @@ -309,6 +312,67 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] {
result[1].varTuple[1].varTuple[1].num === 10
};
}
},
{
name: 'test rec service simple',
test: async () => {
const principalId = getCanisterId('recursive_canister');
const result = execSync(
`dfx canister call recursion testRecServiceSimple '(service "${principalId}")'`
)
.toString()
.trim();

return {
Ok: result === `(service "${principalId}")`
};
}
},
{
name: 'test rec service',
test: async () => {
const principalId = getCanisterId('recursive_canister');
const result = execSync(
`dfx canister call recursion testRecService '(service "${principalId}")'`
)
.toString()
.trim();

return {
Ok: result === `(service "${principalId}")`
};
}
},
{
name: 'test rec service return',
test: async () => {
const principalId = getCanisterId('recursive_canister');
const result = execSync(
`dfx canister call recursion testRecServiceReturn`
)
.toString()
.trim();

return {
Ok: result === `(service "${principalId}")`
};
}
},
{
name: 'test rec service call',
skip: true, // TODO waiting for azle.encode and azle.decode to be implemented
test: async () => {
const principalId = getCanisterId('recursive_canister');
const result = execSync(
`dfx canister call recursion testRecServiceCall '(service "${principalId}")'`
)
.toString()
.trim();

return {
Ok: result === `(service "${principalId}")`
};
}
}
];
}
Expand Down

0 comments on commit 3aee75c

Please sign in to comment.