github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/headers_test.go (about)

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