github.com/koko1123/flow-go-1@v0.29.6/storage/badger/commit_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  // TestCommitsStoreAndRetrieve tests that a commit can be stored, retrieved and attempted to be stored again without an error
    19  func TestCommitsStoreAndRetrieve(t *testing.T) {
    20  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    21  		metrics := metrics.NewNoopCollector()
    22  		store := badgerstorage.NewCommits(metrics, db)
    23  
    24  		// attempt to get a invalid commit
    25  		_, err := store.ByBlockID(unittest.IdentifierFixture())
    26  		assert.True(t, errors.Is(err, storage.ErrNotFound))
    27  
    28  		// store a commit in db
    29  		blockID := unittest.IdentifierFixture()
    30  		expected := unittest.StateCommitmentFixture()
    31  		err = store.Store(blockID, expected)
    32  		require.NoError(t, err)
    33  
    34  		// retrieve the commit by ID
    35  		actual, err := store.ByBlockID(blockID)
    36  		require.NoError(t, err)
    37  		assert.Equal(t, expected, actual)
    38  
    39  		// re-insert the commit - should be idempotent
    40  		err = store.Store(blockID, expected)
    41  		require.NoError(t, err)
    42  	})
    43  }