x/params: CLI Refactor & In-Process Tests (#6720)

ref: #6423
This commit is contained in:
Alexander Bezobchuk 2020-07-14 16:52:31 -04:00 committed by GitHub
parent 351192aa0b
commit 44f14828d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 8 deletions

View File

@ -0,0 +1,93 @@
package cli_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/x/params/client/cli"
)
type IntegrationTestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
cfg := network.DefaultConfig()
cfg.NumValidators = 1
s.cfg = cfg
s.network = network.New(s.T(), cfg)
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
func (s *IntegrationTestSuite) TestNewQuerySubspaceParamsCmd() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectedOutput string
}{
{
"default output",
[]string{
"staking", "MaxValidators",
},
`{"Subspace":"staking","Key":"MaxValidators","Value":"100"}`,
},
{
"text output",
[]string{
"staking", "MaxValidators",
fmt.Sprintf("--%s=text", tmcli.OutputFlag),
},
`Key: MaxValidators
Subspace: staking
Value: "100"`,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewQuerySubspaceParamsCmd()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
out.Reset()
cmd.SetArgs(tc.args)
s.Require().NoError(cmd.ExecuteContext(ctx))
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
})
}
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

View File

@ -7,12 +7,11 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/params/types"
)
// NewQueryCmd returns a root CLI command handler for all x/params query commands.
func NewQueryCmd(m codec.JSONMarshaler) *cobra.Command {
func NewQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the params module",
@ -21,25 +20,29 @@ func NewQueryCmd(m codec.JSONMarshaler) *cobra.Command {
RunE: client.ValidateCmd,
}
cmd.AddCommand(NewQuerySubspaceParamsCmd(m))
cmd.AddCommand(NewQuerySubspaceParamsCmd())
return cmd
}
// NewQuerySubspaceParamsCmd returns a CLI command handler for querying subspace
// parameters managed by the x/params module.
func NewQuerySubspaceParamsCmd(m codec.JSONMarshaler) *cobra.Command {
func NewQuerySubspaceParamsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "subspace [subspace] [key]",
Short: "Query for raw parameters by subspace and key",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithJSONMarshaler(m)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
params := types.NewQuerySubspaceParams(args[0], args[1])
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
bz, err := m.MarshalJSON(params)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
return fmt.Errorf("failed to marshal params: %w", err)
}
@ -50,7 +53,7 @@ func NewQuerySubspaceParamsCmd(m codec.JSONMarshaler) *cobra.Command {
}
var resp types.SubspaceParamsResponse
if err := m.UnmarshalJSON(bz, &resp); err != nil {
if err := clientCtx.JSONMarshaler.UnmarshalJSON(bz, &resp); err != nil {
return err
}

View File

@ -16,6 +16,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/params/client/cli"
"github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/params/simulation"
"github.com/cosmos/cosmos-sdk/x/params/types"
@ -56,7 +57,9 @@ func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {}
func (AppModuleBasic) GetTxCmd(_ client.Context) *cobra.Command { return nil }
// GetQueryCmd returns no root query command for the params module.
func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command { return nil }
func (AppModuleBasic) GetQueryCmd(_ client.Context) *cobra.Command {
return cli.NewQueryCmd()
}
func (am AppModuleBasic) RegisterInterfaceTypes(registry codectypes.InterfaceRegistry) {
proposal.RegisterInterfaces(registry)