Skip to content

Commit

Permalink
test: add TxContext test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Oct 31, 2023
1 parent accbc76 commit 59f0c90
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
58 changes: 58 additions & 0 deletions internal/testclient/common.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
package testclient

import (
"os"
"path/filepath"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"

"pocket/app"
"pocket/cmd/pocketd/cmd"
)

const CometLocalWebsocketURL = "ws://localhost:36657/websocket"

var (
EncodingConfig = app.MakeEncodingConfig()
)

func init() {
// NB: ensure to run tests with --build test flag
cmd.InitSDKConfig()
}

func NewLocalnetClientCtx(t *testing.T, flagSet *pflag.FlagSet) *client.Context {
homedir := DefaultHomeDir()
clientCtx := client.Context{}.
WithCodec(EncodingConfig.Marshaler).
WithTxConfig(EncodingConfig.TxConfig).
WithHomeDir(homedir).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithInterfaceRegistry(EncodingConfig.InterfaceRegistry)

clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, flagSet)
require.NoError(t, err)
return &clientCtx
}

func NewLocalnetFlagSet(t *testing.T) *pflag.FlagSet {
mockFlagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
mockFlagSet.String(flags.FlagNode, "tcp://127.0.0.1:36657", "use localnet poktrolld node")
mockFlagSet.String(flags.FlagHome, "", "use localnet poktrolld node")
mockFlagSet.String(flags.FlagKeyringBackend, "test", "use test keyring")
err := mockFlagSet.Parse([]string{})
require.NoError(t, err)

return mockFlagSet
}

func DefaultHomeDir() string {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}

return filepath.Join(userHomeDir, "."+app.Name)
}
27 changes: 27 additions & 0 deletions internal/testclient/keyring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testclient

import (
cosmoshd "github.com/cosmos/cosmos-sdk/crypto/hd"
cosmoskeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/stretchr/testify/require"
"testing"
)

func NewKey(
t *testing.T, name string,
keyring cosmoskeyring.Keyring,
) (key *cosmoskeyring.Record, mnemonic string) {
t.Helper()

key, mnemonic, err := keyring.NewMnemonic(
name,
cosmoskeyring.English,
"m/44'/118'/0'/0/0",
cosmoskeyring.DefaultBIP39Passphrase,
cosmoshd.Secp256k1,
)
require.NoError(t, err)
require.NotNil(t, key)

return key, mnemonic
}
96 changes: 96 additions & 0 deletions internal/testclient/testtx/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package testtx

import (
"testing"

"cosmossdk.io/depinject"
cometbytes "github.com/cometbft/cometbft/libs/bytes"
comettypes "github.com/cometbft/cometbft/types"
cosmosclient "github.com/cosmos/cosmos-sdk/client"
cosmostx "github.com/cosmos/cosmos-sdk/client/tx"
cosmoskeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"pocket/internal/mocks/mockclient"
"pocket/internal/testclient"
"pocket/pkg/client"
"pocket/pkg/client/tx"
)

func NewOneTimeTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
signingKeyName string,
expectedTx *cometbytes.HexBytes,
expectedTxHash *cometbytes.HexBytes,
) *mockclient.MockTxContext {
t.Helper()

txCtxMock, txCtx := NewAnyTimesTxTxContext(t, keyring)
txCtxMock.EXPECT().NewTxBuilder().
DoAndReturn(txCtx.NewTxBuilder).
Times(1)
txCtxMock.EXPECT().SignTx(
gomock.Eq(signingKeyName),
gomock.AssignableToTypeOf(txCtx.NewTxBuilder()),
gomock.Eq(false), gomock.Eq(false),
).DoAndReturn(txCtx.SignTx).Times(1)
txCtxMock.EXPECT().EncodeTx(gomock.Any()).
DoAndReturn(func(txBuilder cosmosclient.TxBuilder) (_ []byte, err error) {
// intercept cosmosTxContext#EncodeTx to get the encoded tx cometbytes
*expectedTx, err = txCtx.EncodeTx(txBuilder)
*expectedTxHash = comettypes.Tx(expectedTx.Bytes()).Hash()
require.NoError(t, err)
return expectedTx.Bytes(), nil
}).Times(1)
// intercept #BroadcastTxSync() call to mock response and prevent actual broadcast
txCtxMock.EXPECT().BroadcastTxSync(gomock.Any()).
DoAndReturn(func(txBytes []byte) (*cosmostypes.TxResponse, error) {
return &cosmostypes.TxResponse{
Height: 1,
TxHash: expectedTxHash.String(),
RawLog: "",
Logs: nil,
Tx: nil,
Timestamp: "",
Events: nil,
}, nil
}).Times(1)

return txCtxMock
}

func NewAnyTimesTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
) (*mockclient.MockTxContext, client.TxContext) {
t.Helper()
var (
ctrl = gomock.NewController(t)
flagSet = testclient.NewLocalnetFlagSet(t)
)

// intercept #GetAccountNumberSequence() call to mock response and prevent actual query
accountRetrieverMock := mockclient.NewMockAccountRetriever(ctrl)
accountRetrieverMock.EXPECT().GetAccountNumberSequence(gomock.Any(), gomock.Any()).
Return(uint64(1), uint64(1), nil).
AnyTimes()

clientCtx := testclient.NewLocalnetClientCtx(t, flagSet).
WithKeyring(keyring).
WithAccountRetriever(accountRetrieverMock)

txFactory, err := cosmostx.NewFactoryCLI(clientCtx, flagSet)
require.NoError(t, err)
require.NotEmpty(t, txFactory)

txCtxDeps := depinject.Supply(txFactory, clientCtx)
txCtx, err := tx.NewTxContext(txCtxDeps)
require.NoError(t, err)
txCtxMock := mockclient.NewMockTxContext(ctrl)
txCtxMock.EXPECT().GetKeyring().Return(keyring).AnyTimes()

return txCtxMock, txCtx
}
6 changes: 5 additions & 1 deletion pkg/client/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//go:generate mockgen -destination=../../internal/mocks/mockclient/query_client_mock.go -package=mockclient . Dialer,Connection
//go:generate mockgen -destination=../../internal/mocks/mockclient/events_query_client_mock.go -package=mockclient . Dialer,Connection
//go:generate mockgen -destination=../../internal/mocks/mockclient/tx_client_mock.go -package=mockclient . TxContext
//go:generate mockgen -destination=../../internal/mocks/mockclient/cosmos_tx_builder_mock.go -package=mockclient github.com/cosmos/cosmos-sdk/client TxBuilder
//go:generate mockgen -destination=../../internal/mocks/mockclient/cosmos_keyring_mock.go -package=mockclient github.com/cosmos/cosmos-sdk/crypto/keyring Keyring
//go:generate mockgen -destination=../../internal/mocks/mockclient/cosmos_client_mock.go -package=mockclient github.com/cosmos/cosmos-sdk/client AccountRetriever

package client

Expand Down

0 comments on commit 59f0c90

Please sign in to comment.