Skip to content

Commit

Permalink
finish up certified data
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 18, 2024
1 parent 9013a60 commit 5ecdad7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/property/ic_api/certified_data/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"canister": {
"type": "azle",
"main": "src/index.ts",
"init_arg": "(\"\")",
"init_arg": "(false)",
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
Expand Down
3 changes: 1 addition & 2 deletions tests/property/ic_api/certified_data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from 'azle';

// TODO set can be called from reply and reject callbacks
// TODO set should trap if the data is greater than 32 bytes

let afterFirstPostUpgrade = false;

Expand Down Expand Up @@ -64,7 +63,7 @@ export default class {
}

@query([])
getDataCertificateInQuery(): void {
setDataCertificateInQuery(): void {
try {
setCertifiedData(new Uint8Array([3]));
} catch (error) {
Expand Down
60 changes: 60 additions & 0 deletions tests/property/ic_api/certified_data/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,66 @@ export function getTests(): Test {
defaultPropTestParams
);
});

it('always returns empty array when trying to get data certificate in update method', async () => {
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1, maxLength: 32 }),
async (arbitraryData) => {
const actor = await getCanisterActor<Actor>('canister');
uninstallCanister();
deployCanister();

await expect(
actor.getDataCertificateInUpdate()
).resolves.toEqual([]);

await actor.setData(arbitraryData);

await expect(
actor.getDataCertificateInUpdate()
).resolves.toEqual([]);
}
),
defaultPropTestParams
);
});

it('throws error when trying to set certified data in query method', async () => {
const actor = await getCanisterActor<Actor>('canister');
uninstallCanister();
deployCanister();

await expect(
actor.setDataCertificateInQuery()
).resolves.not.toThrow();
});

it('handles undefined data certificate', async () => {
const actor = await getCanisterActor<Actor>('canister');
uninstallCanister();
deployCanister();

// Check that getCertificate returns an empty array when no data is set
const emptyCertificate = await actor.getCertificate();
expect(emptyCertificate).toEqual([]);
});

it('throws error when trying to set certified data longer than 32 bytes', async () => {
const actor = await getCanisterActor<Actor>('canister');
uninstallCanister();
deployCanister();

// Generate data longer than 32 bytes
const longData = new Uint8Array(33).fill(1);

// Attempt to set the long data and expect it to throw
await expect(actor.setData(longData)).rejects.toThrow();

// Verify that the certificate is still empty
const certificate = await actor.getCertificate();
expect(certificate).toEqual([]);
});
};
}

Expand Down

0 comments on commit 5ecdad7

Please sign in to comment.