[de9008]: / contract / test / my-asset-contract.js

Download this file

108 lines (77 with data), 3.7 kB

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