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

     1  package badger_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/dgraph-io/badger/v2"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/module/metrics"
    11  	"github.com/onflow/flow-go/storage"
    12  	bstorage "github.com/onflow/flow-go/storage/badger"
    13  	"github.com/onflow/flow-go/utils/unittest"
    14  )
    15  
    16  func TestApprovalStoreAndRetrieve(t *testing.T) {
    17  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    18  		metrics := metrics.NewNoopCollector()
    19  		store := bstorage.NewResultApprovals(metrics, db)
    20  
    21  		approval := unittest.ResultApprovalFixture()
    22  		err := store.Store(approval)
    23  		require.NoError(t, err)
    24  
    25  		err = store.Index(approval.Body.ExecutionResultID, approval.Body.ChunkIndex, approval.ID())
    26  		require.NoError(t, err)
    27  
    28  		byID, err := store.ByID(approval.ID())
    29  		require.NoError(t, err)
    30  		require.Equal(t, approval, byID)
    31  
    32  		byChunk, err := store.ByChunk(approval.Body.ExecutionResultID, approval.Body.ChunkIndex)
    33  		require.NoError(t, err)
    34  		require.Equal(t, approval, byChunk)
    35  	})
    36  }
    37  
    38  func TestApprovalStoreTwice(t *testing.T) {
    39  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    40  		metrics := metrics.NewNoopCollector()
    41  		store := bstorage.NewResultApprovals(metrics, db)
    42  
    43  		approval := unittest.ResultApprovalFixture()
    44  		err := store.Store(approval)
    45  		require.NoError(t, err)
    46  
    47  		err = store.Index(approval.Body.ExecutionResultID, approval.Body.ChunkIndex, approval.ID())
    48  		require.NoError(t, err)
    49  
    50  		err = store.Store(approval)
    51  		require.NoError(t, err)
    52  
    53  		err = store.Index(approval.Body.ExecutionResultID, approval.Body.ChunkIndex, approval.ID())
    54  		require.NoError(t, err)
    55  	})
    56  }
    57  
    58  func TestApprovalStoreTwoDifferentApprovalsShouldFail(t *testing.T) {
    59  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    60  		metrics := metrics.NewNoopCollector()
    61  		store := bstorage.NewResultApprovals(metrics, db)
    62  
    63  		approval1 := unittest.ResultApprovalFixture()
    64  		approval2 := unittest.ResultApprovalFixture()
    65  
    66  		err := store.Store(approval1)
    67  		require.NoError(t, err)
    68  
    69  		err = store.Index(approval1.Body.ExecutionResultID, approval1.Body.ChunkIndex, approval1.ID())
    70  		require.NoError(t, err)
    71  
    72  		// we can store a different approval, but we can't index a different
    73  		// approval for the same chunk.
    74  		err = store.Store(approval2)
    75  		require.NoError(t, err)
    76  
    77  		err = store.Index(approval1.Body.ExecutionResultID, approval1.Body.ChunkIndex, approval2.ID())
    78  		require.Error(t, err)
    79  		require.True(t, errors.Is(err, storage.ErrDataMismatch))
    80  	})
    81  }