added unit tests

squash me

squash me

squash
This commit is contained in:
rigelrozanski
2017-02-18 19:19:26 -05:00
parent 6a21ad5144
commit 78167b4e3a
11 changed files with 235 additions and 6 deletions
+3
View File
@@ -13,6 +13,9 @@ type Account struct {
}
func (acc *Account) Copy() *Account {
if acc == nil {
return nil
}
accCopy := *acc
return &accCopy
}
+28
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()
}
+5
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)
+1
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
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")
}
+1 -1
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
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")
}