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

     1  package badger_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/onflow/flow-go/storage/badger/operation"
     8  
     9  	"github.com/dgraph-io/badger/v2"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/onflow/flow-go/module/metrics"
    13  	"github.com/onflow/flow-go/storage"
    14  	"github.com/onflow/flow-go/utils/unittest"
    15  
    16  	badgerstorage "github.com/onflow/flow-go/storage/badger"
    17  )
    18  
    19  func TestHeaderStoreRetrieve(t *testing.T) {
    20  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    21  		metrics := metrics.NewNoopCollector()
    22  		headers := badgerstorage.NewHeaders(metrics, db)
    23  
    24  		block := unittest.BlockFixture()
    25  
    26  		// store header
    27  		err := headers.Store(block.Header)
    28  		require.NoError(t, err)
    29  
    30  		// index the header
    31  		err = operation.RetryOnConflict(db.Update, operation.IndexBlockHeight(block.Header.Height, block.ID()))
    32  		require.NoError(t, err)
    33  
    34  		// retrieve header by height
    35  		actual, err := headers.ByHeight(block.Header.Height)
    36  		require.NoError(t, err)
    37  		require.Equal(t, block.Header, actual)
    38  	})
    39  }
    40  
    41  func TestHeaderRetrieveWithoutStore(t *testing.T) {
    42  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    43  		metrics := metrics.NewNoopCollector()
    44  		headers := badgerstorage.NewHeaders(metrics, db)
    45  
    46  		header := unittest.BlockHeaderFixture()
    47  
    48  		// retrieve header by height, should err as not store before height
    49  		_, err := headers.ByHeight(header.Height)
    50  		require.True(t, errors.Is(err, storage.ErrNotFound))
    51  	})
    52  }