From 78167b4e3ac7358fa9bb45fec512f42f350b1f20 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 16 Feb 2017 21:36:49 -0500 Subject: [PATCH 1/4] added unit tests squash me squash me squash --- cmd/commands/utils_test.go | 17 ++++++++++ plugins/counter/counter.go | 2 +- state/state.go | 14 +++++--- state/state_test.go | 68 ++++++++++++++++++++++++++++++++++++++ types/account.go | 3 ++ types/account_test.go | 28 ++++++++++++++++ types/coin_test.go | 5 +++ types/kvstore.go | 1 + types/kvstore_test.go | 59 +++++++++++++++++++++++++++++++++ types/plugin.go | 2 +- types/plugin_test.go | 42 +++++++++++++++++++++++ 11 files changed, 235 insertions(+), 6 deletions(-) create mode 100644 state/state_test.go create mode 100644 types/account_test.go create mode 100644 types/kvstore_test.go create mode 100644 types/plugin_test.go diff --git a/cmd/commands/utils_test.go b/cmd/commands/utils_test.go index c530b04a14..012431e35f 100644 --- a/cmd/commands/utils_test.go +++ b/cmd/commands/utils_test.go @@ -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) { diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 9d849d1d05..79694c4052 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -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 "" } diff --git a/state/state.go b/state/state.go index 026c6a9ad9..385c17dc9b 100644 --- a/state/state.go +++ b/state/state.go @@ -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") + } + } //---------------------------------------- diff --git a/state/state_test.go b/state/state_test.go new file mode 100644 index 0000000000..f1feac492e --- /dev/null +++ b/state/state_test.go @@ -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") + +} diff --git a/types/account.go b/types/account.go index d78f560a6e..d1e62d8321 100644 --- a/types/account.go +++ b/types/account.go @@ -13,6 +13,9 @@ type Account struct { } func (acc *Account) Copy() *Account { + if acc == nil { + return nil + } accCopy := *acc return &accCopy } diff --git a/types/account_test.go b/types/account_test.go new file mode 100644 index 0000000000..771e7f07c6 --- /dev/null +++ b/types/account_test.go @@ -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() +} diff --git a/types/coin_test.go b/types/coin_test.go index 158f159c50..7a3fc2ecdd 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -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) diff --git a/types/kvstore.go b/types/kvstore.go index 96f8c7d0ff..15088bdfb3 100644 --- a/types/kvstore.go +++ b/types/kvstore.go @@ -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) diff --git a/types/kvstore_test.go b/types/kvstore_test.go new file mode 100644 index 0000000000..5c40a2597b --- /dev/null +++ b/types/kvstore_test.go @@ -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") + +} diff --git a/types/plugin.go b/types/plugin.go index ac95cac679..9783f2c7c4 100644 --- a/types/plugin.go +++ b/types/plugin.go @@ -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 diff --git a/types/plugin_test.go b/types/plugin_test.go new file mode 100644 index 0000000000..21cd92114d --- /dev/null +++ b/types/plugin_test.go @@ -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") +} From 5c3550acce082871921202944af78d605761740d Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 19 Feb 2017 14:22:38 -0500 Subject: [PATCH 2/4] fix typo --- state/state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state/state.go b/state/state.go index 385c17dc9b..7fb6c48bac 100644 --- a/state/state.go +++ b/state/state.go @@ -83,7 +83,7 @@ func (s *State) Commit() abci.Result { 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") + return abci.NewError(abci.CodeType_InternalError, "can only use Commit if store is merkleeyes") } } From e6579cf9e9e9f9b0ae280eece5ca1d022b868f9c Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 22 Feb 2017 17:30:50 -0500 Subject: [PATCH 3/4] table driven testing squash --- cmd/commands/utils_test.go | 55 ++++++++++++----- state/state_test.go | 119 +++++++++++++++++++++++++------------ types/account_test.go | 5 +- types/coin_test.go | 88 +++++++++++---------------- types/kvstore_test.go | 92 +++++++++++++++++----------- types/plugin_test.go | 26 +++++++- types/tx_test.go | 20 ++++--- 7 files changed, 246 insertions(+), 159 deletions(-) diff --git a/cmd/commands/utils_test.go b/cmd/commands/utils_test.go index 012431e35f..f16927d486 100644 --- a/cmd/commands/utils_test.go +++ b/cmd/commands/utils_test.go @@ -15,13 +15,24 @@ func TestHex(t *testing.T) { 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") + //define the list of coin tests + var testList = []struct { + testPass bool + errMsg string + }{ + {isHex(hexWPrefix), "isHex not identifying hex with 0x prefix"}, + {!isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix"}, + {!isHex(str), "isHex shouldn't identify non-hex string"}, + {!isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix"}, + {StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass, tl.errMsg) + } + } //Test the parse coin and parse coins functionality @@ -43,15 +54,27 @@ func TestParse(t *testing.T) { return coin } - //testing ParseCoin Function - assert.True(t, types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin") - assert.True(t, types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins") - assert.True(t, types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins") + //define the list of coin tests + var testList = []struct { + testPass bool + errMsg string + }{ + //testing ParseCoin Function + {types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin"}, + {types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins"}, + {types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins"}, - //testing ParseCoins Function - assert.True(t, types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), "parseCoins doesn't parse a single coin") - assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")), - "parseCoins doesn't properly parse two coins") - assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")), - "parseCoins doesn't properly parse two coins which use spaces") + //testing ParseCoins Function + {types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), + "parseCoins doesn't parse a single coin"}, + {types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")), + "parseCoins doesn't properly parse two coins"}, + {types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")), + "parseCoins doesn't properly parse two coins which use spaces"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass, tl.errMsg) + } } diff --git a/state/state_test.go b/state/state_test.go index f1feac492e..162b8e9f76 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -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) + } } diff --git a/types/account_test.go b/types/account_test.go index 771e7f07c6..2f4d49260f 100644 --- a/types/account_test.go +++ b/types/account_test.go @@ -16,10 +16,7 @@ func TestAccount(t *testing.T) { //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") + assert.True(t, &acc != accCopy, "Account Copy Error") //test sending nils for panic var nilAcc *Account diff --git a/types/coin_test.go b/types/coin_test.go index 7a3fc2ecdd..36767ae6b9 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -2,84 +2,64 @@ package types import ( "testing" + + cmn "github.com/tendermint/go-common" + + "github.com/stretchr/testify/assert" ) func TestCoins(t *testing.T) { - coins := Coins{ + + //Define the coins to be used in tests + good := Coins{ Coin{"GAS", 1}, Coin{"MINERAL", 1}, Coin{"TREE", 1}, } - - if !coins.IsValid() { - t.Fatal("Coins are valid") + neg := good.Negative() + sum := good.Plus(neg) + empty := Coins{ + Coin{"GOLD", 0}, } - - if !coins.IsPositive() { - 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) - } - - sumCoins := coins.Plus(negCoins) - if len(sumCoins) != 0 { - t.Fatal("Expected 0 coins") - } -} - -func TestCoinsBadSort(t *testing.T) { - coins := Coins{ + badSort1 := Coins{ Coin{"TREE", 1}, Coin{"GAS", 1}, Coin{"MINERAL", 1}, } - - if coins.IsValid() { - t.Fatal("Coins are not sorted") - } -} - -func TestCoinsBadSort2(t *testing.T) { - // both are after the first one, but the second and third are in the wrong order - coins := Coins{ + badSort2 := Coins{ // both are after the first one, but the second and third are in the wrong order Coin{"GAS", 1}, Coin{"TREE", 1}, Coin{"MINERAL", 1}, } - - if coins.IsValid() { - t.Fatal("Coins are not sorted") - } -} - -func TestCoinsBadAmount(t *testing.T) { - coins := Coins{ + badAmt := Coins{ Coin{"GAS", 1}, Coin{"TREE", 0}, Coin{"MINERAL", 1}, } - - if coins.IsValid() { - t.Fatal("Coins cannot include 0 amounts") - } -} - -func TestCoinsDuplicate(t *testing.T) { - coins := Coins{ + dup := Coins{ Coin{"GAS", 1}, Coin{"GAS", 1}, Coin{"MINERAL", 1}, } - if coins.IsValid() { - t.Fatal("Duplicate coin") + //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"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass, tl.errMsg) } } diff --git a/types/kvstore_test.go b/types/kvstore_test.go index 5c40a2597b..76ba009f30 100644 --- a/types/kvstore_test.go +++ b/types/kvstore_test.go @@ -2,58 +2,78 @@ package types import ( "bytes" - "fmt" "testing" "github.com/stretchr/testify/assert" ) -func TestMemKVStore(t *testing.T) { +func TestKVStore(t *testing.T) { + //stores to be tested 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")) + //key value pairs to be tested within the system + var keyvalue = []struct { + key string + value string + }{ + {"foo", "snake"}, + {"bar", "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") + //set the kvc to have all the key value pairs + setRecords := func(kv KVStore) { + for _, n := range keyvalue { + kv.Set([]byte(n.key), []byte(n.value)) + } + } - //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") + //store has all the key value pairs + storeHasAll := func(kv KVStore) bool { + for _, n := range keyvalue { + if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) { + return false + } + } + return true + } - //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") + //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 logging - assert.True(t, len(kvc.GetLogLines()) == 0, "logging events existed before using SetLogging") - fmt.Println(len(kvc.GetLogLines())) + //test read/write for KVCache + {func() bool { setRecords(kvc); return storeHasAll(kvc) }, + "KVCache doesn't retrieve after Set"}, - kvc.SetLogging() - setRecords() - assert.True(t, len(kvc.GetLogLines()) == 2, "incorrect number of logging events recorded") + //test reset + {func() bool { kvc.Reset(); return !storeHasAll(kvc) }, + "KVCache retrieving after reset"}, - kvc.ClearLogLines() - assert.True(t, len(kvc.GetLogLines()) == 0, "logging events still exists after ClearLogLines") + //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 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"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass(), tl.errMsg) + } } diff --git a/types/plugin_test.go b/types/plugin_test.go index 21cd92114d..2bb9e08490 100644 --- a/types/plugin_test.go +++ b/types/plugin_test.go @@ -31,7 +31,7 @@ func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBloc //---------------------------------- -func TestPlugin(t *testing.T) { +/*func TestPlugin(t *testing.T) { plugins := NewPlugins() assert.True(t, len(plugins.GetList()) == 0, "plugins object init with a objects") @@ -39,4 +39,28 @@ func TestPlugin(t *testing.T) { 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") +}*/ + +func TestPlugin(t *testing.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) + } + } diff --git a/types/tx_test.go b/types/tx_test.go index 43bcc032c6..072b78d206 100644 --- a/types/tx_test.go +++ b/types/tx_test.go @@ -3,7 +3,9 @@ package types import ( "testing" - . "github.com/tendermint/go-common" + cmn "github.com/tendermint/go-common" + + "github.com/stretchr/testify/assert" ) var chainID string = "test_chain" @@ -36,11 +38,11 @@ func TestSendTxSignable(t *testing.T) { }, } signBytes := sendTx.SignBytes(chainID) - signBytesHex := Fmt("%X", signBytes) + signBytesHex := cmn.Fmt("%X", signBytes) expected := "010A746573745F636861696E0100000000000000DE00000000000000006F01020106696E7075743101010000000000000030390301093200000106696E70757432010100000000000000006F01DE0000010201076F757470757431010100000000000000014D01076F75747075743201010000000000000001BC" - if signBytesHex != expected { - t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex) - } + + assert.True(t, signBytesHex == expected, + cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)) } func TestAppTxSignable(t *testing.T) { @@ -56,9 +58,9 @@ func TestAppTxSignable(t *testing.T) { Data: []byte("data1"), } signBytes := callTx.SignBytes(chainID) - signBytesHex := Fmt("%X", signBytes) + signBytesHex := cmn.Fmt("%X", signBytes) expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131" - if signBytesHex != expected { - t.Errorf("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex) - } + + assert.True(t, signBytesHex == expected, + cmn.Fmt("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)) } From 0c12e78d986b3f4638aeb02f0461da29ec877095 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 23 Feb 2017 19:10:19 -0500 Subject: [PATCH 4/4] remove commented test --- types/account_test.go | 1 + types/plugin_test.go | 10 ---------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/types/account_test.go b/types/account_test.go index 2f4d49260f..c6385f36ef 100644 --- a/types/account_test.go +++ b/types/account_test.go @@ -17,6 +17,7 @@ func TestAccount(t *testing.T) { //test Copy accCopy := acc.Copy() assert.True(t, &acc != accCopy, "Account Copy Error") + assert.True(t, acc.Sequence == accCopy.Sequence) //test sending nils for panic var nilAcc *Account diff --git a/types/plugin_test.go b/types/plugin_test.go index 2bb9e08490..9f4b332d0c 100644 --- a/types/plugin_test.go +++ b/types/plugin_test.go @@ -31,16 +31,6 @@ func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBloc //---------------------------------- -/*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") -}*/ - func TestPlugin(t *testing.T) { plugins := NewPlugins()