github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/epoch_commits_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 // TestEpochCommitStoreAndRetrieve tests that a commit can be stored, retrieved and attempted to be stored again without an error 19 func TestEpochCommitStoreAndRetrieve(t *testing.T) { 20 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 21 metrics := metrics.NewNoopCollector() 22 store := badgerstorage.NewEpochCommits(metrics, db) 23 24 // attempt to get a invalid commit 25 _, err := store.ByID(unittest.IdentifierFixture()) 26 assert.True(t, errors.Is(err, storage.ErrNotFound)) 27 28 // store a commit in db 29 expected := unittest.EpochCommitFixture() 30 err = store.Store(expected) 31 require.NoError(t, err) 32 33 // retrieve the commit by ID 34 actual, err := store.ByID(expected.ID()) 35 require.NoError(t, err) 36 assert.Equal(t, expected, actual) 37 38 // test storing same epoch commit 39 err = store.Store(expected) 40 require.NoError(t, err) 41 }) 42 }