Skip to content

Commit

Permalink
x/params: CLI Refactor & In-Process Tests (#6720)
Browse files Browse the repository at this point in the history
ref: #6423
  • Loading branch information
alexanderbez authored Jul 14, 2020
1 parent 351192a commit 44f1482
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
93 changes: 93 additions & 0 deletions x/params/client/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
17 changes: 10 additions & 7 deletions x/params/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion x/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 44f1482

Please sign in to comment.