validatebasic tests cleanup

This commit is contained in:
rigelrozanski 2018-03-26 16:48:15 +02:00
parent c1cb53ca58
commit 1df21e0fb6
5 changed files with 119 additions and 107 deletions

View File

@ -41,6 +41,7 @@ var (
}
)
/*
func TestUpdateVotingPower(t *testing.T) {
ctx, _, keeper := createTestInput(t, nil, false, 0)
@ -195,6 +196,7 @@ func TestValidatorsChanged(t *testing.T) {
testRemove(t, candidates[3].validator(), change[3])
testChange(t, candidates[4].validator(), change[4])
}
*/
// XXX BROKEN TEST
func TestGetValidators(t *testing.T) {
@ -220,7 +222,6 @@ func TestCandidate(t *testing.T) {
bytes.Equal(c1.Address, c2.Address) &&
c1.Assets.Equal(c2.Assets) &&
c1.Liabilities.Equal(c2.Liabilities) &&
c1.VotingPower.Equal(c2.VotingPower) &&
c1.Description == c2.Description
}

View File

@ -11,6 +11,10 @@ import (
// name to idetify transaction types
const MsgType = "stake"
// XXX remove: think it makes more sense belonging with the Params so we can
// initialize at genesis - to allow for the same tests we should should make
// the ValidateBasic() function a return from an initializable function
// ValidateBasic(bondDenom string) function
const StakingToken = "fermion"
//Verify interface at compile time

View File

@ -3,20 +3,13 @@ package stake
import (
"testing"
"github.com/stretchr/testify/assert"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/magiconair/properties/assert"
crypto "github.com/tendermint/go-crypto"
)
var (
addr1 = []byte("addr1")
addr2 = []byte("addr2")
addr3 = []byte("addr3")
emptyAddr sdk.Address
pubkey1 = crypto.GenPrivKeyEd25519().PubKey()
emptyPubkey crypto.PubKey
coinPos = sdk.Coin{"fermion", 1000}
coinZero = sdk.Coin{"fermion", 0}
coinNeg = sdk.Coin{"fermion", -10000}
@ -25,71 +18,62 @@ var (
coinNegNotAtoms = sdk.Coin{"foo", -10000}
)
// test ValidateBasic for MsgDeclareCandidacy
func TestMsgDeclareCandidacy(t *testing.T) {
tests := []struct {
name string
moniker string
identity string
website string
details string
candidateAddr sdk.Address
pubkey crypto.PubKey
bond sdk.Coin
expectPass bool
name, moniker, identity, website, details string
candidateAddr sdk.Address
pubkey crypto.PubKey
bond sdk.Coin
expectPass bool
}{
{"basic good", "a", "b", "c", "d", addr1, pubkey1, coinPos, true},
{"partial description", "", "", "c", "", addr1, pubkey1, coinPos, true},
{"empty description", "", "", "", "", addr1, pubkey1, coinPos, false},
{"empty address", "a", "b", "c", "d", emptyAddr, pubkey1, coinPos, false},
{"empty pubkey", "a", "b", "c", "d", addr1, emptyPubkey, coinPos, true},
{"empty bond", "a", "b", "c", "d", addr1, pubkey1, coinZero, false},
{"negative bond", "a", "b", "c", "d", addr1, pubkey1, coinNeg, false},
{"negative bond", "a", "b", "c", "d", addr1, pubkey1, coinNeg, false},
{"wrong staking token", "a", "b", "c", "d", addr1, pubkey1, coinPosNotAtoms, false},
{"basic good", "a", "b", "c", "d", addrs[0], pks[0], coinPos, true},
{"partial description", "", "", "c", "", addrs[0], pks[0], coinPos, true},
{"empty description", "", "", "", "", addrs[0], pks[0], coinPos, false},
{"empty address", "a", "b", "c", "d", emptyAddr, pks[0], coinPos, false},
{"empty pubkey", "a", "b", "c", "d", addrs[0], emptyPubkey, coinPos, true},
{"empty bond", "a", "b", "c", "d", addrs[0], pks[0], coinZero, false},
{"negative bond", "a", "b", "c", "d", addrs[0], pks[0], coinNeg, false},
{"negative bond", "a", "b", "c", "d", addrs[0], pks[0], coinNeg, false},
{"wrong staking token", "a", "b", "c", "d", addrs[0], pks[0], coinPosNotAtoms, false},
}
for _, tc := range tests {
description := Description{
Moniker: tc.moniker,
Identity: tc.identity,
Website: tc.website,
Details: tc.details,
}
description := NewDescription(tc.moniker, tc.identity, tc.website, tc.details)
msg := NewMsgDeclareCandidacy(tc.candidateAddr, tc.pubkey, tc.bond, description)
assert.Equal(t, tc.expectPass, msg.ValidateBasic() == nil,
"test: ", tc.name)
if tc.expectPass {
assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
} else {
assert.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
}
}
}
// test ValidateBasic for MsgEditCandidacy
func TestMsgEditCandidacy(t *testing.T) {
tests := []struct {
name string
moniker string
identity string
website string
details string
candidateAddr sdk.Address
expectPass bool
name, moniker, identity, website, details string
candidateAddr sdk.Address
expectPass bool
}{
{"basic good", "a", "b", "c", "d", addr1, true},
{"partial description", "", "", "c", "", addr1, true},
{"empty description", "", "", "", "", addr1, false},
{"basic good", "a", "b", "c", "d", addrs[0], true},
{"partial description", "", "", "c", "", addrs[0], true},
{"empty description", "", "", "", "", addrs[0], false},
{"empty address", "a", "b", "c", "d", emptyAddr, false},
}
for _, tc := range tests {
description := Description{
Moniker: tc.moniker,
Identity: tc.identity,
Website: tc.website,
Details: tc.details,
}
description := NewDescription(tc.moniker, tc.identity, tc.website, tc.details)
msg := NewMsgEditCandidacy(tc.candidateAddr, description)
assert.Equal(t, tc.expectPass, msg.ValidateBasic() == nil,
"test: ", tc.name)
if tc.expectPass {
assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
} else {
assert.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
}
}
}
// test ValidateBasic for MsgDelegate
func TestMsgDelegate(t *testing.T) {
tests := []struct {
name string
@ -98,22 +82,26 @@ func TestMsgDelegate(t *testing.T) {
bond sdk.Coin
expectPass bool
}{
{"basic good", addr1, addr2, coinPos, true},
{"self bond", addr1, addr1, coinPos, true},
{"empty delegator", emptyAddr, addr1, coinPos, false},
{"empty candidate", addr1, emptyAddr, coinPos, false},
{"empty bond", addr1, addr2, coinZero, false},
{"negative bond", addr1, addr2, coinNeg, false},
{"wrong staking token", addr1, addr2, coinPosNotAtoms, false},
{"basic good", addrs[0], addrs[1], coinPos, true},
{"self bond", addrs[0], addrs[0], coinPos, true},
{"empty delegator", emptyAddr, addrs[0], coinPos, false},
{"empty candidate", addrs[0], emptyAddr, coinPos, false},
{"empty bond", addrs[0], addrs[1], coinZero, false},
{"negative bond", addrs[0], addrs[1], coinNeg, false},
{"wrong staking token", addrs[0], addrs[1], coinPosNotAtoms, false},
}
for _, tc := range tests {
msg := NewMsgDelegate(tc.delegatorAddr, tc.candidateAddr, tc.bond)
assert.Equal(t, tc.expectPass, msg.ValidateBasic() == nil,
"test: ", tc.name)
if tc.expectPass {
assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
} else {
assert.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
}
}
}
// test ValidateBasic for MsgUnbond
func TestMsgUnbond(t *testing.T) {
tests := []struct {
name string
@ -122,19 +110,22 @@ func TestMsgUnbond(t *testing.T) {
shares string
expectPass bool
}{
{"max unbond", addr1, addr2, "MAX", true},
{"decimal unbond", addr1, addr2, "0.1", true},
{"negative decimal unbond", addr1, addr2, "-0.1", false},
{"zero unbond", addr1, addr2, "0.0", false},
{"invalid decimal", addr1, addr1, "sunny", false},
{"empty delegator", emptyAddr, addr1, "0.1", false},
{"empty candidate", addr1, emptyAddr, "0.1", false},
{"max unbond", addrs[0], addrs[1], "MAX", true},
{"decimal unbond", addrs[0], addrs[1], "0.1", true},
{"negative decimal unbond", addrs[0], addrs[1], "-0.1", false},
{"zero unbond", addrs[0], addrs[1], "0.0", false},
{"invalid decimal", addrs[0], addrs[0], "sunny", false},
{"empty delegator", emptyAddr, addrs[0], "0.1", false},
{"empty candidate", addrs[0], emptyAddr, "0.1", false},
}
for _, tc := range tests {
msg := NewMsgUnbond(tc.delegatorAddr, tc.candidateAddr, tc.shares)
assert.Equal(t, tc.expectPass, msg.ValidateBasic() == nil,
"test: ", tc.name)
if tc.expectPass {
assert.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
} else {
assert.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
}
}
}

View File

@ -19,6 +19,40 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"
)
// dummy addresses used for testing
var (
addrs = []sdk.Address{
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6163"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6164"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6165"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6166"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6167"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6168"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169"),
}
// dummy pubkeys used for testing
pks = []crypto.PubKey{
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB54"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB55"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB56"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB58"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB59"),
}
emptyAddr sdk.Address
emptyPubkey crypto.PubKey
)
// XXX reference the common declaration of this function
func subspace(prefix []byte) (start, end []byte) {
end = make([]byte, len(prefix))
copy(end, prefix)
@ -112,19 +146,6 @@ func newPubKey(pk string) (res crypto.PubKey) {
return pkEd.Wrap()
}
var pks = []crypto.PubKey{
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB54"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB55"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB56"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB58"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB59"),
}
// for incode address generation
func testAddr(addr string) sdk.Address {
res, err := sdk.GetAddress(addr)
@ -134,20 +155,6 @@ func testAddr(addr string) sdk.Address {
return res
}
// dummy addresses used for testing
var addrs = []sdk.Address{
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6163"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6164"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6165"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6166"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6167"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6168"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169"),
}
// XXX TODO remove this dep
func candidatesFromAddrs(ctx sdk.Context, keeper Keeper, addrs []crypto.Address, amts []int64) {
for i := 0; i < len(amts); i++ {

View File

@ -106,14 +106,6 @@ type Candidate struct {
Description Description `json:"description"` // Description terms for the candidate
}
// Description - description fields for a candidate
type Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Website string `json:"website"`
Details string `json:"details"`
}
// NewCandidate - initialize a new candidate
func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate {
return Candidate{
@ -126,6 +118,23 @@ func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Descrip
}
}
// Description - description fields for a candidate
type Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Website string `json:"website"`
Details string `json:"details"`
}
func NewDescription(moniker, identity, website, details string) Description {
return Description{
Moniker: moniker,
Identity: identity,
Website: website,
Details: details,
}
}
// get the exchange rate of global pool shares over delegator shares
func (c Candidate) delegatorShareExRate() sdk.Rat {
if c.Liabilities.IsZero() {