github.com/Finschia/finschia-sdk@v0.48.1/x/gov/types/msgs_test.go (about) 1 package types 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 sdk "github.com/Finschia/finschia-sdk/types" 10 ) 11 12 var ( 13 coinsPos = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)) 14 coinsZero = sdk.NewCoins() 15 coinsMulti = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000), sdk.NewInt64Coin("foo", 10000)) 16 addrs = []sdk.AccAddress{ 17 sdk.AccAddress([]byte(" test1")), 18 sdk.AccAddress([]byte(" test2")), 19 } 20 ) 21 22 func init() { 23 coinsMulti.Sort() 24 } 25 26 // test ValidateBasic for MsgCreateValidator 27 func TestMsgSubmitProposal(t *testing.T) { 28 tests := []struct { 29 title, description string 30 proposalType string 31 proposerAddr sdk.AccAddress 32 initialDeposit sdk.Coins 33 expectPass bool 34 }{ 35 {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, true}, 36 {"", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, false}, 37 {"Test Proposal", "", ProposalTypeText, addrs[0], coinsPos, false}, 38 {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, sdk.AccAddress{}, coinsPos, false}, 39 {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsZero, true}, 40 {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true}, 41 {strings.Repeat("#", MaxTitleLength*2), "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, false}, 42 {"Test Proposal", strings.Repeat("#", MaxDescriptionLength*2), ProposalTypeText, addrs[0], coinsMulti, false}, 43 } 44 45 for i, tc := range tests { 46 msg, err := NewMsgSubmitProposal( 47 ContentFromProposalType(tc.title, tc.description, tc.proposalType), 48 tc.initialDeposit, 49 tc.proposerAddr, 50 ) 51 52 require.NoError(t, err) 53 54 if tc.expectPass { 55 require.NoError(t, msg.ValidateBasic(), "test: %v", i) 56 } else { 57 require.Error(t, msg.ValidateBasic(), "test: %v", i) 58 } 59 } 60 } 61 62 func TestMsgDepositGetSignBytes(t *testing.T) { 63 addr := sdk.AccAddress("addr1") 64 msg := NewMsgDeposit(addr, 0, coinsPos) 65 res := msg.GetSignBytes() 66 67 expected := `{"type":"cosmos-sdk/MsgDeposit","value":{"amount":[{"amount":"1000","denom":"stake"}],"depositor":"link1v9jxgu33p9vj2k","proposal_id":"0"}}` 68 require.Equal(t, expected, string(res)) 69 } 70 71 // test ValidateBasic for MsgDeposit 72 func TestMsgDeposit(t *testing.T) { 73 tests := []struct { 74 proposalID uint64 75 depositorAddr sdk.AccAddress 76 depositAmount sdk.Coins 77 expectPass bool 78 }{ 79 {0, addrs[0], coinsPos, true}, 80 {1, sdk.AccAddress{}, coinsPos, false}, 81 {1, addrs[0], coinsZero, true}, 82 {1, addrs[0], coinsMulti, true}, 83 } 84 85 for i, tc := range tests { 86 msg := NewMsgDeposit(tc.depositorAddr, tc.proposalID, tc.depositAmount) 87 if tc.expectPass { 88 require.NoError(t, msg.ValidateBasic(), "test: %v", i) 89 } else { 90 require.Error(t, msg.ValidateBasic(), "test: %v", i) 91 } 92 } 93 } 94 95 // test ValidateBasic for MsgVote 96 func TestMsgVote(t *testing.T) { 97 tests := []struct { 98 proposalID uint64 99 voterAddr sdk.AccAddress 100 option VoteOption 101 expectPass bool 102 }{ 103 {0, addrs[0], OptionYes, true}, 104 {0, sdk.AccAddress{}, OptionYes, false}, 105 {0, addrs[0], OptionNo, true}, 106 {0, addrs[0], OptionNoWithVeto, true}, 107 {0, addrs[0], OptionAbstain, true}, 108 {0, addrs[0], VoteOption(0x13), false}, 109 } 110 111 for i, tc := range tests { 112 msg := NewMsgVote(tc.voterAddr, tc.proposalID, tc.option) 113 if tc.expectPass { 114 require.Nil(t, msg.ValidateBasic(), "test: %v", i) 115 } else { 116 require.NotNil(t, msg.ValidateBasic(), "test: %v", i) 117 } 118 } 119 } 120 121 // test ValidateBasic for MsgVoteWeighted 122 func TestMsgVoteWeighted(t *testing.T) { 123 tests := []struct { 124 proposalID uint64 125 voterAddr sdk.AccAddress 126 options WeightedVoteOptions 127 expectPass bool 128 }{ 129 {0, addrs[0], NewNonSplitVoteOption(OptionYes), true}, 130 {0, sdk.AccAddress{}, NewNonSplitVoteOption(OptionYes), false}, 131 {0, addrs[0], NewNonSplitVoteOption(OptionNo), true}, 132 {0, addrs[0], NewNonSplitVoteOption(OptionNoWithVeto), true}, 133 {0, addrs[0], NewNonSplitVoteOption(OptionAbstain), true}, 134 {0, addrs[0], WeightedVoteOptions{ // weight sum > 1 135 WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDec(1)}, 136 WeightedVoteOption{Option: OptionAbstain, Weight: sdk.NewDec(1)}, 137 }, false}, 138 {0, addrs[0], WeightedVoteOptions{ // duplicate option 139 WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDecWithPrec(5, 1)}, 140 WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDecWithPrec(5, 1)}, 141 }, false}, 142 {0, addrs[0], WeightedVoteOptions{ // zero weight 143 WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDec(0)}, 144 }, false}, 145 {0, addrs[0], WeightedVoteOptions{ // negative weight 146 WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDec(-1)}, 147 }, false}, 148 {0, addrs[0], WeightedVoteOptions{}, false}, 149 {0, addrs[0], NewNonSplitVoteOption(VoteOption(0x13)), false}, 150 {0, addrs[0], WeightedVoteOptions{ // weight sum <1 151 WeightedVoteOption{Option: OptionYes, Weight: sdk.NewDecWithPrec(5, 1)}, 152 }, false}, 153 } 154 155 for i, tc := range tests { 156 msg := NewMsgVoteWeighted(tc.voterAddr, tc.proposalID, tc.options) 157 if tc.expectPass { 158 require.Nil(t, msg.ValidateBasic(), "test: %v", i) 159 } else { 160 require.NotNil(t, msg.ValidateBasic(), "test: %v", i) 161 } 162 } 163 } 164 165 // this tests that Amino JSON MsgSubmitProposal.GetSignBytes() still works with Content as Any using the ModuleCdc 166 func TestMsgSubmitProposal_GetSignBytes(t *testing.T) { 167 msg, err := NewMsgSubmitProposal(NewTextProposal("test", "abcd"), sdk.NewCoins(), sdk.AccAddress{}) 168 require.NoError(t, err) 169 var bz []byte 170 require.NotPanics(t, func() { 171 bz = msg.GetSignBytes() 172 }) 173 require.Equal(t, 174 `{"type":"cosmos-sdk/MsgSubmitProposal","value":{"content":{"type":"cosmos-sdk/TextProposal","value":{"description":"abcd","title":"test"}},"initial_deposit":[]}}`, 175 string(bz)) 176 }