github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/heights_test.go (about) 1 // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED 2 3 package operation 4 5 import ( 6 "testing" 7 8 "github.com/dgraph-io/badger/v3" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/koko1123/flow-go-1/utils/unittest" 13 ) 14 15 func TestFinalizedInsertUpdateRetrieve(t *testing.T) { 16 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 17 height := uint64(1337) 18 19 err := db.Update(InsertFinalizedHeight(height)) 20 require.Nil(t, err) 21 22 var retrieved uint64 23 err = db.View(RetrieveFinalizedHeight(&retrieved)) 24 require.Nil(t, err) 25 26 assert.Equal(t, retrieved, height) 27 28 height = 9999 29 err = db.Update(UpdateFinalizedHeight(height)) 30 require.Nil(t, err) 31 32 err = db.View(RetrieveFinalizedHeight(&retrieved)) 33 require.Nil(t, err) 34 35 assert.Equal(t, retrieved, height) 36 }) 37 } 38 39 func TestSealedInsertUpdateRetrieve(t *testing.T) { 40 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 41 height := uint64(1337) 42 43 err := db.Update(InsertSealedHeight(height)) 44 require.Nil(t, err) 45 46 var retrieved uint64 47 err = db.View(RetrieveSealedHeight(&retrieved)) 48 require.Nil(t, err) 49 50 assert.Equal(t, retrieved, height) 51 52 height = 9999 53 err = db.Update(UpdateSealedHeight(height)) 54 require.Nil(t, err) 55 56 err = db.View(RetrieveSealedHeight(&retrieved)) 57 require.Nil(t, err) 58 59 assert.Equal(t, retrieved, height) 60 }) 61 } 62 63 func TestLastCompleteBlockHeightInsertUpdateRetrieve(t *testing.T) { 64 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 65 height := uint64(1337) 66 67 err := db.Update(InsertLastCompleteBlockHeight(height)) 68 require.Nil(t, err) 69 70 var retrieved uint64 71 err = db.View(RetrieveLastCompleteBlockHeight(&retrieved)) 72 require.Nil(t, err) 73 74 assert.Equal(t, retrieved, height) 75 76 height = 9999 77 err = db.Update(UpdateLastCompleteBlockHeight(height)) 78 require.Nil(t, err) 79 80 err = db.View(RetrieveLastCompleteBlockHeight(&retrieved)) 81 require.Nil(t, err) 82 83 assert.Equal(t, retrieved, height) 84 }) 85 } 86 87 func TestLastCompleteBlockHeightInsertIfNotExists(t *testing.T) { 88 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 89 height1 := uint64(1337) 90 91 err := db.Update(InsertLastCompleteBlockHeightIfNotExists(height1)) 92 require.Nil(t, err) 93 94 var retrieved uint64 95 err = db.View(RetrieveLastCompleteBlockHeight(&retrieved)) 96 require.Nil(t, err) 97 98 assert.Equal(t, retrieved, height1) 99 100 height2 := uint64(9999) 101 err = db.Update(InsertLastCompleteBlockHeightIfNotExists(height2)) 102 require.Nil(t, err) 103 104 err = db.View(RetrieveLastCompleteBlockHeight(&retrieved)) 105 require.Nil(t, err) 106 107 assert.Equal(t, retrieved, height1) 108 }) 109 }