|
a |
|
b/contract/test/my-asset-contract.js |
|
|
1 |
/* |
|
|
2 |
* SPDX-License-Identifier: Apache-2.0 |
|
|
3 |
*/ |
|
|
4 |
|
|
|
5 |
'use strict'; |
|
|
6 |
|
|
|
7 |
const { ChaincodeStub, ClientIdentity } = require('fabric-shim'); |
|
|
8 |
const { MyAssetContract } = require('..'); |
|
|
9 |
const winston = require('winston'); |
|
|
10 |
|
|
|
11 |
const chai = require('chai'); |
|
|
12 |
const chaiAsPromised = require('chai-as-promised'); |
|
|
13 |
const sinon = require('sinon'); |
|
|
14 |
const sinonChai = require('sinon-chai'); |
|
|
15 |
|
|
|
16 |
chai.should(); |
|
|
17 |
chai.use(chaiAsPromised); |
|
|
18 |
chai.use(sinonChai); |
|
|
19 |
|
|
|
20 |
class TestContext { |
|
|
21 |
|
|
|
22 |
constructor() { |
|
|
23 |
this.stub = sinon.createStubInstance(ChaincodeStub); |
|
|
24 |
this.clientIdentity = sinon.createStubInstance(ClientIdentity); |
|
|
25 |
this.logging = { |
|
|
26 |
getLogger: sinon.stub().returns(sinon.createStubInstance(winston.createLogger().constructor)), |
|
|
27 |
setLevel: sinon.stub(), |
|
|
28 |
}; |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
describe('MyAssetContract', () => { |
|
|
34 |
|
|
|
35 |
let contract; |
|
|
36 |
let ctx; |
|
|
37 |
|
|
|
38 |
beforeEach(() => { |
|
|
39 |
contract = new MyAssetContract(); |
|
|
40 |
ctx = new TestContext(); |
|
|
41 |
ctx.stub.getState.withArgs('1001').resolves(Buffer.from('{"value":"my asset 1001 value"}')); |
|
|
42 |
ctx.stub.getState.withArgs('1002').resolves(Buffer.from('{"value":"my asset 1002 value"}')); |
|
|
43 |
}); |
|
|
44 |
|
|
|
45 |
describe('#myAssetExists', () => { |
|
|
46 |
|
|
|
47 |
it('should return true for a my asset', async () => { |
|
|
48 |
await contract.myAssetExists(ctx, '1001').should.eventually.be.true; |
|
|
49 |
}); |
|
|
50 |
|
|
|
51 |
it('should return false for a my asset that does not exist', async () => { |
|
|
52 |
await contract.myAssetExists(ctx, '1003').should.eventually.be.false; |
|
|
53 |
}); |
|
|
54 |
|
|
|
55 |
}); |
|
|
56 |
|
|
|
57 |
describe('#createMyAsset', () => { |
|
|
58 |
|
|
|
59 |
it('should create a my asset', async () => { |
|
|
60 |
await contract.createMyAsset(ctx, '1003', 'my asset 1003 value'); |
|
|
61 |
ctx.stub.putState.should.have.been.calledOnceWithExactly('1003', Buffer.from('{"value":"my asset 1003 value"}')); |
|
|
62 |
}); |
|
|
63 |
|
|
|
64 |
it('should throw an error for a my asset that already exists', async () => { |
|
|
65 |
await contract.createMyAsset(ctx, '1001', 'myvalue').should.be.rejectedWith(/The my asset 1001 already exists/); |
|
|
66 |
}); |
|
|
67 |
|
|
|
68 |
}); |
|
|
69 |
|
|
|
70 |
describe('#readMyAsset', () => { |
|
|
71 |
|
|
|
72 |
it('should return a my asset', async () => { |
|
|
73 |
await contract.readMyAsset(ctx, '1001').should.eventually.deep.equal({ value: 'my asset 1001 value' }); |
|
|
74 |
}); |
|
|
75 |
|
|
|
76 |
it('should throw an error for a my asset that does not exist', async () => { |
|
|
77 |
await contract.readMyAsset(ctx, '1003').should.be.rejectedWith(/The my asset 1003 does not exist/); |
|
|
78 |
}); |
|
|
79 |
|
|
|
80 |
}); |
|
|
81 |
|
|
|
82 |
describe('#updateMyAsset', () => { |
|
|
83 |
|
|
|
84 |
it('should update a my asset', async () => { |
|
|
85 |
await contract.updateMyAsset(ctx, '1001', 'my asset 1001 new value'); |
|
|
86 |
ctx.stub.putState.should.have.been.calledOnceWithExactly('1001', Buffer.from('{"value":"my asset 1001 new value"}')); |
|
|
87 |
}); |
|
|
88 |
|
|
|
89 |
it('should throw an error for a my asset that does not exist', async () => { |
|
|
90 |
await contract.updateMyAsset(ctx, '1003', 'my asset 1003 new value').should.be.rejectedWith(/The my asset 1003 does not exist/); |
|
|
91 |
}); |
|
|
92 |
|
|
|
93 |
}); |
|
|
94 |
|
|
|
95 |
describe('#deleteMyAsset', () => { |
|
|
96 |
|
|
|
97 |
it('should delete a my asset', async () => { |
|
|
98 |
await contract.deleteMyAsset(ctx, '1001'); |
|
|
99 |
ctx.stub.deleteState.should.have.been.calledOnceWithExactly('1001'); |
|
|
100 |
}); |
|
|
101 |
|
|
|
102 |
it('should throw an error for a my asset that does not exist', async () => { |
|
|
103 |
await contract.deleteMyAsset(ctx, '1003').should.be.rejectedWith(/The my asset 1003 does not exist/); |
|
|
104 |
}); |
|
|
105 |
|
|
|
106 |
}); |
|
|
107 |
|
|
|
108 |
}); |