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{ map[string]string{
grpctypes.GRPCBlockHeightHeader: "1", grpctypes.GRPCBlockHeightHeader: "1",
}, },
true, false,
}, },
{ {
"test query validators gRPC route with valid status", "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") return nil, status.Error(codes.InvalidArgument, "empty request")
} }
if req.Status == "" { // validate the provided status, return all the validators if the status is empty
return nil, status.Error(codes.InvalidArgument, "status cannot be empty") if req.Status != "" && !(req.Status == sdk.Bonded.String() || req.Status == sdk.Unbonded.String() || req.Status == sdk.Unbonding.String()) {
}
if !(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) return nil, status.Errorf(codes.InvalidArgument, "invalid validator status %s", req.Status)
} }
var validators types.Validators var validators types.Validators
ctx := sdk.UnwrapSDKContext(c) ctx := sdk.UnwrapSDKContext(c)
@ -51,6 +50,7 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest
if accumulate { if accumulate {
validators = append(validators, val) validators = append(validators, val)
} }
return true, nil return true, nil
}) })

View File

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