Skip to content

Commit

Permalink
Update test files for ethereum-connector.js
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Pandeey <[email protected]>
  • Loading branch information
Sweetdevil144 committed May 1, 2024
1 parent 60315f1 commit b6f32fb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 60 deletions.
6 changes: 3 additions & 3 deletions packages/caliper-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"pretest": "npm run licchk",
"licchk": "license-check-and-add",
"test": "npm run lint",
"test": "npm run lint && npm run nyc",
"lint": "npx eslint .",
"nyc": "nyc --reporter=text --reporter=clover mocha --recursive -t 10000"
},
Expand All @@ -28,9 +28,9 @@
},
"devDependencies": {
"eslint": "^5.16.0",
"license-check-and-add": "2.3.6",
"mocha": "3.4.2",
"nyc": "11.1.0",
"license-check-and-add": "2.3.6"
"nyc": "11.1.0"
},
"license-check-and-add-config": {
"folder": ".",
Expand Down
98 changes: 41 additions & 57 deletions packages/caliper-ethereum/test/ethereum-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,59 @@

'use strict';

const chai = require('chai');
const path = require('path');
const expect = require('chai').expect;
const sinon = require('sinon');
const expect = chai.expect;
const rewire = require('rewire');
const networkConfig = require('../../caliper-tests-integration/ethereum_tests/networkconfig.json');

const EthereumConnector = rewire('../lib/ethereum-connector');
const { ConfigUtil, CaliperUtils } = require('@hyperledger/caliper-core');
const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil;
const EthereumConnector = require('../lib/ethereum-connector');

describe('EthereumConnector', function() {
let sandbox = sinon.createSandbox();
let mockWeb3, mockEEAClient, configStub;
let ethereumConnectorStub;
let tempConfigFilePath;

beforeEach(() => {
mockWeb3 = {
eth: {
Contract: sinon.stub(),
accounts: {
wallet: {
add: sinon.stub()
},
create: sinon.stub()
},
personal: {
unlockAccount: sinon.stub().resolves(true)
}
},
utils: {}
};
mockEEAClient = function() {
return mockWeb3;
};
ethereumConnectorStub = sinon.stub(EthereumConnector, 'constructor').returns({});
tempConfigFilePath = path.resolve(__dirname, '../../caliper-tests-integration/ethereum_tests/networkconfig.json');
ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath);
});

EthereumConnector.__set__('Web3', function() {
return mockWeb3;
});
EthereumConnector.__set__('EEAClient', mockEEAClient);
afterEach(() => {
ethereumConnectorStub.restore();
});

// Stub configurations
configStub = sandbox.stub(ConfigUtil, 'get');
configStub.withArgs(ConfigUtil.keys.NetworkConfig).returns(networkConfig);
sandbox.stub(CaliperUtils, 'resolvePath').returnsArg(0);
sandbox.stub(JSON, 'parse').returns({
ethereum: {
url: 'ws://localhost:8546',
contractDeployerAddress: '0x1234567890',
transactionConfirmationBlocks: 2,
contracts: {
simple: {
path : 'caliper-tests-integration/ethereum_tests/src/simple/simple.sol', // TODO : change this to the correct path
gas: 3000000
}
}
}
describe('constructor', () => {
it('should create a new EthereumConnector instance', () => {
const ethereumConnector = new EthereumConnector(0, 'ethereum');
expect(ethereumConnector).to.be.instanceOf(EthereumConnector);
});
sandbox.stub(require, 'main').returns({
ethereum: {
url: 'ws://localhost:8546'
});

describe('installSmartContract', () => {
it('should throw an error when the specified contract path does not exist', async () => {
const ethereumConnector = new EthereumConnector(0, 'ethereum');
const contractDetails = {
path: './nonexistent/contract.sol'
};
try {
await ethereumConnector.installSmartContract(contractDetails);
} catch (err) {
expect(err.message).to.contain('Cannot find module');
}
});
});

afterEach(() => {
sandbox.restore();
});
describe('init', () => {
it('should throw an error when the specified contract path does not exist', async () => {
const ethereumConnector = new EthereumConnector(0, 'ethereum');
const contractDetails = {
path: './nonexistent/contract.sol'
};

it('should correctly initialize the EthereumConnector', async () => {
const connector = new EthereumConnector(0, 'ethereum');
expect(connector).to.be.instanceOf(EthereumConnector);
expect(mockWeb3.eth.Contract.called).to.be.false;
try {
await ethereumConnector.init(contractDetails);
} catch (err) {
expect(err.message).to.contain('connection not open');
}
});
});
});

0 comments on commit b6f32fb

Please sign in to comment.