Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const assert = require('assert');
  2. const ganache = require('ganache-cli');
  3. const Web3 = require('web3');
  4. const AnchorHive = artifacts.require('AnchorHive');
  5. const web3 = new Web3(ganache.provider());
  6.  
  7. contract('AnchorHive', function(accounts) {
  8.     let owner;
  9.     let instance;
  10.  
  11.     beforeEach('setup contract for each test', async function () {
  12.         instance = await AnchorHive.new();
  13.         owner = await instance.owner.call();
  14.     });
  15.  
  16.     it('should check ownership change', async () => {
  17.         let newOwner = accounts[2];
  18.         assert.equal(await instance.owner.call(), owner, "Initial owner must be " + owner);
  19.         instance.setOwner(newOwner, {from: owner});
  20.         assert.equal(await instance.owner.call(), newOwner, "New owner must be " + newOwner);
  21.     });
  22.  
  23.     it('should check send anchor', async () => {
  24.         let blockNumber = 1, blockHash = "1";
  25.         instance.addAnchor(blockNumber, blockHash, {from: owner});
  26.         let latestBlockNumber, latestBlockHash, _;
  27.         [latestBlockNumber, latestBlockHash, _] = await instance.getLatestAnchor.call();
  28.         assert.equal(latestBlockNumber, blockNumber, 'Latest block number must be' + blockNumber);
  29.         assert.equal(latestBlockHash, blockHash, 'Latest block hash must be ' + blockHash);
  30.     });
  31.    
  32.     it('should check send negative block number anchor', async () => {
  33.         let expectedBlockNumber = 0;
  34.         let blockNumber = -1, blockHash = "";
  35.         instance.addAnchor(blockNumber, blockHash, {from: owner});
  36.         let latestBlockNumber, latestBlockHash, _;
  37.         [latestBlockNumber, latestBlockHash, _] = await instance.getLatestAnchor.call();
  38.         assert.equal(latestBlockNumber, expectedBlockNumber,
  39.             'Couldn\'t add anchor with negative block number');
  40.     });
  41.    
  42.     it('should check send lesser block number anchor', async () => {
  43.         let expectedBlockNumber = 2;
  44.         let blockNumber = 2, blockHash = "1";
  45.         instance.addAnchor(blockNumber, blockHash, {from: owner});
  46.         blockNumber = 1;
  47.         instance.addAnchor(blockNumber, blockHash, {from: owner});
  48.         let latestBlockNumber, latestBlockHash, _;
  49.         [latestBlockNumber, latestBlockHash, _] = await instance.getLatestAnchor.call();
  50.         assert.equal(latestBlockNumber, expectedBlockNumber,
  51.             'Couldn\'t add anchor with lesser block number');
  52.     });
  53.    
  54.     it('should check get by block number', async () => {
  55.         let blockNumber = 2, blockHash = "2";
  56.         instance.addAnchor(blockNumber, blockHash, {from: owner});
  57.         let latestBlockNumber, latestBlockHash, _;
  58.         [latestBlockNumber, latestBlockHash, _] = await instance.getAnchor.call(blockNumber);
  59.         assert.equal(latestBlockNumber, blockNumber, 'Block number should be ' + blockNumber);
  60.         assert.equal(latestBlockHash, blockHash, 'Block hash should be ' + blockHash);
  61.     });
  62.    
  63.     it('should check send from non owner account', async () => {
  64.         let blockNumber = 1, blockHash = "2";
  65.         try {
  66.             instance.addAnchor(blockNumber, blockHash, {from: accounts[8]});
  67.             assert.fail();
  68.         } catch (error) {
  69.             assert.equal(error['code'], 'ERR_ASSERTION');
  70.         }
  71.     });
  72. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement