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

     1  package badger_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/dgraph-io/badger/v3"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/koko1123/flow-go-1/module/metrics"
    12  	"github.com/koko1123/flow-go-1/storage"
    13  	badgerstorage "github.com/koko1123/flow-go-1/storage/badger"
    14  	"github.com/koko1123/flow-go-1/utils/unittest"
    15  )
    16  
    17  func TestBlocks(t *testing.T) {
    18  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    19  		store := badgerstorage.NewBlocks(db, nil, nil)
    20  
    21  		// check retrieval of non-existing key
    22  		_, err := store.GetLastFullBlockHeight()
    23  		assert.Error(t, err)
    24  		assert.True(t, errors.Is(err, storage.ErrNotFound))
    25  
    26  		// insert a value for height
    27  		var height1 = uint64(1234)
    28  		err = store.UpdateLastFullBlockHeight(height1)
    29  		assert.NoError(t, err)
    30  
    31  		// check value can be retrieved
    32  		actual, err := store.GetLastFullBlockHeight()
    33  		assert.NoError(t, err)
    34  		assert.Equal(t, height1, actual)
    35  
    36  		// update the value for height
    37  		var height2 = uint64(1234)
    38  		err = store.UpdateLastFullBlockHeight(height2)
    39  		assert.NoError(t, err)
    40  
    41  		// check that the new value can be retrieved
    42  		actual, err = store.GetLastFullBlockHeight()
    43  		assert.NoError(t, err)
    44  		assert.Equal(t, height2, actual)
    45  	})
    46  }
    47  
    48  func TestBlockStoreAndRetrieve(t *testing.T) {
    49  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    50  		cacheMetrics := &metrics.NoopCollector{}
    51  		// verify after storing a block should be able to retrieve it back
    52  		blocks := badgerstorage.InitAll(cacheMetrics, db).Blocks
    53  		block := unittest.FullBlockFixture()
    54  		block.SetPayload(unittest.PayloadFixture(unittest.WithAllTheFixins))
    55  
    56  		err := blocks.Store(&block)
    57  		require.NoError(t, err)
    58  
    59  		retrieved, err := blocks.ByID(block.ID())
    60  		require.NoError(t, err)
    61  
    62  		require.Equal(t, &block, retrieved)
    63  
    64  		// verify after a restart, the block stored in the database is the same
    65  		// as the original
    66  		blocksAfterRestart := badgerstorage.InitAll(cacheMetrics, db).Blocks
    67  		receivedAfterRestart, err := blocksAfterRestart.ByID(block.ID())
    68  		require.NoError(t, err)
    69  
    70  		require.Equal(t, &block, receivedAfterRestart)
    71  	})
    72  }