table driven testing

squash
This commit is contained in:
rigelrozanski
2017-02-22 18:17:29 -05:00
parent 5c3550acce
commit e6579cf9e9
7 changed files with 246 additions and 159 deletions
+80 -39
View File
@@ -12,21 +12,13 @@ import (
func TestState(t *testing.T) {
s := NewState(types.NewMemKVStore())
//States and Stores for tests
store := types.NewMemKVStore()
state := NewState(store)
cache := state.CacheWrap()
eyesCli := eyes.NewLocalClient("", 0)
s.SetChainID("testchain")
assert.True(t, s.GetChainID() == "testchain", "ChainID is improperly stored")
setRecords := func(kv types.KVStore) {
kv.Set([]byte("foo"), []byte("snake"))
kv.Set([]byte("bar"), []byte("mouse"))
}
setRecords(s)
assert.True(t, bytes.Equal(s.Get([]byte("foo")), []byte("snake")), "state doesn't retrieve after Set")
assert.True(t, bytes.Equal(s.Get([]byte("bar")), []byte("mouse")), "state doesn't retrieve after Set")
// Test account retrieve
//Account and address for tests
dumAddr := []byte("dummyAddress")
acc := &types.Account{
@@ -35,34 +27,83 @@ func TestState(t *testing.T) {
Balance: nil,
}
s.SetAccount(dumAddr, acc)
assert.True(t, s.GetAccount(dumAddr).Sequence == 1, "GetAccount not retrieving")
//reset the store/state/cache
reset := func() {
store = types.NewMemKVStore()
state = NewState(store)
cache = state.CacheWrap()
}
//Test CacheWrap with local mem store
store := types.NewMemKVStore()
s = NewState(store)
cache := s.CacheWrap()
setRecords(cache)
assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit")
assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit")
cache.CacheSync()
assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit")
assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit")
//set the state to using the eyesCli instead of MemKVStore
useEyesCli := func() {
state = NewState(eyesCli)
cache = state.CacheWrap()
}
//Test Commit on state with non-merkle store
assert.True(t, !s.Commit().IsOK(), "Commit shouldn't work with non-merkle store")
//key value pairs to be tested within the system
keyvalue := []struct {
key string
value string
}{
{"foo", "snake"},
{"bar", "mouse"},
}
//Test CacheWrap with merkleeyes client store
eyesCli := eyes.NewLocalClient("", 0)
s = NewState(eyesCli)
//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))
}
}
cache = s.CacheWrap()
setRecords(cache)
assert.True(t, !bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit")
assert.True(t, !bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit")
cache.CacheSync()
assert.True(t, s.Commit().IsOK(), "Bad Commit")
assert.True(t, bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit")
assert.True(t, bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit")
//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)
}
}