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

Arg data raw prop test #2182

Merged
merged 20 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions tests/property/ic_api/arg_data_raw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
13 changes: 13 additions & 0 deletions tests/property/ic_api/arg_data_raw/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"canisters": {
"canister": {
"type": "azle",
"main": "src/index.ts",
"init_arg": "(\"\")",
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
}
}
}
12 changes: 12 additions & 0 deletions tests/property/ic_api/arg_data_raw/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
bail: true,
testTimeout: 100_000_000,
transform: {
'^.+\\.ts$': ['ts-jest', { isolatedModules: true }],
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
6,256 changes: 6,256 additions & 0 deletions tests/property/ic_api/arg_data_raw/package-lock.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions tests/property/ic_api/arg_data_raw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"scripts": {
"pretest": "tsx test/pretest.ts",
"test": "jest"
},
"dependencies": {
"azle": "0.24.1"
},
"devDependencies": {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
"@dfinity/agent": "^2.0.0",
"@dfinity/principal": "^2.0.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"tsx": "^4.15.7",
"typescript": "^5.2.2"
}
}
61 changes: 61 additions & 0 deletions tests/property/ic_api/arg_data_raw/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
argDataRaw,
candidEncode,
IDL,
init,
postUpgrade,
query,
update
} from 'azle';

export default class {
initArgDataRaw: Uint8Array | null = null;
postUpgradeArgDataRaw: Uint8Array | null = null;

@init([IDL.Text])
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
init(arg: string): void {
console.log('init', arg);
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
this.initArgDataRaw = argDataRaw();
}

@query([], IDL.Opt(IDL.Vec(IDL.Nat8)))
getInitArgDataRaw(): [Uint8Array] | [] {
if (this.initArgDataRaw === null) {
return [];
} else {
return [this.initArgDataRaw];
}
}

@postUpgrade([IDL.Text])
postUpgrade(arg: string): void {
console.log('postUpgrade', arg);
this.postUpgradeArgDataRaw = argDataRaw();
}

@query([], IDL.Opt(IDL.Vec(IDL.Nat8)))
getPostUpgradeArgDataRaw(): [Uint8Array] | [] {
if (this.postUpgradeArgDataRaw === null) {
return [];
} else {
return [this.postUpgradeArgDataRaw];
}
}

@query([IDL.Text], IDL.Vec(IDL.Nat8))
getQueryArgDataRaw(arg: string): Uint8Array {
console.log('query', arg);
return argDataRaw();
}

@update([IDL.Text], IDL.Vec(IDL.Nat8))
getUpdateArgDataRaw(arg: string): Uint8Array {
console.log('update', arg);
return argDataRaw();
}

@query([IDL.Text], IDL.Vec(IDL.Nat8))
candidEncode(arg: string): Uint8Array {
return candidEncode(arg);
}
}
17 changes: 17 additions & 0 deletions tests/property/ic_api/arg_data_raw/test/pretest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { execSync } from 'child_process';

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

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

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

pretest();
5 changes: 5 additions & 0 deletions tests/property/ic_api/arg_data_raw/test/test.ts
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runTests } from 'azle/test';

import { getTests } from './tests';

runTests(getTests());
109 changes: 109 additions & 0 deletions tests/property/ic_api/arg_data_raw/test/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
defaultPropTestParams,
expect,
getCanisterActor,
it,
Test
} from 'azle/test';
import { execSync } from 'child_process';
import fc from 'fast-check';

import { _SERVICE as Actor } from './dfx_generated/canister/canister.did';

export function getTests(): Test {
return () => {
it('gets the init arg data raw from the canister', async () => {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
await fc.assert(
fc.asyncProperty(
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
// TODO once we figure out how to escape single quotes in candid strings
// we can use fc.string() instead of fc.string().filter((s) => !s.includes("'"))
fc.string().filter((s) => !s.includes("'")),
fc.string().filter((s) => !s.includes("'")),
fc.string().filter((s) => !s.includes("'")),
fc.string().filter((s) => !s.includes("'")),
async (
initArgData,
postUpgradeArgData,
queryArgData,
updateArgData
) => {
execSync(
`dfx canister uninstall-code canister || true`,
{
stdio: 'inherit'
}
);

const initArgDataCandidString = `("${escapeArgData(
initArgData
)}")`;

execSync(
`dfx deploy canister --argument '${initArgDataCandidString}'`,
{
stdio: 'inherit'
}
);

const actor = await getCanisterActor<Actor>('canister');
const initArgDataRaw = await actor.getInitArgDataRaw();
const expectedInitArgDataRaw = await actor.candidEncode(
initArgDataCandidString
);
expect(initArgDataRaw).toEqual([
expectedInitArgDataRaw
]);

const queryArgDataCandidString = `("${escapeArgData(
queryArgData
)}")`;
const queryArgDataRaw =
await actor.getQueryArgDataRaw(queryArgData);
const expectedQueryArgDataRaw =
await actor.candidEncode(queryArgDataCandidString);
expect(queryArgDataRaw).toEqual(
expectedQueryArgDataRaw
);

const updateArgDataCandidString = `("${escapeArgData(
updateArgData
)}")`;
const updateArgDataRaw =
await actor.getUpdateArgDataRaw(updateArgData);
const expectedUpdateArgDataRaw =
await actor.candidEncode(updateArgDataCandidString);
expect(updateArgDataRaw).toEqual(
expectedUpdateArgDataRaw
);

const postUpgradeArgDataCandidString = `("${escapeArgData(
postUpgradeArgData
)}")`;

execSync(
`dfx deploy canister --argument '${postUpgradeArgDataCandidString}' --upgrade-unchanged`,
{
stdio: 'inherit'
}
);

const postUpgradeArgDataRaw =
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
await actor.getPostUpgradeArgDataRaw();
const expectedPostUpgradeArgDataRaw =
await actor.candidEncode(
postUpgradeArgDataCandidString
);
expect(postUpgradeArgDataRaw).toEqual([
expectedPostUpgradeArgDataRaw
]);
}
),
defaultPropTestParams
);
});
};
}

function escapeArgData(data: string): string {
return data.replace(/[\\"]/g, '\\$&');
}
10 changes: 10 additions & 0 deletions tests/property/ic_api/arg_data_raw/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"outDir": "HACK_BECAUSE_OF_ALLOW_JS"
}
}
Loading