github.com/koko1123/flow-go-1@v0.29.6/storage/badger/cluster_payloads_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  	"github.com/koko1123/flow-go-1/utils/unittest"
    14  
    15  	badgerstorage "github.com/koko1123/flow-go-1/storage/badger"
    16  )
    17  
    18  func TestStoreRetrieveClusterPayload(t *testing.T) {
    19  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    20  		metrics := metrics.NewNoopCollector()
    21  		store := badgerstorage.NewClusterPayloads(metrics, db)
    22  
    23  		blockID := unittest.IdentifierFixture()
    24  		expected := unittest.ClusterPayloadFixture(5)
    25  
    26  		// store payload
    27  		err := store.Store(blockID, expected)
    28  		require.NoError(t, err)
    29  
    30  		// fetch payload
    31  		payload, err := store.ByBlockID(blockID)
    32  		require.NoError(t, err)
    33  		require.Equal(t, expected, payload)
    34  
    35  		// storing again should error with key already exists
    36  		err = store.Store(blockID, expected)
    37  		require.True(t, errors.Is(err, storage.ErrAlreadyExists))
    38  	})
    39  }
    40  
    41  func TestClusterPayloadRetrieveWithoutStore(t *testing.T) {
    42  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    43  		metrics := metrics.NewNoopCollector()
    44  		store := badgerstorage.NewClusterPayloads(metrics, db)
    45  
    46  		blockID := unittest.IdentifierFixture()
    47  
    48  		_, err := store.ByBlockID(blockID)
    49  		assert.True(t, errors.Is(err, storage.ErrNotFound))
    50  	})
    51  }