Skip to content

Commit

Permalink
test: add test examples for prefixdb.go (#22752)
Browse files Browse the repository at this point in the history
Co-authored-by: heren-ke <[email protected]>
  • Loading branch information
xujiangyu and heren-ke authored Dec 7, 2024
1 parent dda47b5 commit 8ca8272
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions store/db/prefixdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package db_test

import (
"testing"

"cosmossdk.io/store/db"
"cosmossdk.io/store/mock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestPrefixDB(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockDB := mock.NewMockKVStoreWithBatch(mockCtrl)
prefix := []byte("test:")
pdb := db.NewPrefixDB(mockDB, prefix)

key := []byte("key1")
value := []byte("value1")
mockDB.EXPECT().Set(gomock.Eq(append(prefix, key...)), gomock.Eq(value)).Return(nil)

err := pdb.Set(key, value)
require.NoError(t, err)

mockDB.EXPECT().Get(gomock.Eq(append(prefix, key...))).Return(value, nil)

returnedValue, err := pdb.Get(key)
require.NoError(t, err)
require.Equal(t, value, returnedValue)

mockDB.EXPECT().Has(gomock.Eq(append(prefix, key...))).Return(true, nil)

has, err := pdb.Has(key)
require.NoError(t, err)
require.True(t, has)

mockDB.EXPECT().Delete(gomock.Eq(append(prefix, key...))).Return(nil)

err = pdb.Delete(key)
require.NoError(t, err)

mockDB.EXPECT().Has(gomock.Eq(append(prefix, key...))).Return(false, nil)

has, err = pdb.Has(key)
require.NoError(t, err)
require.False(t, has)
}

0 comments on commit 8ca8272

Please sign in to comment.