github.com/cosmos/cosmos-sdk@v0.50.10/x/params/keeper/grpc_query_test.go (about) 1 package keeper_test 2 3 import ( 4 "fmt" 5 6 "github.com/cosmos/cosmos-sdk/x/params/types" 7 "github.com/cosmos/cosmos-sdk/x/params/types/proposal" 8 ) 9 10 func (suite *KeeperTestSuite) TestGRPCQueryParams() { 11 var ( 12 req *proposal.QueryParamsRequest 13 expValue string 14 space types.Subspace 15 ) 16 key := []byte("key") 17 18 testCases := []struct { 19 msg string 20 malleate func() 21 expPass bool 22 }{ 23 { 24 "empty request", 25 func() { 26 req = &proposal.QueryParamsRequest{} 27 }, 28 false, 29 }, 30 { 31 "invalid request with subspace not found", 32 func() { 33 req = &proposal.QueryParamsRequest{Subspace: "test"} 34 }, 35 false, 36 }, 37 { 38 "invalid request with subspace and key not found", 39 func() { 40 req = &proposal.QueryParamsRequest{Subspace: "test", Key: "key"} 41 }, 42 false, 43 }, 44 { 45 "success", 46 func() { 47 space = suite.paramsKeeper.Subspace("test"). 48 WithKeyTable(types.NewKeyTable(types.NewParamSetPair(key, paramJSON{}, validateNoOp))) 49 req = &proposal.QueryParamsRequest{Subspace: "test", Key: "key"} 50 expValue = "" 51 }, 52 true, 53 }, 54 { 55 "update value success", 56 func() { 57 err := space.Update(suite.ctx, key, []byte(`{"param1":"10241024"}`)) 58 suite.Require().NoError(err) 59 req = &proposal.QueryParamsRequest{Subspace: "test", Key: "key"} 60 expValue = `{"param1":"10241024"}` 61 }, 62 true, 63 }, 64 } 65 66 suite.SetupTest() 67 68 for _, tc := range testCases { 69 suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { 70 tc.malleate() 71 72 res, err := suite.queryClient.Params(suite.ctx, req) 73 74 if tc.expPass { 75 suite.Require().NoError(err) 76 suite.Require().NotNil(res) 77 suite.Require().Equal(expValue, res.Param.Value) 78 } else { 79 suite.Require().Error(err) 80 suite.Require().Nil(res) 81 } 82 }) 83 } 84 } 85 86 func (suite *KeeperTestSuite) TestGRPCQuerySubspaces() { 87 // NOTE: Each subspace will not have any keys that we can check against 88 // because InitGenesis has not been called during app construction. 89 resp, err := suite.queryClient.Subspaces(suite.ctx, &proposal.QuerySubspacesRequest{}) 90 suite.Require().NoError(err) 91 suite.Require().NotNil(resp) 92 93 spaces := make([]string, len(resp.Subspaces)) 94 i := 0 95 for _, ss := range resp.Subspaces { 96 spaces[i] = ss.Subspace 97 i++ 98 } 99 100 // require the response contains a few subspaces we know exist 101 suite.Require().Contains(spaces, "bank") 102 suite.Require().Contains(spaces, "staking") 103 }