-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
property_tests/arbitraries/canister_methods/pre_upgrade_method_arb.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import fc from 'fast-check'; | ||
|
||
import { UniqueIdentifierArb } from '../unique_identifier_arb'; | ||
import { | ||
BodyGenerator, | ||
TestsGenerator, | ||
CallbackLocation, | ||
generateCallback, | ||
CallbackLocationArb | ||
} from '.'; | ||
import { Test } from '../../../test'; | ||
import { VoidArb } from '../candid/primitive/void'; | ||
|
||
export type PreUpgradeMethod = { | ||
imports: Set<string>; | ||
globalDeclarations: string[]; | ||
sourceCode: string; | ||
tests: Test[][]; | ||
}; | ||
|
||
export function PreUpgradeMethodArb(constraints: { | ||
generateBody: BodyGenerator; | ||
generateTests: TestsGenerator; | ||
callbackLocation?: CallbackLocation; | ||
}) { | ||
return fc | ||
.tuple( | ||
UniqueIdentifierArb('canisterMethod'), | ||
VoidArb(), | ||
CallbackLocationArb, | ||
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, | ||
returnType, | ||
defaultCallbackLocation, | ||
callbackName | ||
]): PreUpgradeMethod => { | ||
const callbackLocation = | ||
constraints.callbackLocation ?? defaultCallbackLocation; | ||
|
||
const imports = new Set(['preUpgrade']); | ||
|
||
const callback = generateCallback( | ||
[], | ||
returnType, | ||
constraints.generateBody, | ||
callbackLocation, | ||
callbackName | ||
); | ||
|
||
const globalDeclarations = | ||
callbackLocation === 'STANDALONE' ? [callback] : []; | ||
|
||
const sourceCode = `${functionName}: preUpgrade(${ | ||
callbackLocation === 'STANDALONE' ? callbackName : callback | ||
})`; | ||
|
||
const tests = constraints.generateTests( | ||
functionName, | ||
[], | ||
returnType | ||
); | ||
|
||
return { | ||
imports, | ||
globalDeclarations, | ||
sourceCode, | ||
tests | ||
}; | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
property_tests/tests/canister_methods/pre_upgrade/test/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import fc from 'fast-check'; | ||
|
||
import { deepEqual, getActor, runPropTests } from 'azle/property_tests'; | ||
import { CandidValueAndMetaArbWithoutFuncs as CandidValueAndMetaArb } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; | ||
import { CandidReturnTypeArb } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; | ||
import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; | ||
import { | ||
CanisterArb, | ||
CanisterConfig | ||
} from 'azle/property_tests/arbitraries/canister_arb'; | ||
import { UpdateMethodArb } from 'azle/property_tests/arbitraries/canister_methods/update_method_arb'; | ||
import { | ||
QueryMethod, | ||
QueryMethodArb | ||
} from 'azle/property_tests/arbitraries/canister_methods/query_method_arb'; | ||
import { PreUpgradeMethodArb } from 'azle/property_tests/arbitraries/canister_methods/pre_upgrade_method_arb'; | ||
|
||
const SimplePreUpgradeArb = PreUpgradeMethodArb({ | ||
generateBody: () => | ||
/*TS*/ `stable.insert(PRE_UPGRADE_HOOK_EXECUTED, true);`, | ||
generateTests: () => [] | ||
}); | ||
|
||
const HeterogeneousQueryMethodArb = QueryMethodArb( | ||
fc.array(CandidValueAndMetaArb()), | ||
CandidReturnTypeArb(), | ||
{ | ||
generateBody: (_, returnType) => | ||
`return ${returnType.src.valueLiteral}`, | ||
generateTests: () => [] | ||
} | ||
); | ||
|
||
const HeterogeneousUpdateMethodArb = UpdateMethodArb( | ||
fc.array(CandidValueAndMetaArb()), | ||
CandidReturnTypeArb(), | ||
{ | ||
generateBody: (_, returnType) => | ||
`return ${returnType.src.valueLiteral}`, | ||
generateTests: () => [] | ||
} | ||
); | ||
|
||
const small = { | ||
minLength: 0, | ||
maxLength: 20 | ||
}; | ||
|
||
const CanisterConfigArb = fc | ||
.tuple( | ||
SimplePreUpgradeArb, | ||
fc | ||
.array(HeterogeneousQueryMethodArb, small) | ||
.chain((queryMethods) => | ||
fc.constant([ | ||
generateGetPreUpgradeExecutedCanisterMethod(), | ||
...queryMethods | ||
]) | ||
), | ||
fc.array(HeterogeneousUpdateMethodArb, small) | ||
) | ||
|
||
.map( | ||
([preUpgradeMethod, queryMethods, updateMethods]): CanisterConfig< | ||
CorrespondingJSType, | ||
CorrespondingJSType | ||
> => { | ||
const globalDeclarations = [ | ||
/*TS*/ `const PRE_UPGRADE_HOOK_EXECUTED = 'PRE_UPGRADE_HOOK_EXECUTED';`, | ||
/*TS*/ `let stable = StableBTreeMap<text, bool>(0);` | ||
]; | ||
|
||
return { | ||
globalDeclarations, | ||
preUpgradeMethod, | ||
queryMethods, | ||
updateMethods | ||
}; | ||
} | ||
); | ||
|
||
runPropTests(CanisterArb(CanisterConfigArb)); | ||
|
||
function generateGetPreUpgradeExecutedCanisterMethod(): QueryMethod { | ||
return { | ||
imports: new Set(['bool', 'query', 'StableBTreeMap']), | ||
globalDeclarations: [], | ||
sourceCode: /*TS*/ `getPreUpgradeExecuted: query([], bool,() => { | ||
return stable.get(PRE_UPGRADE_HOOK_EXECUTED).Some === true | ||
})`, | ||
tests: [ | ||
[ | ||
{ | ||
name: `pre upgrade was not called after first deploy`, | ||
test: async () => { | ||
const actor = getActor(__dirname); | ||
const result = await actor.getPreUpgradeExecuted(); | ||
|
||
return { Ok: deepEqual(result, false) }; | ||
} | ||
} | ||
], | ||
[ | ||
{ | ||
name: `pre upgrade was called after second deploy`, | ||
test: async () => { | ||
const actor = getActor(__dirname); | ||
const result = await actor.getPreUpgradeExecuted(); | ||
|
||
return { Ok: deepEqual(result, false) }; | ||
} | ||
} | ||
] | ||
] | ||
}; | ||
} |