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