Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

testing(json rpc): Add backend test suite with mock grpc query client #1199

Merged
merged 12 commits into from
Jul 23, 2022
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ require (
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tendermint/btcd v0.1.1 // indirect
Expand Down
73 changes: 73 additions & 0 deletions rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package backend

import (
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"github.com/evmos/ethermint/rpc/backend/mocks"
rpc "github.com/evmos/ethermint/rpc/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

type BackendTestSuite struct {
suite.Suite
backend *Backend
}

func TestBackendTestSuite(t *testing.T) {
suite.Run(t, new(BackendTestSuite))
}

// SetupTest is executed before every BackendTestSuite test
func (suite *BackendTestSuite) SetupTest() {
ctx := server.NewDefaultContext()
ctx.Viper.Set("telemetry.global-labels", []interface{}{})
clientCtx := client.Context{}.WithChainID("ethermint_9000-1").WithHeight(1)
allowUnprotectedTxs := false

suite.backend = NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs)

queryClient := mocks.NewQueryClient(suite.T())
var header metadata.MD
RegisterMockQueries(queryClient, &header)

suite.backend.queryClient.QueryClient = queryClient
suite.backend.ctx = rpc.ContextWithHeight(1)
}

// QueryClient defines a mocked object that implements the grpc QueryCLient
// interface. It's used on tests to test the JSON-RPC without running a grpc
// client server. E.g. JSON-PRC-CLIENT -> BACKEND -> Mock GRPC CLIENT -> APP
var _ evmtypes.QueryClient = &mocks.QueryClient{}

// RegisterMockQueries registers the queries and their respective responses,
// so that they can be called in tests using the queryClient
func RegisterMockQueries(queryClient *mocks.QueryClient, header *metadata.MD) {
queryClient.On("Params", rpc.ContextWithHeight(1), &evmtypes.QueryParamsRequest{}, grpc.Header(header)).
danburck marked this conversation as resolved.
Show resolved Hide resolved
Return(&evmtypes.QueryParamsResponse{}, nil).
Run(func(args mock.Arguments) {
// If Params call is successful, also update the header height
arg := args.Get(2).(grpc.HeaderCallOption)
h := metadata.MD{}
h.Set(grpctypes.GRPCBlockHeightHeader, "1")
*arg.HeaderAddr = h
})
}

func TestQueryClient(t *testing.T) {
queryClient := mocks.NewQueryClient(t)
var header metadata.MD
RegisterMockQueries(queryClient, &header)

// mock calls for abstraction
_, err := queryClient.Params(rpc.ContextWithHeight(1), &evmtypes.QueryParamsRequest{}, grpc.Header(&header))
require.NoError(t, err)
}
30 changes: 30 additions & 0 deletions rpc/backend/evm_backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package backend

import "github.com/ethereum/go-ethereum/common/hexutil"

func (suite *BackendTestSuite) TestBlockNumber() {
testCases := []struct {
mame string
malleate func()
expBlockNumber hexutil.Uint64
expPass bool
}{
{
"pass",
func() {
},
0x1,
true,
},
}
for _, tc := range testCases {
blockNumber, err := suite.backend.BlockNumber()

if tc.expPass {
suite.Require().NoError(err)
suite.Require().Equal(tc.expBlockNumber, blockNumber)
} else {
suite.Require().Error(err)
}
}
}
Loading