2020-10-29 08:32:47 -07:00
|
|
|
package baseapp_test
|
2020-06-03 06:33:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-29 08:32:47 -07:00
|
|
|
"os"
|
2020-06-03 06:33:51 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-29 08:32:47 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
2020-06-03 06:33:51 -07:00
|
|
|
|
2020-10-29 08:32:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2020-09-21 09:48:28 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
2020-10-29 08:32:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2020-07-20 05:30:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2020-06-03 06:33:51 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2020-12-04 07:06:50 -08:00
|
|
|
func TestGRPCGatewayRouter(t *testing.T) {
|
2020-10-29 08:32:47 -07:00
|
|
|
qr := baseapp.NewGRPCQueryRouter()
|
2020-07-03 09:42:12 -07:00
|
|
|
interfaceRegistry := testdata.NewTestInterfaceRegistry()
|
2020-08-17 10:02:13 -07:00
|
|
|
qr.SetInterfaceRegistry(interfaceRegistry)
|
2020-10-15 06:07:59 -07:00
|
|
|
testdata.RegisterQueryServer(qr, testdata.QueryImpl{})
|
2020-10-29 08:32:47 -07:00
|
|
|
helper := &baseapp.QueryServiceTestHelper{
|
2020-06-04 11:48:06 -07:00
|
|
|
GRPCQueryRouter: qr,
|
2020-10-29 08:32:47 -07:00
|
|
|
Ctx: sdk.Context{}.WithContext(context.Background()),
|
2020-06-03 06:33:51 -07:00
|
|
|
}
|
2020-10-15 06:07:59 -07:00
|
|
|
client := testdata.NewQueryClient(helper)
|
2020-06-03 06:33:51 -07:00
|
|
|
|
|
|
|
res, err := client.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
require.Equal(t, "hello", res.Message)
|
|
|
|
|
|
|
|
require.Panics(t, func() {
|
|
|
|
_, _ = client.Echo(context.Background(), nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
res2, err := client.SayHello(context.Background(), &testdata.SayHelloRequest{Name: "Foo"})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
require.Equal(t, "Hello Foo!", res2.Greeting)
|
2020-07-03 09:42:12 -07:00
|
|
|
|
|
|
|
spot := &testdata.Dog{Name: "Spot", Size_: "big"}
|
|
|
|
any, err := types.NewAnyWithValue(spot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
res3, err := client.TestAny(context.Background(), &testdata.TestAnyRequest{AnyAnimal: any})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res3)
|
|
|
|
require.Equal(t, spot, res3.HasAnimal.Animal.GetCachedValue())
|
2020-06-03 06:33:51 -07:00
|
|
|
}
|
2020-10-29 08:32:47 -07:00
|
|
|
|
|
|
|
func TestRegisterQueryServiceTwice(t *testing.T) {
|
|
|
|
// Setup baseapp.
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
encCfg := simapp.MakeTestEncodingConfig()
|
|
|
|
app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, encCfg.TxConfig.TxDecoder())
|
|
|
|
app.SetInterfaceRegistry(encCfg.InterfaceRegistry)
|
|
|
|
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)
|
|
|
|
|
|
|
|
// First time registering service shouldn't panic.
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
testdata.RegisterQueryServer(
|
|
|
|
app.GRPCQueryRouter(),
|
|
|
|
testdata.QueryImpl{},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Second time should panic.
|
|
|
|
require.Panics(t, func() {
|
|
|
|
testdata.RegisterQueryServer(
|
|
|
|
app.GRPCQueryRouter(),
|
|
|
|
testdata.QueryImpl{},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|