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

     1  package operation_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/storage/badger/operation"
    10  	"github.com/onflow/flow-go/utils/unittest"
    11  )
    12  
    13  func TestInsertRetrieveDBTypeMarker(t *testing.T) {
    14  	t.Run("should insert and ensure type marker", func(t *testing.T) {
    15  		t.Run("public", func(t *testing.T) {
    16  			unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    17  
    18  				// can insert db marker to empty DB
    19  				err := db.Update(operation.InsertPublicDBMarker)
    20  				require.NoError(t, err)
    21  				// can insert db marker twice
    22  				err = db.Update(operation.InsertPublicDBMarker)
    23  				require.NoError(t, err)
    24  				// ensure correct db type succeeds
    25  				err = operation.EnsurePublicDB(db)
    26  				require.NoError(t, err)
    27  				// ensure other db type fails
    28  				err = operation.EnsureSecretDB(db)
    29  				require.Error(t, err)
    30  			})
    31  		})
    32  
    33  		t.Run("secret", func(t *testing.T) {
    34  			unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    35  
    36  				// can insert db marker to empty DB
    37  				err := db.Update(operation.InsertSecretDBMarker)
    38  				require.NoError(t, err)
    39  				// can insert db marker twice
    40  				err = db.Update(operation.InsertSecretDBMarker)
    41  				require.NoError(t, err)
    42  				// ensure correct db type succeeds
    43  				err = operation.EnsureSecretDB(db)
    44  				require.NoError(t, err)
    45  				// ensure other db type fails
    46  				err = operation.EnsurePublicDB(db)
    47  				require.Error(t, err)
    48  			})
    49  		})
    50  	})
    51  
    52  	t.Run("should fail to insert different db marker to non-empty db", func(t *testing.T) {
    53  		t.Run("public", func(t *testing.T) {
    54  			unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    55  
    56  				// can insert db marker to empty DB
    57  				err := db.Update(operation.InsertPublicDBMarker)
    58  				require.NoError(t, err)
    59  				// inserting a different marker should fail
    60  				err = db.Update(operation.InsertSecretDBMarker)
    61  				require.Error(t, err)
    62  			})
    63  		})
    64  		t.Run("secret", func(t *testing.T) {
    65  			unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    66  
    67  				// can insert db marker to empty DB
    68  				err := db.Update(operation.InsertSecretDBMarker)
    69  				require.NoError(t, err)
    70  				// inserting a different marker should fail
    71  				err = db.Update(operation.InsertPublicDBMarker)
    72  				require.Error(t, err)
    73  			})
    74  		})
    75  	})
    76  }