Skip to content

Commit

Permalink
refactor to use test help to send tx by client
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno committed Jul 1, 2020
1 parent 55cabc9 commit 3c5dec4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
65 changes: 30 additions & 35 deletions x/bank/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
banktest "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
)

type IntegrationTestSuite struct {
Expand Down Expand Up @@ -176,26 +177,27 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
}

func (s *IntegrationTestSuite) TestNewSendTxCmd() {
buf := new(bytes.Buffer)
val := s.network.Validators[0]
clientCtx := val.ClientCtx.WithOutput(buf)

testCases := []struct {
name string
from string
to string
amount sdk.Coins
args []string
expectErr bool
respType fmt.Stringer
expectedCode uint32
}{
{
"valid transaction (gen-only)",
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
),
[]string{
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
).String(),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
Expand All @@ -207,13 +209,13 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() {
},
{
"valid transaction",
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
),
[]string{
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
).String(),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
Expand All @@ -224,13 +226,13 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() {
},
{
"not enough fees",
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
),
[]string{
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
).String(),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()),
Expand All @@ -241,13 +243,13 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() {
},
{
"not enough gas",
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
),
[]string{
val.Address.String(),
val.Address.String(),
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)),
).String(),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
Expand All @@ -263,19 +265,12 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() {
tc := tc

s.Run(tc.name, func() {
buf.Reset()

cmd := cli.NewSendTxCmd(clientCtx)
cmd.SetErr(buf)
cmd.SetOut(buf)
cmd.SetArgs(tc.args)

err := cmd.Execute()
res, err := banktest.SendTx(val.ClientCtx, tc.from, tc.to, tc.amount, tc.args...)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(buf.Bytes(), tc.respType), buf.String())
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(res, tc.respType), string(res))

txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code)
Expand Down
1 change: 1 addition & 0 deletions x/bank/client/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TxSend(f *cli.Fixtures, from string, to sdk.AccAddress, amount sdk.Coin, fl
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

// SendTx sends a transaction using Client Command and returns the response as bytes or error if there is ap problem.
func SendTx(ctx client.Context, from string, to string, amount sdk.Coins, flags ...string) ([]byte, error) {
buf := new(bytes.Buffer)
ctx = ctx.WithOutput(buf)
Expand Down

0 comments on commit 3c5dec4

Please sign in to comment.