github.com/Finschia/finschia-sdk@v0.48.1/x/distribution/client/cli/tx_test.go (about) 1 package cli 2 3 import ( 4 "testing" 5 6 "github.com/spf13/pflag" 7 8 "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" 9 "github.com/Finschia/finschia-sdk/simapp/params" 10 "github.com/Finschia/finschia-sdk/testutil" 11 "github.com/Finschia/finschia-sdk/testutil/testdata" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/stretchr/testify/assert" 16 17 "github.com/Finschia/finschia-sdk/client" 18 sdk "github.com/Finschia/finschia-sdk/types" 19 ) 20 21 func Test_splitAndCall_NoMessages(t *testing.T) { 22 clientCtx := client.Context{} 23 24 // empty tx will always trigger genOrBroadcastFn 25 maxMsgsCases := []int{0, 1, 10} 26 calledCounter := 0 27 for _, maxMsgs := range maxMsgsCases { 28 err := newSplitAndApply( 29 func(clientCtx client.Context, fs *pflag.FlagSet, msgs ...sdk.Msg) error { 30 // dummy genOrBroadcastFn called once for each case 31 calledCounter++ 32 return nil 33 }, clientCtx, nil, nil, maxMsgs) 34 assert.NoError(t, err, "") 35 } 36 assert.Equal(t, calledCounter, len(maxMsgsCases)) 37 } 38 39 func Test_splitAndCall_Splitting(t *testing.T) { 40 clientCtx := client.Context{} 41 42 addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) 43 44 // Add five messages 45 msgs := []sdk.Msg{ 46 testdata.NewTestMsg(addr), 47 testdata.NewTestMsg(addr), 48 testdata.NewTestMsg(addr), 49 testdata.NewTestMsg(addr), 50 testdata.NewTestMsg(addr), 51 } 52 53 // Keep track of number of calls 54 const chunkSize = 2 55 56 callCount := 0 57 err := newSplitAndApply( 58 func(clientCtx client.Context, fs *pflag.FlagSet, msgs ...sdk.Msg) error { 59 callCount++ 60 61 assert.NotNil(t, clientCtx) 62 assert.NotNil(t, msgs) 63 64 if callCount < 3 { 65 assert.Equal(t, len(msgs), 2) 66 } else { 67 assert.Equal(t, len(msgs), 1) 68 } 69 70 return nil 71 }, 72 clientCtx, nil, msgs, chunkSize) 73 74 assert.NoError(t, err, "") 75 assert.Equal(t, 3, callCount) 76 } 77 78 func TestParseProposal(t *testing.T) { 79 encodingConfig := params.MakeTestEncodingConfig() 80 81 okJSON := testutil.WriteToNewTempFile(t, ` 82 { 83 "title": "Community Pool Spend", 84 "description": "Pay me some Atoms!", 85 "recipient": "link1s5afhd6gxevu37mkqcvvsj8qeylhn0rz46zdlq", 86 "amount": "1000stake", 87 "deposit": "1000stake" 88 } 89 `) 90 91 proposal, err := ParseCommunityPoolSpendProposalWithDeposit(encodingConfig.Marshaler, okJSON.Name()) 92 require.NoError(t, err) 93 94 require.Equal(t, "Community Pool Spend", proposal.Title) 95 require.Equal(t, "Pay me some Atoms!", proposal.Description) 96 require.Equal(t, "link1s5afhd6gxevu37mkqcvvsj8qeylhn0rz46zdlq", proposal.Recipient) 97 require.Equal(t, "1000stake", proposal.Deposit) 98 require.Equal(t, "1000stake", proposal.Amount) 99 }