github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/headers_test.go (about) 1 // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED 2 3 package operation 4 5 import ( 6 "testing" 7 "time" 8 9 "github.com/dgraph-io/badger/v3" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/koko1123/flow-go-1/model/flow" 14 "github.com/koko1123/flow-go-1/utils/unittest" 15 "github.com/onflow/flow-go/crypto" 16 ) 17 18 func TestHeaderInsertCheckRetrieve(t *testing.T) { 19 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 20 expected := &flow.Header{ 21 View: 1337, 22 Timestamp: time.Now().UTC(), 23 ParentID: flow.Identifier{0x11}, 24 PayloadHash: flow.Identifier{0x22}, 25 ParentVoterIndices: []byte{0x44}, 26 ParentVoterSigData: []byte{0x88}, 27 ProposerID: flow.Identifier{0x33}, 28 ProposerSigData: crypto.Signature{0x77}, 29 } 30 blockID := expected.ID() 31 32 err := db.Update(InsertHeader(expected.ID(), expected)) 33 require.Nil(t, err) 34 35 var actual flow.Header 36 err = db.View(RetrieveHeader(blockID, &actual)) 37 require.Nil(t, err) 38 39 assert.Equal(t, *expected, actual) 40 }) 41 } 42 43 func TestHeaderIDIndexByCollectionID(t *testing.T) { 44 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 45 46 headerID := unittest.IdentifierFixture() 47 collectionID := unittest.IdentifierFixture() 48 49 err := db.Update(IndexCollectionBlock(collectionID, headerID)) 50 require.Nil(t, err) 51 52 actualID := &flow.Identifier{} 53 err = db.View(LookupCollectionBlock(collectionID, actualID)) 54 require.Nil(t, err) 55 assert.Equal(t, headerID, *actualID) 56 }) 57 } 58 59 func TestBlockHeightIndexLookup(t *testing.T) { 60 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 61 62 height := uint64(1337) 63 expected := flow.Identifier{0x01, 0x02, 0x03} 64 65 err := db.Update(IndexBlockHeight(height, expected)) 66 require.Nil(t, err) 67 68 var actual flow.Identifier 69 err = db.View(LookupBlockHeight(height, &actual)) 70 require.Nil(t, err) 71 72 assert.Equal(t, expected, actual) 73 }) 74 }