github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/heights_test.go (about)

     1  package operation
     2  
     3  import (
     4  	"math/rand"
     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/storage"
    12  	"github.com/onflow/flow-go/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.NoError(t, err)
    21  
    22  		var retrieved uint64
    23  		err = db.View(RetrieveFinalizedHeight(&retrieved))
    24  		require.NoError(t, err)
    25  
    26  		assert.Equal(t, retrieved, height)
    27  
    28  		height = 9999
    29  		err = db.Update(UpdateFinalizedHeight(height))
    30  		require.NoError(t, err)
    31  
    32  		err = db.View(RetrieveFinalizedHeight(&retrieved))
    33  		require.NoError(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.NoError(t, err)
    45  
    46  		var retrieved uint64
    47  		err = db.View(RetrieveSealedHeight(&retrieved))
    48  		require.NoError(t, err)
    49  
    50  		assert.Equal(t, retrieved, height)
    51  
    52  		height = 9999
    53  		err = db.Update(UpdateSealedHeight(height))
    54  		require.NoError(t, err)
    55  
    56  		err = db.View(RetrieveSealedHeight(&retrieved))
    57  		require.NoError(t, err)
    58  
    59  		assert.Equal(t, retrieved, height)
    60  	})
    61  }
    62  
    63  func TestEpochFirstBlockIndex_InsertRetrieve(t *testing.T) {
    64  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    65  		height := rand.Uint64()
    66  		epoch := rand.Uint64()
    67  
    68  		// retrieve when empty errors
    69  		var retrieved uint64
    70  		err := db.View(RetrieveEpochFirstHeight(epoch, &retrieved))
    71  		require.ErrorIs(t, err, storage.ErrNotFound)
    72  
    73  		// can insert
    74  		err = db.Update(InsertEpochFirstHeight(epoch, height))
    75  		require.NoError(t, err)
    76  
    77  		// can retrieve
    78  		err = db.View(RetrieveEpochFirstHeight(epoch, &retrieved))
    79  		require.NoError(t, err)
    80  		assert.Equal(t, retrieved, height)
    81  
    82  		// retrieve non-existent key errors
    83  		err = db.View(RetrieveEpochFirstHeight(epoch+1, &retrieved))
    84  		require.ErrorIs(t, err, storage.ErrNotFound)
    85  
    86  		// insert existent key errors
    87  		err = db.Update(InsertEpochFirstHeight(epoch, height))
    88  		require.ErrorIs(t, err, storage.ErrAlreadyExists)
    89  	})
    90  }
    91  
    92  func TestLastCompleteBlockHeightInsertUpdateRetrieve(t *testing.T) {
    93  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    94  		height := uint64(1337)
    95  
    96  		err := db.Update(InsertLastCompleteBlockHeight(height))
    97  		require.NoError(t, err)
    98  
    99  		var retrieved uint64
   100  		err = db.View(RetrieveLastCompleteBlockHeight(&retrieved))
   101  		require.NoError(t, err)
   102  
   103  		assert.Equal(t, retrieved, height)
   104  
   105  		height = 9999
   106  		err = db.Update(UpdateLastCompleteBlockHeight(height))
   107  		require.NoError(t, err)
   108  
   109  		err = db.View(RetrieveLastCompleteBlockHeight(&retrieved))
   110  		require.NoError(t, err)
   111  
   112  		assert.Equal(t, retrieved, height)
   113  	})
   114  }
   115  
   116  func TestLastCompleteBlockHeightInsertIfNotExists(t *testing.T) {
   117  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   118  		height1 := uint64(1337)
   119  
   120  		err := db.Update(InsertLastCompleteBlockHeightIfNotExists(height1))
   121  		require.NoError(t, err)
   122  
   123  		var retrieved uint64
   124  		err = db.View(RetrieveLastCompleteBlockHeight(&retrieved))
   125  		require.NoError(t, err)
   126  
   127  		assert.Equal(t, retrieved, height1)
   128  
   129  		height2 := uint64(9999)
   130  		err = db.Update(InsertLastCompleteBlockHeightIfNotExists(height2))
   131  		require.NoError(t, err)
   132  
   133  		err = db.View(RetrieveLastCompleteBlockHeight(&retrieved))
   134  		require.NoError(t, err)
   135  
   136  		assert.Equal(t, retrieved, height1)
   137  	})
   138  }