github.com/koko1123/flow-go-1@v0.29.6/storage/badger/headers_test.go (about)

     1  package badger_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/koko1123/flow-go-1/storage/badger/operation"
     8  
     9  	"github.com/dgraph-io/badger/v3"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/koko1123/flow-go-1/module/metrics"
    13  	"github.com/koko1123/flow-go-1/storage"
    14  	"github.com/koko1123/flow-go-1/utils/unittest"
    15  
    16  	badgerstorage "github.com/koko1123/flow-go-1/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  }