github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/commit_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/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/onflow/flow-go/module/metrics" 12 "github.com/onflow/flow-go/storage" 13 "github.com/onflow/flow-go/utils/unittest" 14 15 badgerstorage "github.com/onflow/flow-go/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 }