Skip to content

Commit

Permalink
add test for util and store
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Aug 20, 2024
1 parent 0a49836 commit bd60f1c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
50 changes: 50 additions & 0 deletions indexer/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package indexer

import (
"testing"

"cosmossdk.io/store/dbadapter"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
)

func Test_StoreIO(t *testing.T) {
db := dbm.NewMemDB()
store := NewCacheStore(dbadapter.Store{DB: db}, 100)

key := []byte("key")
value := []byte("value")

// case 1. key not exists
ok, err := store.Has(key)
require.NoError(t, err)
require.False(t, ok)

bz, err := store.Get(key)
require.NoError(t, err)
require.Nil(t, bz)

// case 2. set key
err = store.Set(key, value)
require.NoError(t, err)

ok, err = store.Has(key)
require.NoError(t, err)
require.True(t, ok)

bz, err = store.Get(key)
require.NoError(t, err)
require.Equal(t, value, bz)

// case 3. delete key
err = store.Delete(key)
require.NoError(t, err)

ok, err = store.Has(key)
require.NoError(t, err)
require.False(t, ok)

bz, err = store.Get(key)
require.NoError(t, err)
require.Nil(t, bz)
}
22 changes: 22 additions & 0 deletions indexer/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ func Test_UnpackData(t *testing.T) {
require.NoError(t, err)
require.Equal(t, resp, respOut)
}

func Test_collJsonVal(t *testing.T) {
type s1 struct {
A string `json:"a"`
B uint64 `json:"b"`
}

codec := collJsonVal[s1]{}
bz, err := codec.Encode(s1{
A: "a",
B: 1,
})
require.NoError(t, err)
require.Equal(t, `{"a":"a","b":1}`, string(bz))

out, err := codec.Decode(bz)
require.NoError(t, err)
require.Equal(t, s1{
A: "a",
B: 1,
}, out)
}

0 comments on commit bd60f1c

Please sign in to comment.