Skip to content

Commit

Permalink
refactor: extract MockTendermintRPC to testutil/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 3, 2022
1 parent 779c0ac commit 08be7ee
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 511 deletions.
41 changes: 41 additions & 0 deletions testutil/cli/tm_mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli

import (
"context"

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"
)

var _ client.TendermintRPC = (*MockTendermintRPC)(nil)

type MockTendermintRPC struct {
rpcclientmock.Client

responseQuery abci.ResponseQuery
}

// NewMockTendermintRPC returns a mock TendermintRPC implementation.
// It is used for CLI testing.
func NewMockTendermintRPC(respQuery abci.ResponseQuery) MockTendermintRPC {
return MockTendermintRPC{responseQuery: respQuery}
}

func (MockTendermintRPC) BroadcastTxSync(context.Context, tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{Code: 0}, nil
}

func (m MockTendermintRPC) ABCIQueryWithOptions(
_ context.Context,
_ string,
_ tmbytes.HexBytes,
_ rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error) {
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
}
32 changes: 2 additions & 30 deletions x/auth/client/cli/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import (
"cosmossdk.io/math"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmcli "github.com/tendermint/tendermint/libs/cli"
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"
Expand All @@ -42,30 +38,6 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)

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) BroadcastTxSync(context.Context, tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{Code: 0}, nil
}

func (m mockTendermintRPC) ABCIQueryWithOptions(
_ context.Context,
_ string, _ tmbytes.HexBytes,
_ rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error) {
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
}

type CLITestSuite struct {
suite.Suite

Expand All @@ -88,15 +60,15 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

var outBuf bytes.Buffer
ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand Down
33 changes: 2 additions & 31 deletions x/authz/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli_test

import (
"bytes"
"context"
"fmt"
"io"
"testing"
Expand All @@ -11,11 +10,7 @@ import (
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/suite"
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/client/flags"
Expand All @@ -41,30 +36,6 @@ var (
typeMsgSubmitProposal = sdk.MsgTypeURL(&govv1.MsgSubmitProposal{})
)

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) BroadcastTxSync(context.Context, tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{Code: 0}, nil
}

func (m mockTendermintRPC) ABCIQueryWithOptions(
_ context.Context,
_ string, _ tmbytes.HexBytes,
_ rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error) {
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
}

type CLITestSuite struct {
suite.Suite

Expand All @@ -87,15 +58,15 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

var outBuf bytes.Buffer
ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand Down
23 changes: 12 additions & 11 deletions x/bank/client/cli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -35,7 +36,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryAllBalancesResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -53,7 +54,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryBalanceResponse{
Balance: &sdk.Coin{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -80,7 +81,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
{
"invalid denom",
func() client.Context {
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Code: 1,
})
return s.baseCtx.WithClient(c)
Expand Down Expand Up @@ -134,7 +135,7 @@ func (s *CLITestSuite) TestGetCmdDenomsMetadata() {
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryDenomsMetadataResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -149,7 +150,7 @@ func (s *CLITestSuite) TestGetCmdDenomsMetadata() {
"valid query with denom",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryDenomMetadataResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -164,7 +165,7 @@ func (s *CLITestSuite) TestGetCmdDenomsMetadata() {
{
"invalid query with denom",
func() client.Context {
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Code: 1,
})
return s.baseCtx.WithClient(c)
Expand Down Expand Up @@ -217,7 +218,7 @@ func (s *CLITestSuite) TestGetCmdQueryTotalSupply() {
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryTotalSupplyResponse{})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -234,7 +235,7 @@ func (s *CLITestSuite) TestGetCmdQueryTotalSupply() {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySupplyOfResponse{
Amount: sdk.Coin{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -249,7 +250,7 @@ func (s *CLITestSuite) TestGetCmdQueryTotalSupply() {
{
"invalid query with denom",
func() client.Context {
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Code: 1,
})
return s.baseCtx.WithClient(c)
Expand Down Expand Up @@ -305,7 +306,7 @@ func (s *CLITestSuite) TestGetCmdQuerySendEnabled() {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySendEnabledResponse{
SendEnabled: []*types.SendEnabled{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand All @@ -322,7 +323,7 @@ func (s *CLITestSuite) TestGetCmdQuerySendEnabled() {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySendEnabledResponse{
SendEnabled: []*types.SendEnabled{},
})
c := newMockTendermintRPC(abci.ResponseQuery{
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
Expand Down
33 changes: 2 additions & 31 deletions x/bank/client/cli/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
package cli_test

import (
"context"
"io"
"testing"

"github.com/stretchr/testify/suite"
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/crypto/keyring"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/bank"
)

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) BroadcastTxSync(context.Context, tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{Code: 0}, nil
}

func (m mockTendermintRPC) ABCIQueryWithOptions(
_ context.Context,
_ string, _ bytes.HexBytes,
_ rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error) {
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
}

type CLITestSuite struct {
suite.Suite

Expand All @@ -62,7 +33,7 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard)
}
15 changes: 2 additions & 13 deletions x/crisis/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"io"
"testing"

clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
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"
Expand All @@ -22,24 +21,14 @@ import (
"github.com/cosmos/cosmos-sdk/x/crisis/client/cli"
)

var _ client.TendermintRPC = (*mockTendermintRPC)(nil)

type mockTendermintRPC struct {
rpcclientmock.Client
}

func (mockTendermintRPC) BroadcastTxSync(context.Context, tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{}, nil
}

func TestNewMsgVerifyInvariantTxCmd(t *testing.T) {
encCfg := testutilmod.MakeTestEncodingConfig(crisis.AppModuleBasic{})
kr := keyring.NewInMemory(encCfg.Codec)
baseCtx := client.Context{}.
WithKeyring(kr).
WithTxConfig(encCfg.TxConfig).
WithCodec(encCfg.Codec).
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")
Expand Down
Loading

0 comments on commit 08be7ee

Please sign in to comment.