Skip to content

Commit

Permalink
add lookup map and lookup set tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ailisp committed May 31, 2022
1 parent 944fa2a commit 3df7ff7
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/collections/lookup-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export class LookupSet {
return null
}

extend(kvs) {
for(let kv of kvs) {
this.set(kv[0])
extend(keys) {
for(let key of keys) {
this.set(key)
}
}
}
117 changes: 117 additions & 0 deletions tests/__tests__/lookup-map.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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/lookup-map.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('LookupMap 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
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'containsKey', ['hello'])),
false
);

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'
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'containsKey', ['hello'])),
true
);
});


test('LookupMap update, remove', async t => {
const { ali, jsvm, testContract } = t.context.accounts;

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' });

// update a value, len shouldn't change
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello1', 'world1']), { attachedDeposit: '100000000000000000000000' });
// update should have effect
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello1'])),
'world1'
);
// not update key should not changed
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])),
'world'
);
// remove non existing element should not error
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello3']), { attachedDeposit: '100000000000000000000000' });
// remove existing key should work
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello1']));
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'containsKey', ['hello1'])),
false
);
// not removed key should not affected
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])),
'world'
);
});

test('LookupMap extend', 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.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])),
'world'
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello1'])),
'world1'
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello2'])),
'world2'
);
})
97 changes: 97 additions & 0 deletions tests/__tests__/lookup-set.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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/lookup-set.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('LookupSet set() contains()', async t => {
const { ali, jsvm, testContract } = t.context.accounts;
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])),
false
);

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

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


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

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

// remove non existing element should not error
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello3']), { attachedDeposit: '100000000000000000000000' });
// remove existing key should work
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello1']));
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello1'])),
false
);
// not removed key should not affected
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])),
true
);
});

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

await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'extend', [['hello', 'world', 'hello1']]), { attachedDeposit: '100000000000000000000000' });
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])),
true
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello1'])),
true
);
t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['world'])),
true
);
})
2 changes: 2 additions & 0 deletions tests/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

near-sdk build src/unordered-map.js build/unordered-map.base64
near-sdk build src/vector.js build/vector.base64
near-sdk build src/lookup-map.js build/lookup-map.base64
near-sdk build src/lookup-set.js build/lookup-set.base64
46 changes: 46 additions & 0 deletions tests/src/lookup-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
NearContract,
NearBindgen,
call,
view,
LookupMap
} from 'near-sdk-js'

@NearBindgen
class LookupMapTestContract extends NearContract {
constructor() {
super()
this.lookupMap = new LookupMap('a');
}

deserialize() {
super.deserialize();
this.lookupMap = Object.assign(new LookupMap, this.lookupMap);
}

@view
get(key) {
return this.lookupMap.get(key);
}

@view
containsKey(key) {
return this.lookupMap.containsKey(key);
}

@call
set(key, value) {
this.lookupMap.set(key, value);
}

@call
remove(key) {
this.lookupMap.remove(key);
}

@call
extend(kvs) {
this.lookupMap.extend(kvs);
}
}

41 changes: 41 additions & 0 deletions tests/src/lookup-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
NearContract,
NearBindgen,
call,
view,
LookupSet
} from 'near-sdk-js'

@NearBindgen
class LookupSetTestContract extends NearContract {
constructor() {
super()
this.lookupSet = new LookupSet('a');
}

deserialize() {
super.deserialize();
this.lookupSet = Object.assign(new LookupSet, this.lookupSet);
}

@view
contains(key) {
return this.lookupSet.contains(key);
}

@call
set(key) {
this.lookupSet.set(key);
}

@call
remove(key) {
this.lookupSet.remove(key);
}

@call
extend(keys) {
this.lookupSet.extend(keys);
}
}

0 comments on commit 3df7ff7

Please sign in to comment.