github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/tests/test_helper.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package tests 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/core/ledger" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 // testhelper embeds (1) a client, (2) a committer and (3) a verifier, all three operate on 17 // a ledger instance and add helping/resuable functionality on top of ledger apis that helps 18 // in avoiding the repeation in the actual tests code. 19 // the 'client' adds value to the simulation relation apis, the 'committer' helps in cutting the 20 // next block and committing the block, and finally, the verifier helps in veryfying that the 21 // ledger apis returns correct values based on the blocks submitted 22 type testhelper struct { 23 *client 24 *committer 25 *verifier 26 lgr ledger.PeerLedger 27 lgrid string 28 assert *assert.Assertions 29 } 30 31 // newTestHelperCreateLgr creates a new ledger and retruns a 'testhelper' for the ledger 32 func (env *env) newTestHelperCreateLgr(id string, t *testing.T) *testhelper { 33 genesisBlk, err := constructTestGenesisBlock(id) 34 assert.NoError(t, err) 35 lgr, err := env.ledgerMgr.CreateLedger(id, genesisBlk) 36 assert.NoError(t, err) 37 client, committer, verifier := newClient(lgr, id, t), newCommitter(lgr, t), newVerifier(lgr, t) 38 return &testhelper{client, committer, verifier, lgr, id, assert.New(t)} 39 } 40 41 // newTestHelperOpenLgr opens an existing ledger and retruns a 'testhelper' for the ledger 42 func (env *env) newTestHelperOpenLgr(id string, t *testing.T) *testhelper { 43 lgr, err := env.ledgerMgr.OpenLedger(id) 44 assert.NoError(t, err) 45 client, committer, verifier := newClient(lgr, id, t), newCommitter(lgr, t), newVerifier(lgr, t) 46 return &testhelper{client, committer, verifier, lgr, id, assert.New(t)} 47 } 48 49 // cutBlockAndCommitLegacy gathers all the transactions simulated by the test code (by calling 50 // the functions available in the 'client') and cuts the next block and commits to the ledger 51 func (h *testhelper) cutBlockAndCommitLegacy() *ledger.BlockAndPvtData { 52 defer func() { 53 h.simulatedTrans = nil 54 h.missingPvtData = make(ledger.TxMissingPvtDataMap) 55 }() 56 return h.committer.cutBlockAndCommitLegacy(h.simulatedTrans, h.missingPvtData) 57 } 58 59 func (h *testhelper) cutBlockAndCommitExpectError() (*ledger.BlockAndPvtData, error) { 60 defer func() { 61 h.simulatedTrans = nil 62 h.missingPvtData = make(ledger.TxMissingPvtDataMap) 63 }() 64 return h.committer.cutBlockAndCommitExpectError(h.simulatedTrans, h.missingPvtData) 65 } 66 67 // assertError is a helper function that can be called as assertError(f()) where 'f' is some other function 68 // this function assumes that the last return type of function 'f' is of type 'error' 69 func (h *testhelper) assertError(output ...interface{}) { 70 lastParam := output[len(output)-1] 71 assert.NotNil(h.t, lastParam) 72 h.assert.Error(lastParam.(error)) 73 } 74 75 // assertNoError see comment on function 'assertError' 76 func (h *testhelper) assertNoError(output ...interface{}) { 77 h.assert.Nil(output[len(output)-1]) 78 }