Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Added PostUpgradeArb #1512

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ jobs:
"property_tests/tests/nat64",
"property_tests/tests/null",
"property_tests/tests/opt",
"property_tests/tests/post_upgrade_method",
"property_tests/tests/principal",
"property_tests/tests/query_methods",
"property_tests/tests/record",
Expand Down
49 changes: 29 additions & 20 deletions property_tests/arbitraries/canister_arb.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import fc from 'fast-check';
import { QueryMethod } from './canister_methods/query_method_arb';
import { Test } from '../../test';
import { UpdateMethod } from './canister_methods/update_method_arb';
import { InitMethod } from './canister_methods/init_method_arb';
import { CorrespondingJSType } from './candid/corresponding_js_type';
import { InitMethod } from './canister_methods/init_method_arb';
import { PostUpgradeMethod } from './canister_methods/post_upgrade_arb';
import { UpdateMethod } from './canister_methods/update_method_arb';

export type Canister = {
initArgs: string[] | undefined;
deployArgs: string[] | undefined;
sourceCode: string;
tests: Test[][];
};

export type CanisterMethod<
ParamAgentArgumentValue extends CorrespondingJSType,
ParamAgentResponseValue
> =
| QueryMethod
| UpdateMethod
| InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>
| PostUpgradeMethod<ParamAgentArgumentValue, ParamAgentResponseValue>;

export type CanisterConfig<
ParamAgentArgumentValue extends CorrespondingJSType = undefined,
ParamAgentResponseValue = undefined
> = {
globalDeclarations?: string[];
initMethod?: InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>;
postUpgradeMethod?: PostUpgradeMethod<
ParamAgentArgumentValue,
ParamAgentResponseValue
>;
queryMethods?: QueryMethod[];
updateMethods?: UpdateMethod[];
};
Expand All @@ -31,17 +45,17 @@ export function CanisterArb<
>
) {
return configArb.map((config): Canister => {
const canisterMethods: (
| QueryMethod
| UpdateMethod
| InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>
)[] = [
const canisterMethods: CanisterMethod<
ParamAgentArgumentValue,
ParamAgentResponseValue
>[] = [
...(config.initMethod ? [config.initMethod] : []),
...(config.postUpgradeMethod ? [config.postUpgradeMethod] : []),
...(config.queryMethods ?? []),
...(config.updateMethods ?? [])
];

const initArgs = config.initMethod?.params.map(
const deployArgs = config.initMethod?.params.map(
({
el: {
src: { candidTypeAnnotation },
Expand Down Expand Up @@ -87,7 +101,7 @@ export function CanisterArb<
);

return {
initArgs,
deployArgs,
sourceCode,
tests
};
Expand All @@ -99,14 +113,10 @@ function generateSourceCode(
canisterMethods: (UpdateMethod | QueryMethod)[]
) {
const imports = [
...new Set(
canisterMethods.reduce(
(acc, method) => {
return [...acc, ...method.imports];
},
['Canister', 'query', 'update']
)
)
...new Set([
'Canister',
...canisterMethods.flatMap((method) => [...method.imports])
])
]
.sort((a, b) => a.localeCompare(b, 'en', { sensitivity: 'base' }))
.join();
Expand All @@ -116,8 +126,7 @@ function generateSourceCode(
);

const declarations = [
...globalDeclarations,
...declarationsFromCanisterMethods
...new Set([...globalDeclarations, ...declarationsFromCanisterMethods])
].join('\n');

const sourceCodes = canisterMethods.map((method) => method.sourceCode);
Expand Down
141 changes: 141 additions & 0 deletions property_tests/arbitraries/canister_methods/post_upgrade_arb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import fc from 'fast-check';
bdemann marked this conversation as resolved.
Show resolved Hide resolved

import { CandidValueAndMeta } from '../candid/candid_value_and_meta_arb';
import { CorrespondingJSType } from '../candid/corresponding_js_type';
import { UniqueIdentifierArb } from '../unique_identifier_arb';
import { Named } from '../..';
import {
BodyGenerator,
TestsGenerator,
CallbackLocation,
isDefined,
generateCallback
} from '.';
import { Test } from '../../../test';
import { VoidArb } from '../candid/primitive/void';

export type PostUpgradeMethod<
ParamAgentArgumentValue extends CorrespondingJSType,
ParamAgentResponseValue
> = {
imports: Set<string>;
globalDeclarations: string[];
params: Named<
CandidValueAndMeta<ParamAgentArgumentValue, ParamAgentResponseValue>
>[];
sourceCode: string;
tests: Test[][];
};

export function PostUpgradeMethodArb<
ParamAgentArgumentValue extends CorrespondingJSType,
ParamAgentResponseValue
>(
paramTypeArrayArb: fc.Arbitrary<
CandidValueAndMeta<ParamAgentArgumentValue, ParamAgentResponseValue>[]
>,
constraints: {
generateBody: BodyGenerator<
ParamAgentArgumentValue,
ParamAgentResponseValue
>;
generateTests: TestsGenerator<
ParamAgentArgumentValue,
ParamAgentResponseValue
>;
callbackLocation?: CallbackLocation;
}
) {
return fc
.tuple(
UniqueIdentifierArb('canisterMethod'),
bdemann marked this conversation as resolved.
Show resolved Hide resolved
paramTypeArrayArb,
VoidArb(),
fc.constantFrom(
'INLINE',
'STANDALONE'
) as fc.Arbitrary<CallbackLocation>,
UniqueIdentifierArb('typeDeclaration')
// TODO: This unique id would be better named globalScope or something
// But needs to match the same scope as typeDeclarations so I'm using
// that for now.
)
.map(
([
functionName,
paramTypes,
returnType,
defaultCallbackLocation,
callbackName
]) => {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Add a return type to this map callback of type PostUpgradeMethod<Something, Something>

const callbackLocation =
constraints.callbackLocation ?? defaultCallbackLocation;

const imports = new Set([
'postUpgrade',
...paramTypes.flatMap((param) => [...param.src.imports])
]);

const namedParams = paramTypes.map(
<T>(param: T, index: number): Named<T> => ({
name: `param${index}`,
el: param
})
);

const callback = generateCallback(
namedParams,
returnType,
constraints.generateBody,
callbackLocation,
callbackName
);

const variableAliasDeclarations = paramTypes
.flatMap((param) => param.src.variableAliasDeclarations)
.filter(isDefined);

const globalDeclarations =
callbackLocation === 'STANDALONE'
? [...variableAliasDeclarations, callback]
: variableAliasDeclarations;

const sourceCode = generateSourceCode(
functionName,
paramTypes,
callbackLocation === 'STANDALONE' ? callbackName : callback
);

const tests = constraints.generateTests(
functionName,
namedParams,
returnType
);

return {
imports,
globalDeclarations,
params: namedParams,
sourceCode,
tests
};
}
);
}

function generateSourceCode<
ParamType extends CorrespondingJSType,
ParamAgentType
>(
functionName: string,
paramTypes: CandidValueAndMeta<ParamType, ParamAgentType>[],
callback: string
): string {
const paramCandidTypeObjects = paramTypes
.map((param) => param.src.candidTypeObject)
.join(', ');

return `${functionName}: postUpgrade([${paramCandidTypeObjects}], ${callback})`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function QueryMethodArb<
constraints.callbackLocation ?? defaultCallbackLocation;

const imports = new Set([
'query',
...paramTypes.flatMap((param) => [...param.src.imports]),
...returnType.src.imports
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function UpdateMethodArb<
constraints.callbackLocation ?? defaultCallbackLocation;

const imports = new Set([
'update',
...paramTypes.flatMap((param) => [...param.src.imports]),
...returnType.src.imports
]);
Expand Down
4 changes: 2 additions & 2 deletions property_tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function runPropTests(canisterArb: fc.Arbitrary<Canister>) {

for (let i = 0; i < canister.tests.length; i++) {
const argumentsString =
canister.initArgs && canister.initArgs.length > 0
? `--argument '(${canister.initArgs.join(', ')})'`
canister.deployArgs && canister.deployArgs.length > 0
? `--argument '(${canister.deployArgs.join(', ')})'`
: '';

execSync(`dfx deploy canister ${argumentsString}`, {
Expand Down
2 changes: 1 addition & 1 deletion property_tests/tests/init_method/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function generateGetInitValuesCanisterMethod(
globalInitVariableNames: string[]
): QueryMethod {
return {
imports: new Set(['Tuple', 'bool']),
imports: new Set(['bool', 'query', 'Tuple']),
globalDeclarations: [],
sourceCode: /*TS*/ `getInitValues: query(
[],
Expand Down
16 changes: 16 additions & 0 deletions property_tests/tests/post_upgrade_method/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"canisters": {
"canister": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle canister",
"wasm": ".azle/canister/canister.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
}
}
}
Loading
Loading