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

     1  package badger_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v2"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/module/metrics"
    10  	badgerstorage "github.com/onflow/flow-go/storage/badger"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  func TestBlockStoreAndRetrieve(t *testing.T) {
    15  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    16  		cacheMetrics := &metrics.NoopCollector{}
    17  		// verify after storing a block should be able to retrieve it back
    18  		blocks := badgerstorage.InitAll(cacheMetrics, db).Blocks
    19  		block := unittest.FullBlockFixture()
    20  		block.SetPayload(unittest.PayloadFixture(unittest.WithAllTheFixins))
    21  
    22  		err := blocks.Store(&block)
    23  		require.NoError(t, err)
    24  
    25  		retrieved, err := blocks.ByID(block.ID())
    26  		require.NoError(t, err)
    27  
    28  		require.Equal(t, &block, retrieved)
    29  
    30  		// verify after a restart, the block stored in the database is the same
    31  		// as the original
    32  		blocksAfterRestart := badgerstorage.InitAll(cacheMetrics, db).Blocks
    33  		receivedAfterRestart, err := blocksAfterRestart.ByID(block.ID())
    34  		require.NoError(t, err)
    35  
    36  		require.Equal(t, &block, receivedAfterRestart)
    37  	})
    38  }