github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/baseapp/grpcrouter_test.go (about) 1 package baseapp_test 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 fbexchaincodec "github.com/fibonacci-chain/fbc/app/codec" 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/simapp" 10 simapp2 "github.com/fibonacci-chain/fbc/libs/ibc-go/testing/simapp" 11 "github.com/fibonacci-chain/fbc/x/evm" 12 13 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 14 dbm "github.com/fibonacci-chain/fbc/libs/tm-db" 15 "github.com/stretchr/testify/require" 16 17 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/baseapp" 18 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types" 19 20 //"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/simapp" 21 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 22 "github.com/fibonacci-chain/fbc/x/evm/types/testdata" 23 ) 24 25 func TestGRPCGatewayRouter(t *testing.T) { 26 qr := baseapp.NewGRPCQueryRouter() 27 interfaceRegistry := testdata.NewTestInterfaceRegistry() 28 qr.SetInterfaceRegistry(interfaceRegistry) 29 testdata.RegisterQueryServer(qr, testdata.QueryImpl{}) 30 helper := &baseapp.QueryServiceTestHelper{ 31 GRPCQueryRouter: qr, 32 Ctx: *(&sdk.Context{}).SetContext(context.Background()), 33 } 34 client := testdata.NewQueryClient(helper) 35 36 res, err := client.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"}) 37 require.Nil(t, err) 38 require.NotNil(t, res) 39 require.Equal(t, "hello", res.Message) 40 41 require.Panics(t, func() { 42 _, _ = client.Echo(context.Background(), nil) 43 }) 44 45 res2, err := client.SayHello(context.Background(), &testdata.SayHelloRequest{Name: "Foo"}) 46 require.Nil(t, err) 47 require.NotNil(t, res) 48 require.Equal(t, "Hello Foo!", res2.Greeting) 49 50 spot := &testdata.Dog{Name: "Spot", Size_: "big"} 51 any, err := types.NewAnyWithValue(spot) 52 require.NoError(t, err) 53 res3, err := client.TestAny(context.Background(), &testdata.TestAnyRequest{AnyAnimal: any}) 54 require.NoError(t, err) 55 require.NotNil(t, res3) 56 require.Equal(t, spot, res3.HasAnimal.Animal.GetCachedValue()) 57 } 58 59 func TestRegisterQueryServiceTwice(t *testing.T) { 60 // Setup baseapp. 61 db := dbm.NewMemDB() 62 encCfg := simapp2.MakeTestEncodingConfig() 63 codecProxy, _ := fbexchaincodec.MakeCodecSuit(simapp.ModuleBasics) 64 app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, evm.TxDecoder(codecProxy)) 65 app.SetInterfaceRegistry(encCfg.InterfaceRegistry) 66 testdata.RegisterInterfaces(encCfg.InterfaceRegistry) 67 68 // First time registering service shouldn't panic. 69 require.NotPanics(t, func() { 70 testdata.RegisterQueryServer( 71 app.GRPCQueryRouter(), 72 testdata.QueryImpl{}, 73 ) 74 }) 75 76 // Second time should panic. 77 require.Panics(t, func() { 78 testdata.RegisterQueryServer( 79 app.GRPCQueryRouter(), 80 testdata.QueryImpl{}, 81 ) 82 }) 83 }