added unit tests

squash me

squash me

squash
This commit is contained in:
rigelrozanski 2017-02-16 21:36:49 -05:00
parent 6a21ad5144
commit 78167b4e3a
11 changed files with 235 additions and 6 deletions

View File

@ -1,12 +1,29 @@
package commands
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/basecoin/types"
)
func TestHex(t *testing.T) {
//test isHex
hexNoPrefix := hex.EncodeToString([]byte("foobar"))
hexWPrefix := "0x" + hexNoPrefix
str := "foobar"
strWPrefix := "0xfoobar"
assert.True(t, isHex(hexWPrefix), "isHex not identifying hex with 0x prefix")
assert.True(t, !isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix")
assert.True(t, !isHex(str), "isHex shouldn't identify non-hex string")
assert.True(t, !isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix")
//test strip hex
assert.True(t, StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters")
}
//Test the parse coin and parse coins functionality
func TestParse(t *testing.T) {

View File

@ -35,7 +35,7 @@ func New() *CounterPlugin {
return &CounterPlugin{}
}
func (cp *CounterPlugin) SetOption(store types.KVStore, key string, value string) (log string) {
func (cp *CounterPlugin) SetOption(store types.KVStore, key, value string) (log string) {
return ""
}

View File

@ -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 is store is merkleeyes")
}
}
//----------------------------------------

68
state/state_test.go Normal file
View File

@ -0,0 +1,68 @@
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) {
s := NewState(types.NewMemKVStore())
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
dumAddr := []byte("dummyAddress")
acc := &types.Account{
PubKey: nil,
Sequence: 1,
Balance: nil,
}
s.SetAccount(dumAddr, acc)
assert.True(t, s.GetAccount(dumAddr).Sequence == 1, "GetAccount not retrieving")
//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")
//Test Commit on state with non-merkle store
assert.True(t, !s.Commit().IsOK(), "Commit shouldn't work with non-merkle store")
//Test CacheWrap with merkleeyes client store
eyesCli := eyes.NewLocalClient("", 0)
s = NewState(eyesCli)
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")
}

View File

@ -13,6 +13,9 @@ type Account struct {
}
func (acc *Account) Copy() *Account {
if acc == nil {
return nil
}
accCopy := *acc
return &accCopy
}

28
types/account_test.go Normal file
View File

@ -0,0 +1,28 @@
package types
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAccount(t *testing.T) {
acc := Account{
PubKey: nil,
Sequence: 0,
Balance: nil,
}
//test Copy
accCopy := acc.Copy()
accCopy.Sequence = 1
t.Log(acc.Sequence)
t.Log(accCopy.Sequence)
assert.True(t, acc.Sequence != accCopy.Sequence, "Account Copy Error")
//test sending nils for panic
var nilAcc *Account
nilAcc.String()
nilAcc.Copy()
}

View File

@ -19,6 +19,11 @@ func TestCoins(t *testing.T) {
t.Fatalf("Expected coins to be positive: %v", coins)
}
emptyCoins := Coins{Coin{"GOLD", 0}}
if !coins.IsGTE(emptyCoins) {
t.Fatalf("Expected %v to be >= %v", coins, emptyCoins)
}
negCoins := coins.Negative()
if negCoins.IsPositive() {
t.Fatalf("Expected neg coins to not be positive: %v", negCoins)

View File

@ -113,6 +113,7 @@ func (kvc *KVCache) Get(key []byte) (value []byte) {
}
}
//Update the store with the values from the cache
func (kvc *KVCache) Sync() {
for e := kvc.keys.Front(); e != nil; e = e.Next() {
key := e.Value.([]byte)

59
types/kvstore_test.go Normal file
View File

@ -0,0 +1,59 @@
package types
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMemKVStore(t *testing.T) {
ms := NewMemKVStore()
ms.Set([]byte("foo"), []byte("snake"))
ms.Set([]byte("bar"), []byte("mouse"))
assert.True(t, bytes.Equal(ms.Get([]byte("foo")), []byte("snake")), "MemKVStore doesn't retrieve after Set")
assert.True(t, bytes.Equal(ms.Get([]byte("bar")), []byte("mouse")), "MemKVStore doesn't retrieve after Set")
}
func TestKVCache(t *testing.T) {
store := NewMemKVStore()
kvc := NewKVCache(store)
setRecords := func() {
kvc.Set([]byte("foo"), []byte("snake"))
kvc.Set([]byte("bar"), []byte("mouse"))
}
//test read/write
setRecords()
assert.True(t, bytes.Equal(kvc.Get([]byte("foo")), []byte("snake")), "KVCache doesn't retrieve after Set")
assert.True(t, bytes.Equal(kvc.Get([]byte("bar")), []byte("mouse")), "KVCache doesn't retrieve after Set")
//test reset
kvc.Reset()
assert.True(t, !bytes.Equal(kvc.Get([]byte("foo")), []byte("snake")), "KVCache retrieving after reset")
assert.True(t, !bytes.Equal(kvc.Get([]byte("bar")), []byte("mouse")), "KVCache retrieving after reset")
//test sync
setRecords()
assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before synced")
assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before synced")
kvc.Sync()
assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store isn't retrieving after synced")
assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store isn't retrieving after synced")
//test logging
assert.True(t, len(kvc.GetLogLines()) == 0, "logging events existed before using SetLogging")
fmt.Println(len(kvc.GetLogLines()))
kvc.SetLogging()
setRecords()
assert.True(t, len(kvc.GetLogLines()) == 2, "incorrect number of logging events recorded")
kvc.ClearLogLines()
assert.True(t, len(kvc.GetLogLines()) == 0, "logging events still exists after ClearLogLines")
}

View File

@ -15,7 +15,7 @@ type Plugin interface {
RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result)
// Other ABCI message handlers
SetOption(store KVStore, key string, value string) (log string)
SetOption(store KVStore, key, value string) (log string)
InitChain(store KVStore, vals []*abci.Validator)
BeginBlock(store KVStore, hash []byte, header *abci.Header)
EndBlock(store KVStore, height uint64) abci.ResponseEndBlock

42
types/plugin_test.go Normal file
View File

@ -0,0 +1,42 @@
package types
import (
"testing"
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
)
//----------------------------------
type Dummy struct{}
func (d *Dummy) Name() string {
return "dummy"
}
func (d *Dummy) RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result) {
return
}
func (d *Dummy) SetOption(storei KVStore, key, value string) (log string) {
return ""
}
func (d *Dummy) InitChain(store KVStore, vals []*abci.Validator) {
}
func (d *Dummy) BeginBlock(store KVStore, hash []byte, header *abci.Header) {
}
func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBlock) {
return
}
//----------------------------------
func TestPlugin(t *testing.T) {
plugins := NewPlugins()
assert.True(t, len(plugins.GetList()) == 0, "plugins object init with a objects")
plugins.RegisterPlugin(&Dummy{})
assert.True(t, len(plugins.GetList()) == 1, "plugin wasn't added to plist after registered")
dum := plugins.GetByName("dummy")
assert.True(t, dum.Name() == "dummy", "plugin wasn't retrieved properly with GetByName")
}