forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing(json rpc): Add backend test suite with mock grpc query client (…
…evmos#1199) * tests(json-rpc): wip evm_backend unit test setup * tests(json-rpc): wip evm_backend unit test setup * fix viper * wip query client mock * fix first backend test except error message * clean up * wip Context with Height * fix JSON RPC backend test setup * typo * refactor folder structure * Update rpc/backend/evm_backend_test.go Co-authored-by: Federico Kunze Küllmer <[email protected]>
- Loading branch information
1 parent
7c423b0
commit 8fbb286
Showing
4 changed files
with
497 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)). | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.