2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-07-03 21:21:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
2018-11-12 10:54:18 -08:00
|
|
|
stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-11-12 10:54:18 -08:00
|
|
|
coinsPos = sdk.Coins{sdk.NewInt64Coin(stakeTypes.DefaultBondDenom, 1000)}
|
2018-06-21 17:19:14 -07:00
|
|
|
coinsZero = sdk.Coins{}
|
2018-07-30 17:09:50 -07:00
|
|
|
coinsPosNotAtoms = sdk.Coins{sdk.NewInt64Coin("foo", 10000)}
|
2018-11-12 10:54:18 -08:00
|
|
|
coinsMulti = sdk.Coins{sdk.NewInt64Coin(stakeTypes.DefaultBondDenom, 1000), sdk.NewInt64Coin("foo", 10000)}
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
2018-11-12 10:54:18 -08:00
|
|
|
func init() {
|
|
|
|
coinsMulti.Sort()
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// test ValidateBasic for MsgCreateValidator
|
|
|
|
func TestMsgSubmitProposal(t *testing.T) {
|
|
|
|
_, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{})
|
|
|
|
tests := []struct {
|
|
|
|
title, description string
|
2018-07-10 17:59:07 -07:00
|
|
|
proposalType ProposalKind
|
2018-07-06 00:06:53 -07:00
|
|
|
proposerAddr sdk.AccAddress
|
2018-06-21 17:19:14 -07:00
|
|
|
initialDeposit sdk.Coins
|
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, true},
|
|
|
|
{"", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, false},
|
|
|
|
{"Test Proposal", "", ProposalTypeText, addrs[0], coinsPos, false},
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeParameterChange, addrs[0], coinsPos, true},
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeSoftwareUpgrade, addrs[0], coinsPos, true},
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", 0x05, addrs[0], coinsPos, false},
|
2018-07-06 00:06:53 -07:00
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, sdk.AccAddress{}, coinsPos, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsZero, true},
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
msg := NewMsgSubmitProposal(tc.title, tc.description, tc.proposalType, tc.proposerAddr, tc.initialDeposit)
|
|
|
|
if tc.expectPass {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.NoError(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
} else {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.Error(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgDeposit
|
|
|
|
func TestMsgDeposit(t *testing.T) {
|
|
|
|
_, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{})
|
|
|
|
tests := []struct {
|
2018-11-06 23:33:18 -08:00
|
|
|
proposalID uint64
|
2018-07-06 00:06:53 -07:00
|
|
|
depositerAddr sdk.AccAddress
|
2018-06-21 17:19:14 -07:00
|
|
|
depositAmount sdk.Coins
|
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{0, addrs[0], coinsPos, true},
|
2018-07-06 00:06:53 -07:00
|
|
|
{1, sdk.AccAddress{}, coinsPos, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
{1, addrs[0], coinsZero, true},
|
|
|
|
{1, addrs[0], coinsMulti, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
msg := NewMsgDeposit(tc.depositerAddr, tc.proposalID, tc.depositAmount)
|
|
|
|
if tc.expectPass {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.NoError(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
} else {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.Error(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgDeposit
|
|
|
|
func TestMsgVote(t *testing.T) {
|
|
|
|
_, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{})
|
|
|
|
tests := []struct {
|
2018-11-06 23:33:18 -08:00
|
|
|
proposalID uint64
|
2018-07-06 00:06:53 -07:00
|
|
|
voterAddr sdk.AccAddress
|
2018-06-21 17:19:14 -07:00
|
|
|
option VoteOption
|
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{0, addrs[0], OptionYes, true},
|
2018-07-06 00:06:53 -07:00
|
|
|
{0, sdk.AccAddress{}, OptionYes, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
{0, addrs[0], OptionNo, true},
|
|
|
|
{0, addrs[0], OptionNoWithVeto, true},
|
|
|
|
{0, addrs[0], OptionAbstain, true},
|
|
|
|
{0, addrs[0], VoteOption(0x13), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
msg := NewMsgVote(tc.voterAddr, tc.proposalID, tc.option)
|
|
|
|
if tc.expectPass {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|