-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathrouter_test.go
70 lines (57 loc) · 2.54 KB
/
router_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package runtime_test
import (
"testing"
"github.com/stretchr/testify/require"
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
coretesting "cosmossdk.io/core/testing"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
counterkeeper "github.com/cosmos/cosmos-sdk/testutil/x/counter/keeper"
countertypes "github.com/cosmos/cosmos-sdk/testutil/x/counter/types"
)
func TestRouterService(t *testing.T) {
interfaceRegistry := codectypes.NewInterfaceRegistry()
msgRouter := baseapp.NewMsgServiceRouter()
msgRouter.SetInterfaceRegistry(interfaceRegistry)
queryRouter := baseapp.NewGRPCQueryRouter()
queryRouter.SetInterfaceRegistry(interfaceRegistry)
key := storetypes.NewKVStoreKey(countertypes.StoreKey)
storeService := runtime.NewKVStoreService(key)
counterKeeper := counterkeeper.NewKeeper(runtime.NewEnvironment(storeService, coretesting.NewNopLogger()))
countertypes.RegisterInterfaces(interfaceRegistry)
countertypes.RegisterMsgServer(msgRouter, counterKeeper)
countertypes.RegisterQueryServer(queryRouter, counterKeeper)
messageRouterService := runtime.NewMsgRouterService(msgRouter)
queryRouterService := runtime.NewQueryRouterService(queryRouter)
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
// Messages
t.Run("invalid msg", func(t *testing.T) {
_, err := messageRouterService.Invoke(testCtx.Ctx, &bankv1beta1.MsgSend{})
require.ErrorContains(t, err, "could not find response type for message cosmos.bank.v1beta1.MsgSend")
})
t.Run("invoke: valid msg (proto v1)", func(t *testing.T) {
resp, err := messageRouterService.Invoke(testCtx.Ctx, &countertypes.MsgIncreaseCounter{
Signer: "cosmos1",
Count: 42,
})
require.NoError(t, err)
require.NotNil(t, resp)
})
// Queries
t.Run("invalid query", func(t *testing.T) {
_, err := queryRouterService.Invoke(testCtx.Ctx, &bankv1beta1.QueryBalanceRequest{})
require.ErrorContains(t, err, "could not find response type for request cosmos.bank.v1beta1.QueryBalanceRequest")
})
t.Run("invoke: valid query (proto v1)", func(t *testing.T) {
_ = counterKeeper.CountStore.Set(testCtx.Ctx, 42)
resp, err := queryRouterService.Invoke(testCtx.Ctx, &countertypes.QueryGetCountRequest{})
require.NoError(t, err)
require.NotNil(t, resp)
respVal, ok := resp.(*countertypes.QueryGetCountResponse)
require.True(t, ok)
require.Equal(t, int64(42), respVal.TotalCount)
})
}