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

     1  package procedure
     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/model/cluster"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/storage/badger/operation"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  func TestInsertRetrieveClusterBlock(t *testing.T) {
    16  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    17  		block := unittest.ClusterBlockFixture()
    18  
    19  		err := db.Update(InsertClusterBlock(&block))
    20  		require.NoError(t, err)
    21  
    22  		var retrieved cluster.Block
    23  		err = db.View(RetrieveClusterBlock(block.Header.ID(), &retrieved))
    24  		require.NoError(t, err)
    25  
    26  		require.Equal(t, block, retrieved)
    27  	})
    28  }
    29  
    30  func TestFinalizeClusterBlock(t *testing.T) {
    31  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    32  		parent := unittest.ClusterBlockFixture()
    33  
    34  		block := unittest.ClusterBlockWithParent(&parent)
    35  
    36  		err := db.Update(InsertClusterBlock(&block))
    37  		require.NoError(t, err)
    38  
    39  		err = db.Update(operation.IndexClusterBlockHeight(block.Header.ChainID, parent.Header.Height, parent.ID()))
    40  		require.NoError(t, err)
    41  
    42  		err = db.Update(operation.InsertClusterFinalizedHeight(block.Header.ChainID, parent.Header.Height))
    43  		require.NoError(t, err)
    44  
    45  		err = db.Update(FinalizeClusterBlock(block.Header.ID()))
    46  		require.NoError(t, err)
    47  
    48  		var boundary uint64
    49  		err = db.View(operation.RetrieveClusterFinalizedHeight(block.Header.ChainID, &boundary))
    50  		require.NoError(t, err)
    51  		require.Equal(t, block.Header.Height, boundary)
    52  
    53  		var headID flow.Identifier
    54  		err = db.View(operation.LookupClusterBlockHeight(block.Header.ChainID, boundary, &headID))
    55  		require.NoError(t, err)
    56  		require.Equal(t, block.ID(), headID)
    57  	})
    58  }