101 lines
2.2 KiB
Go
101 lines
2.2 KiB
Go
package types_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
"github.com/cosmos/cosmos-sdk/types"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
)
|
|
|
|
type MockLogger struct {
|
|
logs *[]string
|
|
}
|
|
|
|
func NewMockLogger() MockLogger {
|
|
logs := make([]string, 0)
|
|
return MockLogger{
|
|
&logs,
|
|
}
|
|
}
|
|
|
|
func (l MockLogger) Debug(msg string, kvs ...interface{}) {
|
|
*l.logs = append(*l.logs, msg)
|
|
}
|
|
|
|
func (l MockLogger) Info(msg string, kvs ...interface{}) {
|
|
*l.logs = append(*l.logs, msg)
|
|
}
|
|
|
|
func (l MockLogger) Error(msg string, kvs ...interface{}) {
|
|
*l.logs = append(*l.logs, msg)
|
|
}
|
|
|
|
func (l MockLogger) With(kvs ...interface{}) log.Logger {
|
|
panic("not implemented")
|
|
}
|
|
|
|
func TestContextGetOpShouldNeverPanic(t *testing.T) {
|
|
var ms types.MultiStore
|
|
ctx := types.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
|
|
indices := []int64{
|
|
-10, 1, 0, 10, 20,
|
|
}
|
|
|
|
for _, index := range indices {
|
|
_, _ = ctx.GetOp(index)
|
|
}
|
|
}
|
|
|
|
func defaultContext(key types.StoreKey) types.Context {
|
|
db := dbm.NewMemDB()
|
|
cms := store.NewCommitMultiStore(db)
|
|
cms.MountStoreWithDB(key, types.StoreTypeIAVL, db)
|
|
cms.LoadLatestVersion()
|
|
ctx := types.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
|
|
return ctx
|
|
}
|
|
|
|
func TestCacheContext(t *testing.T) {
|
|
key := types.NewKVStoreKey(t.Name())
|
|
k1 := []byte("hello")
|
|
v1 := []byte("world")
|
|
k2 := []byte("key")
|
|
v2 := []byte("value")
|
|
|
|
ctx := defaultContext(key)
|
|
store := ctx.KVStore(key)
|
|
store.Set(k1, v1)
|
|
require.Equal(t, v1, store.Get(k1))
|
|
require.Nil(t, store.Get(k2))
|
|
|
|
cctx, write := ctx.CacheContext()
|
|
cstore := cctx.KVStore(key)
|
|
require.Equal(t, v1, cstore.Get(k1))
|
|
require.Nil(t, cstore.Get(k2))
|
|
|
|
cstore.Set(k2, v2)
|
|
require.Equal(t, v2, cstore.Get(k2))
|
|
require.Nil(t, store.Get(k2))
|
|
|
|
write()
|
|
|
|
require.Equal(t, v2, store.Get(k2))
|
|
}
|
|
|
|
func TestLogContext(t *testing.T) {
|
|
key := types.NewKVStoreKey(t.Name())
|
|
ctx := defaultContext(key)
|
|
logger := NewMockLogger()
|
|
ctx = ctx.WithLogger(logger)
|
|
ctx.Logger().Debug("debug")
|
|
ctx.Logger().Info("info")
|
|
ctx.Logger().Error("error")
|
|
require.Equal(t, *logger.logs, []string{"debug", "info", "error"})
|
|
}
|