github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/qcs_test.go (about) 1 package badger_test 2 3 import ( 4 "testing" 5 6 "github.com/dgraph-io/badger/v2" 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/model/flow" 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/storage/badger/operation" 14 "github.com/onflow/flow-go/storage/badger/transaction" 15 "github.com/onflow/flow-go/utils/unittest" 16 ) 17 18 // TestQuorumCertificates_StoreTx tests storing and retrieving of QC. 19 func TestQuorumCertificates_StoreTx(t *testing.T) { 20 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 21 metrics := metrics.NewNoopCollector() 22 store := bstorage.NewQuorumCertificates(metrics, db, 10) 23 qc := unittest.QuorumCertificateFixture() 24 25 err := operation.RetryOnConflictTx(db, transaction.Update, store.StoreTx(qc)) 26 require.NoError(t, err) 27 28 actual, err := store.ByBlockID(qc.BlockID) 29 require.NoError(t, err) 30 31 require.Equal(t, qc, actual) 32 }) 33 } 34 35 // TestQuorumCertificates_StoreTx_OtherQC checks if storing other QC for same blockID results in 36 // expected storage error and already stored value is not overwritten. 37 func TestQuorumCertificates_StoreTx_OtherQC(t *testing.T) { 38 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 39 metrics := metrics.NewNoopCollector() 40 store := bstorage.NewQuorumCertificates(metrics, db, 10) 41 qc := unittest.QuorumCertificateFixture() 42 otherQC := unittest.QuorumCertificateFixture(func(otherQC *flow.QuorumCertificate) { 43 otherQC.View = qc.View 44 otherQC.BlockID = qc.BlockID 45 }) 46 47 err := operation.RetryOnConflictTx(db, transaction.Update, store.StoreTx(qc)) 48 require.NoError(t, err) 49 50 err = operation.RetryOnConflictTx(db, transaction.Update, store.StoreTx(otherQC)) 51 require.ErrorIs(t, err, storage.ErrAlreadyExists) 52 53 actual, err := store.ByBlockID(otherQC.BlockID) 54 require.NoError(t, err) 55 56 require.Equal(t, qc, actual) 57 }) 58 } 59 60 // TestQuorumCertificates_ByBlockID that ByBlockID returns correct sentinel error if no QC for given block ID has been found 61 func TestQuorumCertificates_ByBlockID(t *testing.T) { 62 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 63 metrics := metrics.NewNoopCollector() 64 store := bstorage.NewQuorumCertificates(metrics, db, 10) 65 66 actual, err := store.ByBlockID(unittest.IdentifierFixture()) 67 require.ErrorIs(t, err, storage.ErrNotFound) 68 require.Nil(t, actual) 69 }) 70 }