Merge branch 'unitTests' into develop
This commit is contained in:
+10
-4
@@ -38,7 +38,7 @@ func (s *State) GetChainID() string {
|
||||
}
|
||||
|
||||
func (s *State) Get(key []byte) (value []byte) {
|
||||
if s.readCache != nil {
|
||||
if s.readCache != nil { //if not a cachewrap
|
||||
value, ok := s.readCache[string(key)]
|
||||
if ok {
|
||||
return value
|
||||
@@ -48,7 +48,7 @@ func (s *State) Get(key []byte) (value []byte) {
|
||||
}
|
||||
|
||||
func (s *State) Set(key []byte, value []byte) {
|
||||
if s.readCache != nil {
|
||||
if s.readCache != nil { //if not a cachewrap
|
||||
s.readCache[string(key)] = value
|
||||
}
|
||||
s.store.Set(key, value)
|
||||
@@ -78,8 +78,14 @@ func (s *State) CacheSync() {
|
||||
}
|
||||
|
||||
func (s *State) Commit() abci.Result {
|
||||
s.readCache = make(map[string][]byte)
|
||||
return s.store.(*eyes.Client).CommitSync()
|
||||
switch s.store.(type) {
|
||||
case *eyes.Client:
|
||||
s.readCache = make(map[string][]byte)
|
||||
return s.store.(*eyes.Client).CommitSync()
|
||||
default:
|
||||
return abci.NewError(abci.CodeType_InternalError, "can only use Commit if store is merkleeyes")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/tendermint/basecoin/types"
|
||||
eyes "github.com/tendermint/merkleeyes/client"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestState(t *testing.T) {
|
||||
|
||||
//States and Stores for tests
|
||||
store := types.NewMemKVStore()
|
||||
state := NewState(store)
|
||||
cache := state.CacheWrap()
|
||||
eyesCli := eyes.NewLocalClient("", 0)
|
||||
|
||||
//Account and address for tests
|
||||
dumAddr := []byte("dummyAddress")
|
||||
|
||||
acc := &types.Account{
|
||||
PubKey: nil,
|
||||
Sequence: 1,
|
||||
Balance: nil,
|
||||
}
|
||||
|
||||
//reset the store/state/cache
|
||||
reset := func() {
|
||||
store = types.NewMemKVStore()
|
||||
state = NewState(store)
|
||||
cache = state.CacheWrap()
|
||||
}
|
||||
|
||||
//set the state to using the eyesCli instead of MemKVStore
|
||||
useEyesCli := func() {
|
||||
state = NewState(eyesCli)
|
||||
cache = state.CacheWrap()
|
||||
}
|
||||
|
||||
//key value pairs to be tested within the system
|
||||
keyvalue := []struct {
|
||||
key string
|
||||
value string
|
||||
}{
|
||||
{"foo", "snake"},
|
||||
{"bar", "mouse"},
|
||||
}
|
||||
|
||||
//set the kvc to have all the key value pairs
|
||||
setRecords := func(kv types.KVStore) {
|
||||
for _, n := range keyvalue {
|
||||
kv.Set([]byte(n.key), []byte(n.value))
|
||||
}
|
||||
}
|
||||
|
||||
//store has all the key value pairs
|
||||
storeHasAll := func(kv types.KVStore) bool {
|
||||
for _, n := range keyvalue {
|
||||
if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//define the test list
|
||||
testList := []struct {
|
||||
testPass func() bool
|
||||
errMsg string
|
||||
}{
|
||||
//test chainID
|
||||
{func() bool { state.SetChainID("testchain"); return state.GetChainID() == "testchain" },
|
||||
"ChainID is improperly stored"},
|
||||
|
||||
//test basic retrieve
|
||||
{func() bool { setRecords(state); return storeHasAll(state) },
|
||||
"state doesn't retrieve after Set"},
|
||||
|
||||
// Test account retrieve
|
||||
{func() bool { state.SetAccount(dumAddr, acc); return state.GetAccount(dumAddr).Sequence == 1 },
|
||||
"GetAccount not retrieving"},
|
||||
|
||||
//Test CacheWrap with local mem store
|
||||
{func() bool { reset(); setRecords(cache); return !storeHasAll(store) },
|
||||
"store retrieving before CacheSync"},
|
||||
{func() bool { cache.CacheSync(); return storeHasAll(store) },
|
||||
"store doesn't retrieve after CacheSync"},
|
||||
|
||||
//Test Commit on state with non-merkle store
|
||||
{func() bool { return !state.Commit().IsOK() },
|
||||
"Commit shouldn't work with non-merkle store"},
|
||||
|
||||
//Test CacheWrap with merkleeyes client store
|
||||
{func() bool { useEyesCli(); setRecords(cache); return !storeHasAll(eyesCli) },
|
||||
"eyesCli retrieving before Commit"},
|
||||
{func() bool { cache.CacheSync(); return state.Commit().IsOK() },
|
||||
"Bad Commit"},
|
||||
{func() bool { return storeHasAll(eyesCli) },
|
||||
"eyesCli doesn't retrieve after Commit"},
|
||||
}
|
||||
|
||||
//execute the tests
|
||||
for _, tl := range testList {
|
||||
assert.True(t, tl.testPass(), tl.errMsg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user