github.com/ewagmig/fabric@v2.1.1+incompatible/core/ledger/kvledger/history/kv_encoding_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package history 8 9 import ( 10 "bytes" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var strKeySep = string(compositeKeySep) 17 18 func TestCompositeKeyConstruction(t *testing.T) { 19 type keyComponents struct { 20 ns, key string 21 blkNum, tranNum uint64 22 } 23 24 testData := []*keyComponents{ 25 {"ns1", "key1", 1, 0}, 26 {"ns1", "key1\x00", 1, 5}, 27 {"ns1", "key1\x00\x00", 100, 100}, 28 {"ns1", "key\x00\x001", 100, 100}, 29 {"ns1", "\x00key\x00\x001", 100, 100}, 30 } 31 32 for _, testDatum := range testData { 33 key := constructDataKey(testDatum.ns, testDatum.key, testDatum.blkNum, testDatum.tranNum) 34 rangeScan := constructRangeScan(testDatum.ns, testDatum.key) 35 assert.Equal(t, bytes.Compare(rangeScan.startKey, key), -1) //startKey should be smaller than key 36 assert.Equal(t, bytes.Compare(rangeScan.endKey, key), 1) //endKey should be greater than key 37 } 38 39 for i, testDatum := range testData { 40 for j, another := range testData { 41 if i == j { 42 continue 43 } 44 rangeScan := constructRangeScan(testDatum.ns, testDatum.key) 45 anotherKey := constructDataKey(another.ns, another.key, another.blkNum, another.tranNum) 46 assert.False(t, bytes.Compare(anotherKey, rangeScan.startKey) == 1 && bytes.Compare(anotherKey, rangeScan.endKey) == -1) //any key should not fall in the range of start/end key range query for any other key 47 } 48 } 49 } 50 51 func TestSplitCompositeKey(t *testing.T) { 52 dataKey := constructDataKey("ns1", "key1", 20, 200) 53 rangeScan := constructRangeScan("ns1", "key1") 54 blkNum, txNum, err := rangeScan.decodeBlockNumTranNum(dataKey) 55 assert.NoError(t, err) 56 assert.Equal(t, blkNum, uint64(20)) 57 assert.Equal(t, txNum, uint64(200)) 58 }