cosmos-sdk/x/stake/tx_test.go

84 lines
1.8 KiB
Go
Raw Normal View History

2018-02-24 05:19:32 -08:00
package stake
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
wire "github.com/tendermint/go-wire"
2018-02-25 15:45:20 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-02-24 05:19:32 -08:00
)
var (
validator = []byte("addressvalidator1")
empty sdk.Address
2018-02-24 05:19:32 -08:00
2018-02-25 15:45:20 -08:00
coinPos = sdk.Coin{"fermion", 1000}
coinZero = sdk.Coin{"fermion", 0}
coinNeg = sdk.Coin{"fermion", -10000}
coinPosNotAtoms = sdk.Coin{"foo", 10000}
coinZeroNotAtoms = sdk.Coin{"foo", 0}
coinNegNotAtoms = sdk.Coin{"foo", -10000}
2018-02-24 05:19:32 -08:00
)
func TestMsgAddrValidateBasic(t *testing.T) {
2018-02-24 05:19:32 -08:00
tests := []struct {
name string
address sdk.Address
2018-02-24 05:19:32 -08:00
wantErr bool
}{
2018-03-17 15:13:42 -07:00
{"basic good", addrs[0], false},
{"empty delegator", sdk.Address{}, true},
2018-02-24 05:19:32 -08:00
}
for _, tc := range tests {
tx := NewMsgAddr(tc.address)
2018-02-24 05:19:32 -08:00
assert.Equal(t, tc.wantErr, tx.ValidateBasic() != nil,
"test: %v, tx.ValidateBasic: %v", tc.name, tx.ValidateBasic())
}
}
func TestValidateCoin(t *testing.T) {
tests := []struct {
name string
coin sdk.Coin
wantErr bool
}{
{"basic good", coinPos, false},
{"zero coin", coinZero, true},
{"neg coin", coinNeg, true},
}
for _, tc := range tests {
2018-03-17 15:13:42 -07:00
assert.Equal(t, tc.wantErr, validateCoin(tc.coin) != nil,
"test: %v, tx.ValidateBasic: %v", tc.name, validateCoin(tc.coin))
}
}
2018-03-17 15:13:42 -07:00
func TestSerializeMsg(t *testing.T) {
2018-02-24 05:19:32 -08:00
// make sure all types construct properly
bondAmt := 1234321
2018-03-17 15:13:42 -07:00
bond := sdk.Coin{Denom: "atom", Amount: int64(bondAmt)}
2018-02-24 05:19:32 -08:00
tests := []struct {
2018-03-17 15:13:42 -07:00
tx sdk.Msg
2018-02-24 05:19:32 -08:00
}{
2018-03-17 15:13:42 -07:00
{NewMsgDeclareCandidacy(addrs[0], pks[0], bond, Description{})},
{NewMsgEditCandidacy(addrs[0], Description{})},
{NewMsgDelegate(addrs[0], bond)},
{NewMsgUnbond(addrs[0], strconv.Itoa(bondAmt))},
2018-02-24 05:19:32 -08:00
}
for i, tc := range tests {
var tx sdk.Tx
bs := wire.BinaryBytes(tc.tx)
err := wire.ReadBinaryBytes(bs, &tx)
2018-02-25 15:45:20 -08:00
if assert.NoError(t, err, "%d", i) {
assert.Equal(t, tc.tx, tx, "%d", i)
2018-02-24 05:19:32 -08:00
}
}
}