refactor(mint): CLI tests using Tendermint Mock (#13059)
* wip: mint CLI tests using Tendermint Mock * try fix test * remove query test * add cli tests Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
parent
0eacc88096
commit
3e3b225a5e
@ -1,7 +1,7 @@
|
||||
//go:build e2e
|
||||
// +build e2e
|
||||
|
||||
package testutil
|
||||
package mint
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -10,11 +10,10 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
clienttestutil "github.com/cosmos/cosmos-sdk/x/mint/client/testutil"
|
||||
)
|
||||
|
||||
func TestIntegrationTestSuite(t *testing.T) {
|
||||
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
|
||||
cfg.NumValidators = 1
|
||||
suite.Run(t, clienttestutil.NewIntegrationTestSuite(cfg))
|
||||
suite.Run(t, NewIntegrationTestSuite(cfg))
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package testutil
|
||||
package mint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -1,4 +1,4 @@
|
||||
package testutil
|
||||
package mint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
233
x/mint/client/cli/query_test.go
Normal file
233
x/mint/client/cli/query_test.go
Normal file
@ -0,0 +1,233 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/bytes"
|
||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock"
|
||||
coretypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint"
|
||||
mintcli "github.com/cosmos/cosmos-sdk/x/mint/client/cli"
|
||||
tmcli "github.com/tendermint/tendermint/libs/cli"
|
||||
)
|
||||
|
||||
var _ client.TendermintRPC = (*mockTendermintRPC)(nil)
|
||||
|
||||
type mockTendermintRPC struct {
|
||||
rpcclientmock.Client
|
||||
|
||||
responseQuery abci.ResponseQuery
|
||||
}
|
||||
|
||||
func (_ mockTendermintRPC) BroadcastTxCommit(_ context.Context, _ tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
|
||||
return &coretypes.ResultBroadcastTxCommit{}, nil
|
||||
}
|
||||
|
||||
func (m mockTendermintRPC) ABCIQueryWithOptions(
|
||||
_ context.Context,
|
||||
_ string, _ bytes.HexBytes,
|
||||
_ rpcclient.ABCIQueryOptions,
|
||||
) (*coretypes.ResultABCIQuery, error) {
|
||||
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
|
||||
}
|
||||
|
||||
func TestGetCmdQueryParams(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(mint.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
cmd := mintcli.GetCmdQueryParams()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
flagArgs []string
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
`[--height=1 --output=json]`,
|
||||
`{"mint_denom":"","inflation_rate_change":"0","inflation_max":"0","inflation_min":"0","goal_bonded":"0","blocks_per_year":"0"}`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
`[--height=1 --output=text]`,
|
||||
`blocks_per_year: "0"
|
||||
goal_bonded: "0"
|
||||
inflation_max: "0"
|
||||
inflation_min: "0"
|
||||
inflation_rate_change: "0"
|
||||
mint_denom: ""`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.flagArgs)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
if len(tc.flagArgs) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), "params [] [] Query the current minting parameters")
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.flagArgs)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCmdQueryInflation(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(mint.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
cmd := mintcli.GetCmdQueryInflation()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
flagArgs []string
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
`[--height=1 --output=json]`,
|
||||
`<nil>`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
`[--height=1 --output=text]`,
|
||||
`<nil>`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.flagArgs)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
if len(tc.flagArgs) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), "inflation [] [] Query the current minting inflation value")
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.flagArgs)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCmdQueryAnnualProvisions(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(mint.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
cmd := mintcli.GetCmdQueryAnnualProvisions()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
flagArgs []string
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
`[--height=1 --output=json]`,
|
||||
`<nil>`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
`[--height=1 --output=text]`,
|
||||
`<nil>`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.flagArgs)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
if len(tc.flagArgs) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), "annual-provisions [] [] Query the current minting annual provisions value")
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.flagArgs)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user