github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/keeper/grpc_query_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	gocontext "context"
     5  	"fmt"
     6  
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  	"github.com/cosmos/cosmos-sdk/x/staking/testutil"
     9  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    10  )
    11  
    12  func (s *KeeperTestSuite) TestGRPCQueryValidator() {
    13  	ctx, keeper, queryClient := s.ctx, s.stakingKeeper, s.queryClient
    14  	require := s.Require()
    15  
    16  	validator := testutil.NewValidator(s.T(), sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0])
    17  	require.NoError(keeper.SetValidator(ctx, validator))
    18  	var req *types.QueryValidatorRequest
    19  	testCases := []struct {
    20  		msg      string
    21  		malleate func()
    22  		expPass  bool
    23  	}{
    24  		{
    25  			"empty request",
    26  			func() {
    27  				req = &types.QueryValidatorRequest{}
    28  			},
    29  			false,
    30  		},
    31  		{
    32  			"with valid and not existing address",
    33  			func() {
    34  				req = &types.QueryValidatorRequest{
    35  					ValidatorAddr: "cosmosvaloper15jkng8hytwt22lllv6mw4k89qkqehtahd84ptu",
    36  				}
    37  			},
    38  			false,
    39  		},
    40  		{
    41  			"valid request",
    42  			func() {
    43  				req = &types.QueryValidatorRequest{ValidatorAddr: validator.OperatorAddress}
    44  			},
    45  			true,
    46  		},
    47  	}
    48  
    49  	for _, tc := range testCases {
    50  		s.Run(fmt.Sprintf("Case %s", tc.msg), func() {
    51  			tc.malleate()
    52  			res, err := queryClient.Validator(gocontext.Background(), req)
    53  			if tc.expPass {
    54  				require.NoError(err)
    55  				require.True(validator.Equal(&res.Validator))
    56  			} else {
    57  				require.Error(err)
    58  				require.Nil(res)
    59  			}
    60  		})
    61  	}
    62  }