Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add CLI, gRPC, MsgServer tests #1405

Merged
merged 14 commits into from
Jun 25, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/fbridge) [\#1395](https://github.com/Finschia/finschia-sdk/pull/1395) Return error instead of panic for behaviors triggered by client
* (x/fswap) [\#1396](https://github.com/Finschia/finschia-sdk/pull/1396) refactor to use snake_case in proto
* (x/fswap) [\#1391](https://github.com/Finschia/finschia-sdk/pull/1391) add cli_test for fswap module
* (x/fbridge) [\#1405](https://github.com/Finschia/finschia-sdk/pull/1405) Add CLI, gRPC, MsgServer tests
* (x/fswap) [\#1415](https://github.com/Finschia/finschia-sdk/pull/1415) add more testcases for fswap module

### Bug Fixes
Expand Down
28 changes: 28 additions & 0 deletions client/tendermint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

import (
"context"

rpcclient "github.com/tendermint/tendermint/rpc/client"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
)

// TendermintRPC defines the interface of a Tendermint RPC client needed for
// queries and transaction handling.
type TendermintRPC interface {
rpcclient.ABCIClient

Validators(ctx context.Context, height *int64, page, perPage *int) (*coretypes.ResultValidators, error)
Status(context.Context) (*coretypes.ResultStatus, error)
Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error)
BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*coretypes.ResultBlockchainInfo, error)
Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error)
Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error)
TxSearch(
ctx context.Context,
query string,
prove bool,
page, perPage *int,
orderBy string,
) (*coretypes.ResultTxSearch, error)
}
Empty file modified init_node.sh
100644 → 100755
Empty file.
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/Finschia/finschia-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}

Check warning on line 27 in testutil/cli/tm_mocks.go

View check run for this annotation

Codecov / codecov/patch

testutil/cli/tm_mocks.go#L26-L27

Added lines #L26 - L27 were not covered by tests
}

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

Check warning on line 31 in testutil/cli/tm_mocks.go

View check run for this annotation

Codecov / codecov/patch

testutil/cli/tm_mocks.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}

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

Check warning on line 40 in testutil/cli/tm_mocks.go

View check run for this annotation

Codecov / codecov/patch

testutil/cli/tm_mocks.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}
78 changes: 78 additions & 0 deletions types/module/testutil/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package testutil

import (
"github.com/Finschia/finschia-sdk/client"
"github.com/Finschia/finschia-sdk/codec"
"github.com/Finschia/finschia-sdk/codec/types"
"github.com/Finschia/finschia-sdk/std"
"github.com/Finschia/finschia-sdk/types/module"
"github.com/Finschia/finschia-sdk/x/auth/tx"
)

// TestEncodingConfig defines an encoding configuration that is used for testing
// purposes. Note, MakeTestEncodingConfig takes a series of AppModuleBasic types
// which should only contain the relevant module being tested and any potential
// dependencies.
type TestEncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}

func MakeTestEncodingConfig(modules ...module.AppModuleBasic) TestEncodingConfig {
aminoCdc := codec.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)

Check warning on line 26 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L23-L26

Added lines #L23 - L26 were not covered by tests

encCfg := TestEncodingConfig{
InterfaceRegistry: interfaceRegistry,
Codec: cdc,
TxConfig: tx.NewTxConfig(cdc, tx.DefaultSignModes),
Amino: aminoCdc,

Check warning on line 32 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L28-L32

Added lines #L28 - L32 were not covered by tests
}

mb := module.NewBasicManager(modules...)

Check warning on line 35 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L35

Added line #L35 was not covered by tests

std.RegisterLegacyAminoCodec(encCfg.Amino)
std.RegisterInterfaces(encCfg.InterfaceRegistry)
mb.RegisterLegacyAminoCodec(encCfg.Amino)
mb.RegisterInterfaces(encCfg.InterfaceRegistry)

Check warning on line 40 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L37-L40

Added lines #L37 - L40 were not covered by tests

return encCfg

Check warning on line 42 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L42

Added line #L42 was not covered by tests
}

func MakeTestTxConfig() client.TxConfig {
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
return tx.NewTxConfig(cdc, tx.DefaultSignModes)

Check warning on line 48 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L45-L48

Added lines #L45 - L48 were not covered by tests
}

type TestBuilderTxConfig struct {
client.TxConfig
TxBuilder *TestTxBuilder
}

func MakeBuilderTestTxConfig() TestBuilderTxConfig {
return TestBuilderTxConfig{
TxConfig: MakeTestTxConfig(),

Check warning on line 58 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L56-L58

Added lines #L56 - L58 were not covered by tests
}
}

func (cfg TestBuilderTxConfig) NewTxBuilder() client.TxBuilder {
if cfg.TxBuilder == nil {
cfg.TxBuilder = &TestTxBuilder{
TxBuilder: cfg.TxConfig.NewTxBuilder(),

Check warning on line 65 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L62-L65

Added lines #L62 - L65 were not covered by tests
}
}
return cfg.TxBuilder

Check warning on line 68 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L68

Added line #L68 was not covered by tests
}

type TestTxBuilder struct {
client.TxBuilder
ExtOptions []*types.Any
}

func (b *TestTxBuilder) SetExtensionOptions(extOpts ...*types.Any) {
b.ExtOptions = extOpts

Check warning on line 77 in types/module/testutil/codec.go

View check run for this annotation

Codecov / codecov/patch

types/module/testutil/codec.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}
6 changes: 3 additions & 3 deletions x/fbridge/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
flagSequences = "sequences"
FlagSequences = "sequences"
)

// NewQueryCmd returns the query commands for fbridge module
Expand Down Expand Up @@ -100,7 +100,7 @@ func NewQuerySeqToBlocknumsCmd() *cobra.Command {
}
qc := types.NewQueryClient(clientCtx)

seqSlice, err := cmd.Flags().GetInt64Slice(flagSequences)
seqSlice, err := cmd.Flags().GetInt64Slice(FlagSequences)
if err != nil {
return err
}
Expand All @@ -119,7 +119,7 @@ func NewQuerySeqToBlocknumsCmd() *cobra.Command {
},
}

cmd.Flags().Int64Slice(flagSequences, []int64{}, "comma separated list of bridge sequnece numbers")
cmd.Flags().Int64Slice(FlagSequences, []int64{}, "comma separated list of bridge sequnece numbers")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
Expand Down
Loading
Loading