Skip to content

Commit

Permalink
test-issues-242: add test de/serliaze err
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Aug 6, 2023
1 parent bd82bd8 commit 6d53d22
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 4 deletions.
50 changes: 50 additions & 0 deletions examples/__tests__/test-status-message-deserialize-err.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Worker } from "near-workspaces";
import test from "ava";

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the contract.
const statusMessage = await root.devDeploy("./build/status-message-deserialize-err.wasm");

// Create test users
const ali = await root.createSubAccount("ali");
const bob = await root.createSubAccount("bob");
const carl = await root.createSubAccount("carl");

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, statusMessage, ali, bob, carl };
});

test.after.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("Root gets null status", async (t) => {
const { statusMessage, root } = t.context.accounts;
const result = await statusMessage.view("get_status", {
account_id: root.accountId,
});
t.is(result, null);
});

test("Ali sets then gets status", async (t) => {
const { ali, statusMessage } = t.context.accounts;
await ali.call(statusMessage, "set_status", { message: "hello" });

let res = await ali.callRaw(statusMessage, "get_status", { account_id: ali.accountId });

t.assert(
res.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes(
"Smart contract panicked: deserialize err"
)
);
});

47 changes: 47 additions & 0 deletions examples/__tests__/test-status-message-serialize-err.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Worker } from "near-workspaces";
import test from "ava";

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the contract.
const statusMessage = await root.devDeploy("./build/status-message-serialize-err.wasm");

// Create test users
const ali = await root.createSubAccount("ali");
const bob = await root.createSubAccount("bob");
const carl = await root.createSubAccount("carl");

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, statusMessage, ali, bob, carl };
});

test.after.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("Root gets null status", async (t) => {
const { statusMessage, root } = t.context.accounts;
const result = await statusMessage.view("get_status", {
account_id: root.accountId,
});
t.is(result, null);
});

test("Ali sets status", async (t) => {
const { ali, statusMessage } = t.context.accounts;
let res = await ali.callRaw(statusMessage, "set_status", { message: "hello" });

t.assert(
res.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes(
"Smart contract panicked: serialize err"
)
);
});
6 changes: 5 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"build": "run-s build:*",
"build:status-message": "near-sdk-js build src/status-message.js build/status-message.wasm",
"build:status-message-borsh": "near-sdk-js build src/status-message-borsh.js build/status-message-borsh.wasm",
"build:status-message-serialize-err": "near-sdk-js build src/status-message-serialize-err.js build/status-message-serialize-err.wasm",
"build:status-message-deserialize-err": "near-sdk-js build src/status-message-deserialize-err.js build/status-message-deserialize-err.wasm",
"build:clean-state": "near-sdk-js build src/clean-state.js build/clean-state.wasm",
"build:counter": "near-sdk-js build src/counter.js build/counter.wasm",
"build:counter-lowlevel": "near-sdk-js build src/counter-lowlevel.js build/counter-lowlevel.wasm",
Expand Down Expand Up @@ -47,7 +49,9 @@
"test:programmatic-update": "ava __tests__/test-programmatic-update.ava.js",
"test:state-migration": "ava __tests__/test-state-migration.ava.js",
"test:nested-collections": "ava __tests__/test-nested-collections.ava.js",
"test:status-message-borsh": "ava __tests__/test-status-message-borsh.ava.js"
"test:status-message-borsh": "ava __tests__/test-status-message-borsh.ava.js",
"test:status-message-serialize-err": "ava __tests__/test-status-message-serialize-err.ava.js",
"test:status-message-deserialize-err": "ava __tests__/test-status-message-deserialize-err.ava.js"
},
"author": "Near Inc <hello@nearprotocol.com>",
"license": "Apache-2.0",
Expand Down
1 change: 1 addition & 0 deletions examples/src/status-message-borsh.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {NearBindgen, call, view, near} from "near-sdk-js";
import * as borsh from 'borsh';

const schema = {
struct: { records: {map: { key: 'string', value: 'string' }} }
Expand Down
25 changes: 25 additions & 0 deletions examples/src/status-message-deserialize-err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {NearBindgen, call, view, near} from "near-sdk-js";

@NearBindgen({
deserializer(_value) {
throw new Error('deserialize err')
},
})
export class StatusMessage {
constructor() {
this.records = new Map()
}

@call({})
set_status({ message }) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
}

@view({})
get_status({ account_id }) {
env.log(`get_status for account_id ${account_id}`)
return this.records.get(account_id) || null
}
}
25 changes: 25 additions & 0 deletions examples/src/status-message-serialize-err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {NearBindgen, call, view, near} from "near-sdk-js";

@NearBindgen({
serializer(_value) {
throw new Error('serialize err')
},
})
export class StatusMessage {
constructor() {
this.records = new Map()
}

@call({})
set_status({ message }) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
}

@view({})
get_status({ account_id }) {
env.log(`get_status for account_id ${account_id}`)
return this.records.get(account_id) || null
}
}
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d53d22

Please sign in to comment.