Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/ecocredit): query Classes ORM #837

Merged
merged 5 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions x/ecocredit/server/core/query_classes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package core

import (
"context"
"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
ecocreditv1beta1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1beta1"
"github.com/regen-network/regen-ledger/x/ecocredit/v1beta1"
)

// Classes queries for all credit classes with pagination.
func (k Keeper) Classes(ctx context.Context, request *v1beta1.QueryClassesRequest) (*v1beta1.QueryClassesResponse, error) {
pg, err := GogoPageReqToPulsarPageReq(request.Pagination)
Copy link
Contributor

@anilcse anilcse Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relavant to this PR alone, but I think we should move x/ecocredit/server/core/query_utils.go to global scope types or app/utils. It smells generic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah its pretty generic, but i think we can punt it until someone needs it elsewhere

if err != nil {
return nil, err
}
it, err := k.stateStore.ClassInfoStore().List(ctx, &ecocreditv1beta1.ClassInfoPrimaryKey{}, ormlist.Paginate(pg))
if err != nil {
return nil, err
}

infos := make([]*v1beta1.ClassInfo, 0)
for it.Next() {
info, err := it.Value()
if err != nil {
return nil, err
}
var ci v1beta1.ClassInfo
if err = PulsarToGogoSlow(info, &ci); err != nil {
return nil, err
}
infos = append(infos, &ci)
}
pr, err := PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
}
return &v1beta1.QueryClassesResponse{
Classes: infos,
Pagination: pr,
}, err
}
46 changes: 46 additions & 0 deletions x/ecocredit/server/core/query_classes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package core

import (
"github.com/cosmos/cosmos-sdk/types/query"
ecocreditv1beta1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1beta1"
"github.com/regen-network/regen-ledger/x/ecocredit/v1beta1"
"gotest.tools/v3/assert"
"testing"
)

func TestQuery_Classes(t *testing.T) {
t.Parallel()
s := setupBase(t)
err := s.stateStore.ClassInfoStore().Insert(s.ctx, &ecocreditv1beta1.ClassInfo{
Name: "C01",
Admin: s.addr,
Metadata: nil,
CreditType: "C",
})
assert.NilError(t, err)
err = s.stateStore.ClassInfoStore().Insert(s.ctx, &ecocreditv1beta1.ClassInfo{
Name: "C02",
Admin: s.addr,
Metadata: nil,
CreditType: "C",
})
assert.NilError(t, err)

// requesting all
res, err := s.k.Classes(s.ctx, &v1beta1.QueryClassesRequest{})
assert.NilError(t, err)
assert.Equal(t, 2, len(res.Classes))
assert.Equal(t, "C01", res.Classes[0].Name)
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, "C02", res.Classes[1].Name)

// request with pagination
res, err = s.k.Classes(s.ctx, &v1beta1.QueryClassesRequest{Pagination: &query.PageRequest{
Limit: 1,
CountTotal: true,
Reverse: true,
}})
assert.NilError(t, err)
assert.Equal(t, 1, len(res.Classes))
assert.Equal(t, uint64(2), res.Pagination.Total)
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, "C02", res.Classes[0].Name)
}