github.com/yimialmonte/fabric@v2.1.1+incompatible/core/ledger/confighistory/db_helper_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package confighistory 8 9 import ( 10 "bytes" 11 "math" 12 "os" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestEncodeDecodeCompositeKey(t *testing.T) { 20 sampleKeys := []*compositeKey{ 21 {ns: "ns0", key: "key0", blockNum: 0}, 22 {ns: "ns1", key: "key1", blockNum: 1}, 23 {ns: "ns2", key: "key2", blockNum: 99}, 24 {ns: "ns3", key: "key3", blockNum: math.MaxUint64}, 25 } 26 for _, k := range sampleKeys { 27 k1 := decodeCompositeKey(encodeCompositeKey(k.ns, k.key, k.blockNum)) 28 assert.Equal(t, k, k1) 29 } 30 } 31 32 func TestCompareEncodedHeight(t *testing.T) { 33 assert.Equal(t, bytes.Compare(encodeBlockNum(20), encodeBlockNum(40)), 1) 34 assert.Equal(t, bytes.Compare(encodeBlockNum(40), encodeBlockNum(10)), -1) 35 } 36 37 func TestQueries(t *testing.T) { 38 testDBPath := "/tmp/fabric/core/ledger/confighistory" 39 deleteTestPath(t, testDBPath) 40 provider, err := newDBProvider(testDBPath) 41 require.NoError(t, err) 42 defer deleteTestPath(t, testDBPath) 43 44 db := provider.getDB("ledger1") 45 // A query on an empty store 46 checkEntryAt(t, "testcase-query1", db, "ns1", "key1", 45, nil) 47 // test data 48 sampleData := []*compositeKV{ 49 {&compositeKey{ns: "ns1", key: "key1", blockNum: 40}, []byte("val1_40")}, 50 {&compositeKey{ns: "ns1", key: "key1", blockNum: 30}, []byte("val1_30")}, 51 {&compositeKey{ns: "ns1", key: "key1", blockNum: 20}, []byte("val1_20")}, 52 {&compositeKey{ns: "ns1", key: "key1", blockNum: 10}, []byte("val1_10")}, 53 {&compositeKey{ns: "ns1", key: "key1", blockNum: 0}, []byte("val1_0")}, 54 } 55 populateDBWithSampleData(t, db, sampleData) 56 // access most recent entry below ht=[45] - expected item is the one committed at ht = 40 57 checkRecentEntryBelow(t, "testcase-query2", db, "ns1", "key1", 45, sampleData[0]) 58 checkRecentEntryBelow(t, "testcase-query3", db, "ns1", "key1", 35, sampleData[1]) 59 checkRecentEntryBelow(t, "testcase-query4", db, "ns1", "key1", 30, sampleData[2]) 60 checkRecentEntryBelow(t, "testcase-query5", db, "ns1", "key1", 10, sampleData[4]) 61 62 checkEntryAt(t, "testcase-query6", db, "ns1", "key1", 40, sampleData[0]) 63 checkEntryAt(t, "testcase-query7", db, "ns1", "key1", 30, sampleData[1]) 64 checkEntryAt(t, "testcase-query8", db, "ns1", "key1", 0, sampleData[4]) 65 checkEntryAt(t, "testcase-query9", db, "ns1", "key1", 35, nil) 66 checkEntryAt(t, "testcase-query10", db, "ns1", "key1", 45, nil) 67 } 68 69 func populateDBWithSampleData(t *testing.T, db *db, sampledata []*compositeKV) { 70 batch := newBatch() 71 for _, data := range sampledata { 72 batch.add(data.ns, data.key, data.blockNum, data.value) 73 } 74 assert.NoError(t, db.writeBatch(batch, true)) 75 } 76 77 func checkRecentEntryBelow(t *testing.T, testcase string, db *db, ns, key string, commitHt uint64, expectedOutput *compositeKV) { 78 t.Run(testcase, 79 func(t *testing.T) { 80 kv, err := db.mostRecentEntryBelow(commitHt, ns, key) 81 assert.NoError(t, err) 82 assert.Equal(t, expectedOutput, kv) 83 }) 84 } 85 86 func checkEntryAt(t *testing.T, testcase string, db *db, ns, key string, commitHt uint64, expectedOutput *compositeKV) { 87 t.Run(testcase, 88 func(t *testing.T) { 89 kv, err := db.entryAt(commitHt, ns, key) 90 assert.NoError(t, err) 91 assert.Equal(t, expectedOutput, kv) 92 }) 93 } 94 95 func deleteTestPath(t *testing.T, dbPath string) { 96 err := os.RemoveAll(dbPath) 97 assert.NoError(t, err) 98 }