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

complete tests for UnorderedMap #63

Merged
merged 1 commit into from
May 27, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/collections/unordered-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class UnorderedMap {
if (this.len() == 1) {
// If there is only one element then swap remove simply removes it without
// swapping with the last element.
return near.jsvmStorageRemove(indexLookup)
near.jsvmStorageRemove(indexLookup)
} else {
// If there is more than one element then swap remove swaps it with the last
// element.
Expand Down
4 changes: 2 additions & 2 deletions src/collections/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ export class Vector {

clear() {
for (let i = 0; i < this.length; i++) {
let key = this.indexToKey(this.length)
let key = this.indexToKey(i)
near.jsvmStorageRemove(key)
}
this.len = 0
this.length = 0
}

toArray() {
Expand Down
58 changes: 0 additions & 58 deletions tests/__tests__/main.ava.js

This file was deleted.

181 changes: 181 additions & 0 deletions tests/__tests__/unordered-map.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { Worker } from 'near-workspaces';
import { readFile } from 'fs/promises'
import test from 'ava';

function encodeCall(contract, method, args) {
return Buffer.concat([Buffer.from(contract), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(JSON.stringify(args))])
}

test.beforeEach(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 jsvm contract.
const jsvm = await root.createAndDeploy(
root.getSubAccount('jsvm').accountId,
'../res/jsvm.wasm',
);

// Deploy test JS contract
const testContract = await root.createSubAccount('test-contract');
let contract_base64 = (await readFile('build/contract.base64')).toString();
await testContract.call(jsvm, 'deploy_js_contract', Buffer.from(contract_base64, 'base64'), { attachedDeposit: '400000000000000000000000' });
await testContract.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' });

// 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, jsvm, testContract, ali, bob, carl };
});

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

test('UnorderedMap is empty by default', async t => {
const { root, jsvm, testContract } = t.context.accounts;
const result = await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [root.accountId]));
t.is(result, 0);
});

test('UnorderedMap set() get()', async t => {
const { ali, jsvm, testContract } = t.context.accounts;
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])),
null
);

await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello', 'world']), { attachedDeposit: '100000000000000000000000' });

t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])),
'world'
);
});


test('UnorderedMap insert, update, len and iterate', async t => {
const { ali, jsvm, testContract } = t.context.accounts;

t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [])),
0
);
t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[]
);

await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello', 'world']), { attachedDeposit: '100000000000000000000000' });
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello1', 'world0']), { attachedDeposit: '100000000000000000000000' });
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [])),
2
);

// update a value, len shouldn't change
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello1', 'world1']), { attachedDeposit: '100000000000000000000000' });
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [])),
2
);
// update should have effect
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello1'])),
'world1'
);

await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello2', 'world2']), { attachedDeposit: '100000000000000000000000' });
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [])),
3
);

// Try to set a key with same value, len shouldn't change
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello2', 'world2']), { attachedDeposit: '100000000000000000000000' });
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [])),
3
);

t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[['hello', 'world'], ['hello1', 'world1'], ['hello2', 'world2']]
);
});

test('UnorderedMap extend, remove, clear', async t => {
const { ali, jsvm, testContract } = t.context.accounts;

await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'extend', [[['hello', 'world'], ['hello1', 'world1'], ['hello2', 'world2']]]), { attachedDeposit: '100000000000000000000000' });

t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[['hello', 'world'], ['hello1', 'world1'], ['hello2', 'world2']]
);

// remove non existing element should not error
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello3']), { attachedDeposit: '100000000000000000000000' });
t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[['hello', 'world'], ['hello1', 'world1'], ['hello2', 'world2']]
);

// remove not the last one should work
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello']), { attachedDeposit: '100000000000000000000000' });
t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[['hello2', 'world2'], ['hello1', 'world1']]
);

// remove the last one should work
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello1']), { attachedDeposit: '100000000000000000000000' });
t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[['hello2', 'world2']]
);

// remove when length is 1 should work
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [])),
1
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'isEmpty', [])),
false
);
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello2']), { attachedDeposit: '100000000000000000000000' });
t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[]
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'isEmpty', [])),
true
);

await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'extend', [[['hello', 'world'], ['hello1', 'world1'], ['hello2', 'world2']]]), { attachedDeposit: '100000000000000000000000' });
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'isEmpty', [])),
false
);
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'clear', []), { attachedDeposit: '100000000000000000000000' });

t.deepEqual(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'toArray', [])),
[]
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'isEmpty', [])),
true
);
})
4 changes: 2 additions & 2 deletions tests/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class UnorderedMapTestContract extends NearContract {

@view
toArray() {
this.unorderedMap.toArray();
return this.unorderedMap.toArray();
}

@view
@call
extend(kvs) {
this.unorderedMap.extend(kvs);
}
Expand Down
Loading