Skip to content

Commit

Permalink
* adjusted to an array of distributions
Browse files Browse the repository at this point in the history
* added test cases

Signed-off-by: Jürgen Eckel <[email protected]>
  • Loading branch information
eckelj committed Nov 28, 2024
1 parent 2a7e82b commit 074b109
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 103 deletions.
2 changes: 1 addition & 1 deletion proto/planetmintgo/dao/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ message QueryDistributionsRequest {
}

message QueryDistributionsResponse {
DistributionOrder distributions = 1;
repeated DistributionOrder distributions = 1 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

20 changes: 16 additions & 4 deletions x/dao/keeper/query_distributions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/planetmint/planetmint-go/errormsg"
"github.com/planetmint/planetmint-go/x/dao/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) Distributions(goCtx context.Context, req *types.QueryDistributionsRequest) (*types.QueryDistributionsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
return nil, status.Error(codes.InvalidArgument, errormsg.InvalidRequest)
}

ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Process the query
_ = ctx
distributions := make([]types.DistributionOrder, 0)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DistributionKey))
pageRes, err := query.Paginate(store, req.Pagination, func(_ []byte, value []byte) (err error) {
var distribution types.DistributionOrder
err = distribution.Unmarshal(value)
distributions = append(distributions, distribution)
return
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}

return &types.QueryDistributionsResponse{}, nil
return &types.QueryDistributionsResponse{Distributions: distributions, Pagination: pageRes}, nil
}
87 changes: 87 additions & 0 deletions x/dao/keeper/query_distributions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
keepertest "github.com/planetmint/planetmint-go/testutil/keeper"
"github.com/planetmint/planetmint-go/util"
"github.com/planetmint/planetmint-go/x/dao/types"
"github.com/stretchr/testify/require"
)

func TestQueryDistribtions(t *testing.T) {
keeper, ctx := keepertest.DaoKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
//var popEpochs int64 = 24

Check failure on line 17 in x/dao/keeper/query_distributions_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (.)

commentFormatting: put a space between `//` and comment text (gocritic)
distributions := createNDistributionOrder(keeper, ctx, 20)
for _, tc := range []struct {
desc string
request *types.QueryDistributionsRequest
responseDistributions []types.DistributionOrder
err error
}{
{
desc: "query 0 offset 10 limit",
request: &types.QueryDistributionsRequest{
Pagination: &query.PageRequest{
Key: nil,
Offset: 0,
Limit: 10,
CountTotal: true,
Reverse: false,
},
},
responseDistributions: distributions[:10],
},
{
desc: "query 1 offset 5 limit",
request: &types.QueryDistributionsRequest{
Pagination: &query.PageRequest{
Key: nil,
Offset: 1,
Limit: 5,
CountTotal: true,
Reverse: false,
},
},
responseDistributions: distributions[1:6],
},
{
desc: "query 5*1000 key 0 offset 10 limit",
request: &types.QueryDistributionsRequest{
Pagination: &query.PageRequest{
Key: util.SerializeInt64(5 * 1000),
Offset: 0,
Limit: 10,
CountTotal: true,
Reverse: false,
},
},
responseDistributions: distributions[4:14],
},
{
desc: "query 2*1000 key 0 offset 10 limit",
request: &types.QueryDistributionsRequest{
Pagination: &query.PageRequest{
Key: util.SerializeInt64(2 * 1000),
Offset: 0,
Limit: 10,
CountTotal: true,
Reverse: false,
},
},
responseDistributions: distributions[1:11],
},
} {
t.Run(tc.desc, func(t *testing.T) {
res, err := keeper.Distributions(wctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, tc.err, err)
} else {
require.Equal(t, tc.responseDistributions, res.Distributions)
}
})
}
}
Loading

0 comments on commit 074b109

Please sign in to comment.