cosmos-sdk/x/bank/client/cli/query_test.go
Matt Kocubinski 50cec67475
refactor(crisis): CLI tests using Tendermint mock (#12874)
## Description

Closes: #12873 
- Rewrite existing x/crisis e2e tests as CLI tests using Tendermint mock
- Move existing e2e tests to `tests/e2e`
- Refactor x/bank account test utils for reuse in x/crisis (will also be useful elsewhere)



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-08-10 13:54:31 +00:00

364 lines
7.8 KiB
Go

package cli_test
import (
"bytes"
"context"
"fmt"
"io"
"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
func (s *CLITestSuite) TestGetBalancesCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.GetBalancesCmd()
cmd.SetOutput(io.Discard)
testCases := []struct {
name string
ctxGen func() client.Context
args []string
expectResult proto.Message
expectErr bool
}{
{
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryAllBalancesResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
accounts[0].Address.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QueryAllBalancesResponse{},
false,
},
{
"valid query with denom",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryBalanceResponse{
Balance: &sdk.Coin{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
accounts[0].Address.String(),
fmt.Sprintf("--%s=photon", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&sdk.Coin{},
false,
},
{
"invalid Address",
func() client.Context {
return s.baseCtx
},
[]string{
"foo",
},
nil,
true,
},
{
"invalid denom",
func() client.Context {
c := newMockTendermintRPC(abci.ResponseQuery{
Code: 1,
})
return s.baseCtx.WithClient(c)
},
[]string{
accounts[0].Address.String(),
fmt.Sprintf("--%s=foo", cli.FlagDenom),
},
nil,
true,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
var outBuf bytes.Buffer
clientCtx := tc.ctxGen().WithOutput(&outBuf)
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))
err := cmd.Execute()
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(s.encCfg.Codec.UnmarshalJSON(outBuf.Bytes(), tc.expectResult))
s.Require().NoError(err)
}
})
}
}
func (s *CLITestSuite) TestGetCmdDenomsMetadata() {
cmd := cli.GetCmdDenomsMetadata()
cmd.SetOutput(io.Discard)
testCases := []struct {
name string
ctxGen func() client.Context
args []string
expectResult proto.Message
expectErr bool
}{
{
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryDenomsMetadataResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QueryDenomsMetadataResponse{},
false,
},
{
"valid query with denom",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryDenomMetadataResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=photon", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QueryDenomMetadataResponse{},
false,
},
{
"invalid query with denom",
func() client.Context {
c := newMockTendermintRPC(abci.ResponseQuery{
Code: 1,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=foo", cli.FlagDenom),
},
nil,
true,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
var outBuf bytes.Buffer
clientCtx := tc.ctxGen().WithOutput(&outBuf)
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))
err := cmd.Execute()
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(s.encCfg.Codec.UnmarshalJSON(outBuf.Bytes(), tc.expectResult))
s.Require().NoError(err)
}
})
}
}
func (s *CLITestSuite) TestGetCmdQueryTotalSupply() {
cmd := cli.GetCmdQueryTotalSupply()
cmd.SetOutput(io.Discard)
testCases := []struct {
name string
ctxGen func() client.Context
args []string
expectResult proto.Message
expectErr bool
}{
{
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryTotalSupplyResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QueryTotalSupplyResponse{},
false,
},
{
"valid query with denom",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySupplyOfResponse{
Amount: sdk.Coin{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=photon", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&sdk.Coin{},
false,
},
{
"invalid query with denom",
func() client.Context {
c := newMockTendermintRPC(abci.ResponseQuery{
Code: 1,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=foo", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
nil,
true,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
var outBuf bytes.Buffer
clientCtx := tc.ctxGen().WithOutput(&outBuf)
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))
err := cmd.Execute()
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(s.encCfg.Codec.UnmarshalJSON(outBuf.Bytes(), tc.expectResult))
s.Require().NoError(err)
}
})
}
}
func (s *CLITestSuite) TestGetCmdQuerySendEnabled() {
cmd := cli.GetCmdQuerySendEnabled()
cmd.SetOutput(io.Discard)
testCases := []struct {
name string
ctxGen func() client.Context
args []string
expectResult proto.Message
expectErr bool
}{
{
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySendEnabledResponse{
SendEnabled: []*types.SendEnabled{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QuerySendEnabledResponse{},
false,
},
{
"valid query with denoms",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySendEnabledResponse{
SendEnabled: []*types.SendEnabled{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
"photon",
"stake",
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QuerySendEnabledResponse{},
false,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
var outBuf bytes.Buffer
clientCtx := tc.ctxGen().WithOutput(&outBuf)
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))
err := cmd.Execute()
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(s.encCfg.Codec.UnmarshalJSON(outBuf.Bytes(), tc.expectResult))
s.Require().NoError(err)
}
})
}
}