refactor(evidence): CLI tests using Tendermint Mock (#13056)
* wip * fix something * remove test case from tx test * fix test * add build tags to cli test * use require instead of assert * add cli tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//go:build e2e
|
||||
// +build e2e
|
||||
|
||||
package testutil
|
||||
package evidence
|
||||
|
||||
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/evidence/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 evidence
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -0,0 +1,132 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
tmbytes "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/crypto/keyring"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/evidence"
|
||||
"github.com/cosmos/cosmos-sdk/x/evidence/client/cli"
|
||||
)
|
||||
|
||||
var _ client.TendermintRPC = (*mockTendermintRPC)(nil)
|
||||
|
||||
type mockTendermintRPC struct {
|
||||
rpcclientmock.Client
|
||||
|
||||
responseQuery abci.ResponseQuery
|
||||
}
|
||||
|
||||
func newMockTendermintRPC(respQuery abci.ResponseQuery) mockTendermintRPC {
|
||||
return mockTendermintRPC{responseQuery: respQuery}
|
||||
}
|
||||
|
||||
func (_ mockTendermintRPC) BroadcastTxCommit(_ context.Context, _ tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
|
||||
return &coretypes.ResultBroadcastTxCommit{}, nil
|
||||
}
|
||||
|
||||
func (m mockTendermintRPC) ABCIQueryWithOptions(
|
||||
_ context.Context,
|
||||
_ string, _ tmbytes.HexBytes,
|
||||
_ rpcclient.ABCIQueryOptions,
|
||||
) (*coretypes.ResultABCIQuery, error) {
|
||||
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
|
||||
}
|
||||
|
||||
func TestGetQueryCmd(t *testing.T) {
|
||||
cmd := cli.GetQueryCmd()
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(evidence.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")
|
||||
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
ctxGen func() client.Context
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
expectErr bool
|
||||
}{
|
||||
"non-existent evidence": {
|
||||
[]string{"DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660"},
|
||||
func() client.Context {
|
||||
bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{})
|
||||
c := newMockTendermintRPC(abci.ResponseQuery{
|
||||
Value: bz,
|
||||
})
|
||||
return baseCtx.WithClient(c)
|
||||
},
|
||||
"DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660",
|
||||
"",
|
||||
true,
|
||||
},
|
||||
"all evidence (default pagination)": {
|
||||
[]string{},
|
||||
func() client.Context {
|
||||
bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{})
|
||||
c := newMockTendermintRPC(abci.ResponseQuery{
|
||||
Value: bz,
|
||||
})
|
||||
return baseCtx.WithClient(c)
|
||||
},
|
||||
"",
|
||||
"evidence: []\npagination: null",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var outBuf bytes.Buffer
|
||||
|
||||
clientCtx := tc.ctxGen().WithOutput(&outBuf)
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.args)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(clientCtx, cmd))
|
||||
|
||||
if len(tc.args) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.Contains(t, fmt.Sprint(cmd), "evidence [] [] Query for evidence by hash or for all (paginated) submitted evidence")
|
||||
require.Contains(t, strings.TrimSpace(out.String()), tc.expectedOutput)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user