github.com/renegr87/renegr87@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/rwsetutil/kv_rwset_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package rwsetutil 8 9 import ( 10 "io/ioutil" 11 "testing" 12 13 "github.com/davecgh/go-spew/spew" 14 "github.com/golang/protobuf/proto" 15 "github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 const kvrwsetV1ProtoBytesFile = "testdata/kvrwsetV1ProtoBytes" 20 21 // TestKVRWSetV1BackwardCompatible passes if the 'KVRWSet' messgae declared in the latest version 22 // is able to unmarshal the protobytes that are produced by the 'KVRWSet' proto message declared in 23 // v1.0. This is to make sure that any incompatible changes does not go uncaught. 24 func TestKVRWSetV1BackwardCompatible(t *testing.T) { 25 protoBytes, err := ioutil.ReadFile(kvrwsetV1ProtoBytesFile) 26 assert.NoError(t, err) 27 kvrwset1 := &kvrwset.KVRWSet{} 28 assert.NoError(t, proto.Unmarshal(protoBytes, kvrwset1)) 29 kvrwset2 := constructSampleKVRWSet() 30 t.Logf("kvrwset1=%s, kvrwset2=%s", spew.Sdump(kvrwset1), spew.Sdump(kvrwset2)) 31 assert.Equal(t, kvrwset2, kvrwset1) 32 } 33 34 // PrepareBinaryFileSampleKVRWSetV1 constructs a proto message for kvrwset and marshals its bytes to file 'kvrwsetV1ProtoBytes'. 35 // this code should be run on fabric version 1.0 so as to produce a sample file of proto message declared in V1 36 // In order to invoke this function on V1 code, copy this over on to V1 code, make the first letter as 'T', and finally invoke this function 37 // using golang test framwork 38 func PrepareBinaryFileSampleKVRWSetV1(t *testing.T) { 39 b, err := proto.Marshal(constructSampleKVRWSet()) 40 assert.NoError(t, err) 41 assert.NoError(t, ioutil.WriteFile(kvrwsetV1ProtoBytesFile, b, 0644)) 42 } 43 44 func constructSampleKVRWSet() *kvrwset.KVRWSet { 45 rqi1 := &kvrwset.RangeQueryInfo{StartKey: "k0", EndKey: "k9", ItrExhausted: true} 46 SetRawReads(rqi1, []*kvrwset.KVRead{ 47 {Key: "k1", Version: &kvrwset.Version{BlockNum: 1, TxNum: 1}}, 48 {Key: "k2", Version: &kvrwset.Version{BlockNum: 1, TxNum: 2}}, 49 }) 50 51 rqi2 := &kvrwset.RangeQueryInfo{StartKey: "k00", EndKey: "k90", ItrExhausted: true} 52 SetMerkelSummary(rqi2, &kvrwset.QueryReadsMerkleSummary{MaxDegree: 5, MaxLevel: 4, MaxLevelHashes: [][]byte{[]byte("Hash-1"), []byte("Hash-2")}}) 53 return &kvrwset.KVRWSet{ 54 Reads: []*kvrwset.KVRead{{Key: "key1", Version: &kvrwset.Version{BlockNum: 1, TxNum: 1}}}, 55 RangeQueriesInfo: []*kvrwset.RangeQueryInfo{rqi1, rqi2}, 56 Writes: []*kvrwset.KVWrite{{Key: "key2", IsDelete: false, Value: []byte("value2")}}, 57 } 58 }