Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const assert = require('assert');
- const ganache = require('ganache-cli');
- const Web3 = require('web3');
- const AnchorHive = artifacts.require('AnchorHive');
- const web3 = new Web3(ganache.provider());
- contract('AnchorHive', function(accounts) {
- let owner;
- let instance;
- beforeEach('setup contract for each test', async function () {
- instance = await AnchorHive.new();
- owner = await instance.owner.call();
- });
- it('should check ownership change', async () => {
- let newOwner = accounts[2];
- assert.equal(await instance.owner.call(), owner, "Initial owner must be " + owner);
- instance.setOwner(newOwner, {from: owner});
- assert.equal(await instance.owner.call(), newOwner, "New owner must be " + newOwner);
- });
- it('should check send anchor', async () => {
- let blockNumber = 1, blockHash = "1";
- instance.addAnchor(blockNumber, blockHash, {from: owner});
- let latestBlockNumber, latestBlockHash, _;
- [latestBlockNumber, latestBlockHash, _] = await instance.getLatestAnchor.call();
- assert.equal(latestBlockNumber, blockNumber, 'Latest block number must be' + blockNumber);
- assert.equal(latestBlockHash, blockHash, 'Latest block hash must be ' + blockHash);
- });
- it('should check send negative block number anchor', async () => {
- let expectedBlockNumber = 0;
- let blockNumber = -1, blockHash = "";
- instance.addAnchor(blockNumber, blockHash, {from: owner});
- let latestBlockNumber, latestBlockHash, _;
- [latestBlockNumber, latestBlockHash, _] = await instance.getLatestAnchor.call();
- assert.equal(latestBlockNumber, expectedBlockNumber,
- 'Couldn\'t add anchor with negative block number');
- });
- it('should check send lesser block number anchor', async () => {
- let expectedBlockNumber = 2;
- let blockNumber = 2, blockHash = "1";
- instance.addAnchor(blockNumber, blockHash, {from: owner});
- blockNumber = 1;
- instance.addAnchor(blockNumber, blockHash, {from: owner});
- let latestBlockNumber, latestBlockHash, _;
- [latestBlockNumber, latestBlockHash, _] = await instance.getLatestAnchor.call();
- assert.equal(latestBlockNumber, expectedBlockNumber,
- 'Couldn\'t add anchor with lesser block number');
- });
- it('should check get by block number', async () => {
- let blockNumber = 2, blockHash = "2";
- instance.addAnchor(blockNumber, blockHash, {from: owner});
- let latestBlockNumber, latestBlockHash, _;
- [latestBlockNumber, latestBlockHash, _] = await instance.getAnchor.call(blockNumber);
- assert.equal(latestBlockNumber, blockNumber, 'Block number should be ' + blockNumber);
- assert.equal(latestBlockHash, blockHash, 'Block hash should be ' + blockHash);
- });
- it('should check send from non owner account', async () => {
- let blockNumber = 1, blockHash = "2";
- try {
- instance.addAnchor(blockNumber, blockHash, {from: accounts[8]});
- assert.fail();
- } catch (error) {
- assert.equal(error['code'], 'ERR_ASSERTION');
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement