diff --git a/indexer/store_test.go b/indexer/store_test.go new file mode 100644 index 0000000..8b823c4 --- /dev/null +++ b/indexer/store_test.go @@ -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) +} diff --git a/indexer/utils_test.go b/indexer/utils_test.go index b96e9a8..4fd80c9 100644 --- a/indexer/utils_test.go +++ b/indexer/utils_test.go @@ -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) +}