Skip to content

Commit e4d191d

Browse files
committed
Add code coverage for unit test
Signed-off-by: Atsushi Neki <[email protected]>
1 parent affddef commit e4d191d

File tree

8 files changed

+225
-50
lines changed

8 files changed

+225
-50
lines changed

.mocharc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"watch-extensions": "ts",
44
"recursive": true,
55
"spec": ["./app/test/**/*.test.ts"],
6-
"timeout": 5000,
6+
"timeout": 10000,
77
"extension": ["ts"]
88
}

.nycrc.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@
66
"sourceMap": true,
77
"instrument": true,
88
"temp-dir": "app/test/.nyc_output",
9-
"report-dir": "app/test/coverage"
9+
"report-dir": "app/test/coverage",
10+
"all": true,
11+
"include": ["app"],
12+
"exclude": ["**/e2e-test", "app/test"],
13+
"check-coverage": false,
14+
"branches": 80,
15+
"lines": 80,
16+
"functions": 80,
17+
"statements": 80
1018
}

app/platform/fabric/gateway/FabricGateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export class FabricGateway {
426426
return targets;
427427
}
428428

429-
async sendRecvDiscoveryResult() {
429+
async sendDiscoveryRequest() {
430430
try {
431431
await this.ds.send({
432432
asLocalhost: this.asLocalhost,
@@ -456,7 +456,7 @@ export class FabricGateway {
456456
}
457457

458458
if (this.ds && this.dsTargets.length) {
459-
const result = await this.sendRecvDiscoveryResult();
459+
const result = await this.sendDiscoveryRequest();
460460
return result;
461461
}
462462

app/test/FabricGateway.test.ts

Lines changed: 194 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,212 @@
22
*SPDX-License-Identifier: Apache-2.0
33
*/
44

5-
/* tslint:disable:no-unused-expression */
6-
import { expect, use } from 'chai';
7-
import chaiAsPromised from 'chai-as-promised';
8-
import sinonChai from 'sinon-chai';
5+
import { expect } from './expect';
96
import { FabricConfig } from '../platform/fabric/FabricConfig';
107

11-
use(chaiAsPromised);
12-
use(sinonChai);
13-
14-
// export const expect = chai.expect
158
import proxyquire from 'proxyquire';
169
import sinon from 'sinon';
17-
import { assert } from 'console';
18-
// import { expect } from './expect'
10+
import { FabricGateway } from '../platform/fabric/gateway/FabricGateway';
1911

20-
describe('setupDiscoveryRequest', () => {
21-
it('should return without error', async () => {
22-
const stubSign = sinon.stub();
23-
const { FabricGateway } = proxyquire
24-
.noCallThru()
25-
.load('../platform/fabric/gateway/FabricGateway', {
26-
'fabric-common': {
27-
DiscoveryService: function() {
12+
// DiscoveryService (this.ds)
13+
const stubSign = sinon.stub();
14+
const stubGetDiscoveryResults = sinon.stub();
15+
const stubClose = sinon.stub();
16+
17+
// Discoverer
18+
const stubConnect = sinon.stub();
19+
20+
// logger
21+
const stubError = sinon.stub();
22+
const stubInfo = sinon.stub();
23+
24+
// Client
25+
const stubSetTlsClientCertAndKey = sinon.stub();
26+
const stubNewEndpoint = sinon.stub();
27+
28+
function getFabricGatewayInstance() {
29+
const { FabricGateway } = proxyquire
30+
.noCallThru()
31+
.load('../platform/fabric/gateway/FabricGateway', {
32+
'fabric-common': {
33+
DiscoveryService: function() {
34+
return {
35+
build: sinon.stub(),
36+
sign: stubSign,
37+
send: sinon.stub(),
38+
getDiscoveryResults: stubGetDiscoveryResults,
39+
close: stubClose
40+
};
41+
},
42+
Client: function() {
43+
return {
44+
setTlsClientCertAndKey: stubSetTlsClientCertAndKey,
45+
newEndpoint: stubNewEndpoint
46+
};
47+
},
48+
Discoverer: function() {
49+
return {
50+
connect: stubConnect
51+
};
52+
}
53+
},
54+
'../../../common/helper': {
55+
helper: {
56+
getLogger: function() {
2857
return {
29-
build: sinon.stub(),
30-
sign: stubSign
58+
error: stubError,
59+
info: stubInfo
3160
};
3261
}
33-
},
34-
'../../../common/helper': {
35-
helper: {
36-
getLogger: function() {}
37-
}
3862
}
39-
});
40-
41-
const config = new FabricConfig();
42-
config.initialize('first-network', {
43-
name: 'My first network',
44-
profile: './connection-profile/first-network.json'
63+
}
4564
});
65+
const config = new FabricConfig();
66+
config.initialize('first-network', {
67+
name: 'My first network',
68+
profile: './connection-profile/first-network.json'
69+
});
70+
sinon.stub(config, 'getPeerTlsCACertsPem');
71+
72+
const gw = new FabricGateway(config);
73+
gw.clientTlsIdentity = {
74+
credentials: {
75+
certificate: 'abc',
76+
privateKey: 'def'
77+
},
78+
mspId: 'org1',
79+
type: 'X.509'
80+
};
81+
82+
const stubGetNetwork = sinon.stub(gw.gateway, 'getNetwork');
83+
const stubGetChannel = sinon.stub();
84+
stubGetChannel.returns({});
85+
stubGetNetwork.returns(Promise.resolve({ getChannel: stubGetChannel }));
86+
87+
return gw;
88+
}
4689

47-
const gw = new FabricGateway(config);
48-
const stubGetNetwork = sinon.stub(gw.gateway, 'getNetwork');
49-
const stubGetChannel = sinon.stub();
50-
stubGetChannel.returns({});
51-
stubGetNetwork.returns(Promise.resolve({ getChannel: stubGetChannel }));
90+
function resetAllStubs() {
91+
// DiscoveryService (this.ds)
92+
stubSign.reset();
93+
stubGetDiscoveryResults.reset();
94+
stubClose.reset();
95+
96+
// Discoverer
97+
stubConnect.reset();
98+
99+
// logger
100+
stubError.reset();
101+
stubInfo.reset();
102+
103+
// Client
104+
stubSetTlsClientCertAndKey.reset();
105+
stubNewEndpoint.reset();
106+
}
107+
108+
describe('setupDiscoveryRequest', () => {
109+
let gw: FabricGateway;
110+
111+
before(() => {
112+
gw = getFabricGatewayInstance();
113+
});
114+
115+
beforeEach(() => {
116+
resetAllStubs();
117+
});
118+
119+
it('should return without error', async () => {
52120
await gw.setupDiscoveryRequest('testChannel');
53121
expect(stubSign.calledOnce).to.be.equal(true);
54122
});
123+
124+
it('should throw error if fail to sign', async () => {
125+
stubSign.throws();
126+
await gw.setupDiscoveryRequest('testChannel');
127+
expect(stubError.calledOnce).to.be.equal(true);
128+
});
129+
});
130+
131+
describe('getDiscoveryServiceTarget', () => {
132+
let gw: FabricGateway;
133+
134+
before(() => {
135+
gw = getFabricGatewayInstance();
136+
});
137+
138+
beforeEach(() => {
139+
resetAllStubs();
140+
});
141+
142+
it('should return without error', async () => {
143+
const targetArray = await gw.getDiscoveryServiceTarget();
144+
expect(stubSetTlsClientCertAndKey.calledOnceWith('abc', 'def')).to.be.equal(
145+
true
146+
);
147+
expect(stubNewEndpoint.calledOnce).to.be.equal(true);
148+
expect(stubConnect.calledOnce).to.be.equal(true);
149+
expect(targetArray.length).to.be.equal(1);
150+
});
151+
152+
it('should return without error when not assigned client TLS config', async () => {
153+
gw.clientTlsIdentity = undefined;
154+
const targetArray = await gw.getDiscoveryServiceTarget();
155+
expect(stubSetTlsClientCertAndKey.calledOnceWith()).to.be.equal(true);
156+
expect(stubNewEndpoint.calledOnce).to.be.equal(true);
157+
expect(stubConnect.calledOnce).to.be.equal(true);
158+
expect(targetArray.length).to.be.equal(1);
159+
});
160+
});
161+
162+
describe('sendDiscoveryRequest', () => {
163+
let gw: FabricGateway;
164+
165+
before(async () => {
166+
gw = getFabricGatewayInstance();
167+
await gw.setupDiscoveryRequest('testChannel');
168+
});
169+
170+
beforeEach(() => {
171+
resetAllStubs();
172+
});
173+
174+
it('should return without error', async () => {
175+
stubGetDiscoveryResults.returns(Promise.resolve());
176+
await gw.sendDiscoveryRequest();
177+
expect(stubError.called).be.equal(false);
178+
});
179+
180+
it('should throw error when failed to call getDiscoveryResults()', async () => {
181+
stubGetDiscoveryResults.throws();
182+
await gw.sendDiscoveryRequest();
183+
expect(stubError.called).be.equal(true);
184+
expect(stubClose.calledOnce).be.equal(true);
185+
});
186+
});
187+
188+
describe('getDiscoveryResult', () => {
189+
let gw: FabricGateway;
190+
let stubSetupDiscoveryRequest: sinon.SinonStub;
191+
192+
before(async () => {
193+
gw = getFabricGatewayInstance();
194+
await gw.setupDiscoveryRequest('testChannel');
195+
stubSetupDiscoveryRequest = sinon.stub(gw, 'setupDiscoveryRequest');
196+
});
197+
198+
beforeEach(() => {
199+
resetAllStubs();
200+
stubSetupDiscoveryRequest.reset();
201+
});
202+
203+
it('should return without error', async () => {
204+
await gw.getDiscoveryResult('testChannel');
205+
expect(stubSetupDiscoveryRequest.calledOnce).be.equal(false);
206+
});
207+
208+
it('should return without error if discover service has not been allocated yet', async () => {
209+
gw.ds = null;
210+
await gw.getDiscoveryResult('testChannel');
211+
expect(stubSetupDiscoveryRequest.calledOnce).be.equal(true);
212+
});
55213
});

app/test/expect.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
*SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
import chai from 'chai';
6+
import chaiAsPromised from 'chai-as-promised';
7+
import sinonChai from 'sinon-chai';
8+
9+
chai.use(chaiAsPromised);
10+
chai.use(sinonChai);
11+
12+
export const expect = chai.expect;

ci/azure-pipelines.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ jobs:
2121
- template: install_deps.yml
2222
- checkout: self
2323
- script: |
24-
npm config set prefix ~/npm && npm install -g mocha
25-
npm install chai && npm install
26-
./build_docker_image.sh -d
27-
docker-compose -f docker-compose-testdb.yaml up -d
28-
cd app/test && npm install
24+
npm config set prefix ~/npm
25+
npm install
2926
npm run test
30-
cd ../../client && npm install
27+
cd ./client && npm install
3128
echo "--------> npm tests with code coverage"
3229
npm run test:ci -- -u --coverage && npm run build
3330
displayName: Run Tests With Coverage Report

main.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ function print_help() {
1919
function do_install() {
2020
VERBOSE=${VERBOSE:+-ddd}
2121
npm install $VERBOSE
22-
(cd app/test && npm install $VERBOSE)
2322
(cd client && npm install $VERBOSE && npm run build)
2423
}
2524

2625
function do_test() {
27-
(cd app/test && npm run test)
26+
(npm run test)
2827
(cd client && npm run test:ci -- -u --coverage)
2928
}
3029

3130
function do_clean() {
3231
rm -rf node_modules
3332
rm -rf client/node_modules client/build client/coverage
34-
rm -rf app/test/node_modules
3533
}
3634

3735
# Get subcommand

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
"e2e-gui-test-v1-run": "cd client/e2e-test; ./gui-e2e-test-start.sh -1",
125125
"e2e-gui-test-v2-run": "cd client/e2e-test; ./gui-e2e-test-start.sh -2",
126126
"e2e-gui-test-v1": "run-s e2e-test-build-src e2e-gui-test-v1-run",
127-
"e2e-gui-test-v2": "run-s e2e-test-build-src e2e-gui-test-v2-run"
127+
"e2e-gui-test-v2": "run-s e2e-test-build-src e2e-gui-test-v2-run",
128+
"test": "nyc mocha",
129+
"test:watch": "mocha -w --reporter min"
128130
}
129131
}

0 commit comments

Comments
 (0)