github.com/koko1123/flow-go-1@v0.29.6/storage/badger/results_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/require" 9 10 "github.com/koko1123/flow-go-1/module/metrics" 11 "github.com/koko1123/flow-go-1/storage" 12 bstorage "github.com/koko1123/flow-go-1/storage/badger" 13 "github.com/koko1123/flow-go-1/utils/unittest" 14 ) 15 16 func TestResultStoreAndRetrieve(t *testing.T) { 17 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 18 metrics := metrics.NewNoopCollector() 19 store := bstorage.NewExecutionResults(metrics, db) 20 21 result := unittest.ExecutionResultFixture() 22 blockID := unittest.IdentifierFixture() 23 err := store.Store(result) 24 require.NoError(t, err) 25 26 err = store.Index(blockID, result.ID()) 27 require.NoError(t, err) 28 29 actual, err := store.ByBlockID(blockID) 30 require.NoError(t, err) 31 32 require.Equal(t, result, actual) 33 }) 34 } 35 36 func TestResultStoreTwice(t *testing.T) { 37 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 38 metrics := metrics.NewNoopCollector() 39 store := bstorage.NewExecutionResults(metrics, db) 40 41 result := unittest.ExecutionResultFixture() 42 blockID := unittest.IdentifierFixture() 43 err := store.Store(result) 44 require.NoError(t, err) 45 46 err = store.Index(blockID, result.ID()) 47 require.NoError(t, err) 48 49 err = store.Store(result) 50 require.NoError(t, err) 51 52 err = store.Index(blockID, result.ID()) 53 require.NoError(t, err) 54 }) 55 } 56 57 func TestResultBatchStoreTwice(t *testing.T) { 58 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 59 metrics := metrics.NewNoopCollector() 60 store := bstorage.NewExecutionResults(metrics, db) 61 62 result := unittest.ExecutionResultFixture() 63 blockID := unittest.IdentifierFixture() 64 65 batch := bstorage.NewBatch(db) 66 err := store.BatchStore(result, batch) 67 require.NoError(t, err) 68 69 err = store.BatchIndex(blockID, result.ID(), batch) 70 require.NoError(t, err) 71 72 require.NoError(t, batch.Flush()) 73 74 batch = bstorage.NewBatch(db) 75 err = store.BatchStore(result, batch) 76 require.NoError(t, err) 77 78 err = store.BatchIndex(blockID, result.ID(), batch) 79 require.NoError(t, err) 80 81 require.NoError(t, batch.Flush()) 82 }) 83 } 84 85 func TestResultStoreTwoDifferentResultsShouldFail(t *testing.T) { 86 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 87 metrics := metrics.NewNoopCollector() 88 store := bstorage.NewExecutionResults(metrics, db) 89 90 result1 := unittest.ExecutionResultFixture() 91 result2 := unittest.ExecutionResultFixture() 92 blockID := unittest.IdentifierFixture() 93 err := store.Store(result1) 94 require.NoError(t, err) 95 96 err = store.Index(blockID, result1.ID()) 97 require.NoError(t, err) 98 99 // we can store a different result, but we can't index 100 // a different result for that block, because it will mean 101 // one block has two different results. 102 err = store.Store(result2) 103 require.NoError(t, err) 104 105 err = store.Index(blockID, result2.ID()) 106 require.Error(t, err) 107 require.True(t, errors.Is(err, storage.ErrDataMismatch)) 108 }) 109 } 110 111 func TestResultStoreForceIndexOverridesMapping(t *testing.T) { 112 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 113 metrics := metrics.NewNoopCollector() 114 store := bstorage.NewExecutionResults(metrics, db) 115 116 result1 := unittest.ExecutionResultFixture() 117 result2 := unittest.ExecutionResultFixture() 118 blockID := unittest.IdentifierFixture() 119 err := store.Store(result1) 120 require.NoError(t, err) 121 err = store.Index(blockID, result1.ID()) 122 require.NoError(t, err) 123 124 err = store.Store(result2) 125 require.NoError(t, err) 126 127 // force index 128 err = store.ForceIndex(blockID, result2.ID()) 129 require.NoError(t, err) 130 131 // retrieve index to make sure it points to second ER now 132 byBlockID, err := store.ByBlockID(blockID) 133 134 require.Equal(t, result2, byBlockID) 135 require.NoError(t, err) 136 }) 137 }