tests cleanup

cleanup
This commit is contained in:
rigelrozanski
2017-04-13 23:31:52 -04:00
committed by Rigel Rozanski
parent 16ff0ccf4f
commit 0720a03dae
12 changed files with 562 additions and 625 deletions
+4 -2
View File
@@ -1,6 +1,7 @@
package types
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@@ -12,8 +13,9 @@ func TestNilAccount(t *testing.T) {
//test Copy
accCopy := acc.Copy()
assert.True(t, &acc != accCopy, "Account Copy Error")
assert.True(t, acc.Sequence == accCopy.Sequence)
//note that the assert.True is used instead of assert.Equal because looking at pointers
assert.True(t, &acc != accCopy, fmt.Sprintf("Account Copy Error, acc1: %v, acc2: %v", &acc, accCopy))
assert.Equal(t, acc.Sequence, accCopy.Sequence)
//test sending nils for panic
var nilAcc *Account
+11 -21
View File
@@ -1,14 +1,14 @@
package types
import (
"fmt"
"testing"
cmn "github.com/tendermint/go-common"
"github.com/stretchr/testify/assert"
)
func TestCoins(t *testing.T) {
assert := assert.New(t)
//Define the coins to be used in tests
good := Coins{
@@ -42,24 +42,14 @@ func TestCoins(t *testing.T) {
Coin{"MINERAL", 1},
}
//define the list of coin tests
var testList = []struct {
testPass bool
errMsg string
}{
{good.IsValid(), "Coins are valid"},
{good.IsPositive(), cmn.Fmt("Expected coins to be positive: %v", good)},
{good.IsGTE(empty), cmn.Fmt("Expected %v to be >= %v", good, empty)},
{!neg.IsPositive(), cmn.Fmt("Expected neg coins to not be positive: %v", neg)},
{len(sum) == 0, "Expected 0 coins"},
{!badSort1.IsValid(), "Coins are not sorted"},
{!badSort2.IsValid(), "Coins are not sorted"},
{!badAmt.IsValid(), "Coins cannot include 0 amounts"},
{!dup.IsValid(), "Duplicate coin"},
}
assert.True(good.IsValid(), "Coins are valid")
assert.True(good.IsPositive(), fmt.Sprintf("Expected coins to be positive: %v", good))
assert.True(good.IsGTE(empty), fmt.Sprintf("Expected %v to be >= %v", good, empty))
assert.False(neg.IsPositive(), fmt.Sprintf("Expected neg coins to not be positive: %v", neg))
assert.Zero(len(sum), "Expected 0 coins")
assert.False(badSort1.IsValid(), "Coins are not sorted")
assert.False(badSort2.IsValid(), "Coins are not sorted")
assert.False(badAmt.IsValid(), "Coins cannot include 0 amounts")
assert.False(dup.IsValid(), "Duplicate coin")
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass, tl.errMsg)
}
}
+22 -31
View File
@@ -8,6 +8,7 @@ import (
)
func TestKVStore(t *testing.T) {
assert := assert.New(t)
//stores to be tested
ms := NewMemKVStore()
@@ -40,40 +41,30 @@ func TestKVStore(t *testing.T) {
return true
}
//define the test list
var testList = []struct {
testPass func() bool
errMsg string
}{
//test read/write for MemKVStore
{func() bool { setRecords(ms); return storeHasAll(ms) },
"MemKVStore doesn't retrieve after Set"},
//test read/write for MemKVStore
setRecords(ms)
assert.True(storeHasAll(ms), "MemKVStore doesn't retrieve after Set")
//test read/write for KVCache
{func() bool { setRecords(kvc); return storeHasAll(kvc) },
"KVCache doesn't retrieve after Set"},
//test read/write for KVCache
setRecords(kvc)
assert.True(storeHasAll(kvc), "KVCache doesn't retrieve after Set")
//test reset
{func() bool { kvc.Reset(); return !storeHasAll(kvc) },
"KVCache retrieving after reset"},
//test reset
kvc.Reset()
assert.False(storeHasAll(kvc), "KVCache retrieving after reset")
//test sync
{func() bool { setRecords(kvc); return !storeHasAll(store) },
"store retrieving before synced"},
{func() bool { kvc.Sync(); return storeHasAll(store) },
"store isn't retrieving after synced"},
//test sync
setRecords(kvc)
assert.False(storeHasAll(store), "store retrieving before synced")
kvc.Sync()
assert.True(storeHasAll(store), "store isn't retrieving after synced")
//test logging
{func() bool { return len(kvc.GetLogLines()) == 0 },
"logging events existed before using SetLogging"},
{func() bool { kvc.SetLogging(); setRecords(kvc); return len(kvc.GetLogLines()) == 2 },
"incorrect number of logging events recorded"},
{func() bool { kvc.ClearLogLines(); return len(kvc.GetLogLines()) == 0 },
"logging events still exists after ClearLogLines"},
}
//test logging
assert.Zero(len(kvc.GetLogLines()), "logging events existed before using SetLogging")
kvc.SetLogging()
setRecords(kvc)
assert.Equal(len(kvc.GetLogLines()), 2, "incorrect number of logging events recorded")
kvc.ClearLogLines()
assert.Zero(len(kvc.GetLogLines()), "logging events still exists after ClearLogLines")
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass(), tl.errMsg)
}
}
+5 -20
View File
@@ -32,25 +32,10 @@ func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBloc
//----------------------------------
func TestPlugin(t *testing.T) {
assert := assert.New(t)
plugins := NewPlugins()
//define the test list
var testList = []struct {
testPass func() bool
errMsg string
}{
{func() bool { return (len(plugins.GetList()) == 0) },
"plugins object init with a objects"},
{func() bool { plugins.RegisterPlugin(&Dummy{}); return (len(plugins.GetList()) == 1) },
"plugin wasn't added to plist after registered"},
{func() bool { return (plugins.GetByName("dummy").Name() == "dummy") },
"plugin wasn't retrieved properly with GetByName"},
}
//execute the tests
for _, tl := range testList {
assert.True(t, tl.testPass(), tl.errMsg)
}
assert.Zero(len(plugins.GetList()), "plugins object init with a objects")
plugins.RegisterPlugin(&Dummy{})
assert.Equal(len(plugins.GetList()), 1, "plugin wasn't added to plist after registered")
assert.Equal(plugins.GetByName("dummy").Name(), "dummy", "plugin wasn't retrieved properly with GetByName")
}
+20 -18
View File
@@ -1,11 +1,11 @@
package types
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-data"
)
@@ -40,11 +40,11 @@ func TestSendTxSignable(t *testing.T) {
},
}
signBytes := sendTx.SignBytes(chainID)
signBytesHex := cmn.Fmt("%X", signBytes)
signBytesHex := fmt.Sprintf("%X", signBytes)
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F01020106696E7075743101010000000000000030390301093200000106696E70757432010100000000000000006F01DE0000010201076F757470757431010100000000000000014D01076F75747075743201010000000000000001BC"
assert.True(t, signBytesHex == expected,
cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
assert.Equal(t, signBytesHex, expected,
fmt.Sprintf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
}
func TestAppTxSignable(t *testing.T) {
@@ -60,14 +60,16 @@ func TestAppTxSignable(t *testing.T) {
Data: []byte("data1"),
}
signBytes := callTx.SignBytes(chainID)
signBytesHex := cmn.Fmt("%X", signBytes)
signBytesHex := fmt.Sprintf("%X", signBytes)
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131"
assert.True(t, signBytesHex == expected,
cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
assert.Equal(t, signBytesHex, expected,
fmt.Sprintf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
}
func TestSendTxJSON(t *testing.T) {
assert, require := assert.New(t), require.New(t)
chainID := "test_chain_id"
test1PrivAcc := PrivAccountFromSecret("sendtx1")
test2PrivAcc := PrivAccountFromSecret("sendtx2")
@@ -89,37 +91,37 @@ func TestSendTxJSON(t *testing.T) {
// serialize this as json and back
js, err := data.ToJSON(TxS{tx})
require.Nil(t, err)
require.Nil(err)
// fmt.Println(string(js))
txs := TxS{}
err = data.FromJSON(js, &txs)
require.Nil(t, err)
require.Nil(err)
tx2, ok := txs.Tx.(*SendTx)
require.True(t, ok)
require.True(ok)
// make sure they are the same!
signBytes := tx.SignBytes(chainID)
signBytes2 := tx2.SignBytes(chainID)
assert.Equal(t, signBytes, signBytes2)
assert.Equal(t, tx, tx2)
assert.Equal(signBytes, signBytes2)
assert.Equal(tx, tx2)
// sign this thing
sig := test1PrivAcc.Sign(signBytes)
// we handle both raw sig and wrapped sig the same
tx.SetSignature(test1PrivAcc.PubKey.Address(), sig)
tx2.SetSignature(test1PrivAcc.PubKey.Address(), crypto.SignatureS{sig})
assert.Equal(t, tx, tx2)
assert.Equal(tx, tx2)
// let's marshal / unmarshal this with signature
js, err = data.ToJSON(TxS{tx})
require.Nil(t, err)
require.Nil(err)
// fmt.Println(string(js))
err = data.FromJSON(js, &txs)
require.Nil(t, err)
require.Nil(err)
tx2, ok = txs.Tx.(*SendTx)
require.True(t, ok)
require.True(ok)
// and make sure the sig is preserved
assert.Equal(t, tx, tx2)
assert.False(t, tx2.Inputs[0].Signature.Empty())
assert.Equal(tx, tx2)
assert.False(tx2.Inputs[0].Signature.Empty())
}