## Description Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package baseapp
|
|
|
|
import (
|
|
gocontext "context"
|
|
"fmt"
|
|
|
|
gogogrpc "github.com/gogo/protobuf/grpc"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
// QueryServiceTestHelper provides a helper for making grpc query service
|
|
// rpc calls in unit tests. It implements both the grpc Server and ClientConn
|
|
// interfaces needed to register a query service server and create a query
|
|
// service client.
|
|
type QueryServiceTestHelper struct {
|
|
*GRPCQueryRouter
|
|
Ctx sdk.Context
|
|
}
|
|
|
|
var (
|
|
_ gogogrpc.Server = &QueryServiceTestHelper{}
|
|
_ gogogrpc.ClientConn = &QueryServiceTestHelper{}
|
|
)
|
|
|
|
// NewQueryServerTestHelper creates a new QueryServiceTestHelper that wraps
|
|
// the provided sdk.Context
|
|
func NewQueryServerTestHelper(ctx sdk.Context, interfaceRegistry types.InterfaceRegistry) *QueryServiceTestHelper {
|
|
qrt := NewGRPCQueryRouter()
|
|
qrt.SetInterfaceRegistry(interfaceRegistry)
|
|
return &QueryServiceTestHelper{GRPCQueryRouter: qrt, Ctx: ctx}
|
|
}
|
|
|
|
// Invoke implements the grpc ClientConn.Invoke method
|
|
func (q *QueryServiceTestHelper) Invoke(_ gocontext.Context, method string, args, reply interface{}, _ ...grpc.CallOption) error {
|
|
querier := q.Route(method)
|
|
if querier == nil {
|
|
return fmt.Errorf("handler not found for %s", method)
|
|
}
|
|
reqBz, err := q.cdc.Marshal(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := querier(q.Ctx, abci.RequestQuery{Data: reqBz})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = q.cdc.Unmarshal(res.Value, reply)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// NewStream implements the grpc ClientConn.NewStream method
|
|
func (q *QueryServiceTestHelper) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
|
|
return nil, fmt.Errorf("not supported")
|
|
}
|