From 8ca82729fafde0fbdad7de6e6d18c76f7584bb93 Mon Sep 17 00:00:00 2001 From: xujiangyu <47973791+xujiangyu@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:53:05 +0800 Subject: [PATCH] test: add test examples for prefixdb.go (#22752) Co-authored-by: heren-ke --- store/db/prefixdb_test.go | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 store/db/prefixdb_test.go diff --git a/store/db/prefixdb_test.go b/store/db/prefixdb_test.go new file mode 100644 index 00000000000..199aa50d056 --- /dev/null +++ b/store/db/prefixdb_test.go @@ -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) +}