Skip to content

Commit

Permalink
feat: query project info (#841)
Browse files Browse the repository at this point in the history
Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com>
  • Loading branch information
technicallyty and technicallyty authored Mar 9, 2022
1 parent ba44c7e commit 6b8229f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions x/ecocredit/server/core/query_project_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core

import (
"context"
v1 "github.com/regen-network/regen-ledger/x/ecocredit/v1"
)

// ProjectInfo queries project info from the given project name.
func (k Keeper) ProjectInfo(ctx context.Context, request *v1.QueryProjectInfoRequest) (*v1.QueryProjectInfoResponse, error) {
pInfo, err := k.stateStore.ProjectInfoStore().GetByName(ctx, request.ProjectId)
if err != nil {
return nil, err
}
var pi v1.ProjectInfo
if err = PulsarToGogoSlow(pInfo, &pi); err != nil {
return nil, err
}
return &v1.QueryProjectInfoResponse{Info: &pi}, nil
}
32 changes: 32 additions & 0 deletions x/ecocredit/server/core/query_project_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core

import (
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
ecocreditv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
v1 "github.com/regen-network/regen-ledger/x/ecocredit/v1"
"gotest.tools/v3/assert"
"testing"
)

func TestQuery_ProjectInfo(t *testing.T) {
t.Parallel()
s := setupBase(t)

// insert 1 project
err := s.stateStore.ProjectInfoStore().Insert(s.ctx, &ecocreditv1.ProjectInfo{
Name: "P01",
ClassId: 1,
ProjectLocation: "US-CA",
Metadata: nil,
})
assert.NilError(t, err)

// valid query
res, err := s.k.ProjectInfo(s.ctx, &v1.QueryProjectInfoRequest{ProjectId: "P01"})
assert.NilError(t, err)
assert.Equal(t, "P01", res.Info.Name)

// invalid query
_, err = s.k.ProjectInfo(s.ctx, &v1.QueryProjectInfoRequest{ProjectId: "F01"})
assert.ErrorContains(t, err, ormerrors.NotFound.Error())
}

0 comments on commit 6b8229f

Please sign in to comment.