Fix `status` filter (#7167)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Anil Kumar Kammari 2020-08-26 13:12:25 +05:30 committed by GitHub
parent a180d0b0ea
commit b5bc864862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 8 deletions

View File

@ -62,7 +62,7 @@ func (s *IntegrationTestSuite) TestQueryValidatorsGRPCHandler() {
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
true,
false,
},
{
"test query validators gRPC route with valid status",

View File

@ -26,12 +26,11 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.Status == "" {
return nil, status.Error(codes.InvalidArgument, "status cannot be empty")
}
if !(req.Status == sdk.Bonded.String() || req.Status == sdk.Unbonded.String() || req.Status == sdk.Unbonding.String()) {
// validate the provided status, return all the validators if the status is empty
if req.Status != "" && !(req.Status == sdk.Bonded.String() || req.Status == sdk.Unbonded.String() || req.Status == sdk.Unbonding.String()) {
return nil, status.Errorf(codes.InvalidArgument, "invalid validator status %s", req.Status)
}
var validators types.Validators
ctx := sdk.UnwrapSDKContext(c)
@ -51,6 +50,7 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest
if accumulate {
validators = append(validators, val)
}
return true, nil
})

View File

@ -18,18 +18,25 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
msg string
malleate func()
expPass bool
numVals int
hasNext bool
}{
{
"empty request",
func() {
req = &types.QueryValidatorsRequest{}
},
true,
len(vals),
false,
},
{"invalid request with empty status",
{"empty status returns all the validators",
func() {
req = &types.QueryValidatorsRequest{Status: ""}
},
true,
len(vals),
false,
},
{
@ -38,6 +45,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
req = &types.QueryValidatorsRequest{Status: "test"}
},
false,
0,
false,
},
{"valid request",
func() {
@ -45,6 +54,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
Pagination: &query.PageRequest{Limit: 1, CountTotal: true}}
},
true,
1,
true,
},
}
for _, tc := range testCases {
@ -54,9 +65,14 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
if tc.expPass {
suite.NoError(err)
suite.NotNil(valsResp)
suite.Equal(1, len(valsResp.Validators))
suite.NotNil(valsResp.Pagination.NextKey)
suite.Equal(tc.numVals, len(valsResp.Validators))
suite.Equal(uint64(len(vals)), valsResp.Pagination.Total)
if (tc.hasNext) {
suite.NotNil(valsResp.Pagination.NextKey)
} else {
suite.Nil(valsResp.Pagination.NextKey)
}
} else {
suite.Require().Error(err)
}