Skip to content

Commit

Permalink
feat(x/ecocredit): add Basket query (#753)
Browse files Browse the repository at this point in the history
* feat(x/ecocredit): add Basket query

* pulsar to proto

* fix test

* Update x/ecocredit/server/basket/query_basket_test.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 11, 2022
1 parent 4759669 commit 2f8412b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
20 changes: 18 additions & 2 deletions x/ecocredit/server/basket/query_basket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@ package basket

import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

func (k Keeper) Basket(ctx context.Context, request *baskettypes.QueryBasketRequest) (*baskettypes.QueryBasketResponse, error) {
//TODO implement me
panic("implement me")
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

basket, err := k.stateStore.BasketStore().GetByBasketDenom(ctx, request.BasketDenom)
if err != nil {
return nil, err
}

basketGogo := &baskettypes.Basket{}
err = PulsarToGogoSlow(basket, basketGogo)
if err != nil {
return nil, err
}

return &baskettypes.QueryBasketResponse{Basket: basketGogo}, nil
}
55 changes: 55 additions & 0 deletions x/ecocredit/server/basket/query_basket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package basket_test

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/orm/model/ormdb"
"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
"github.com/cosmos/cosmos-sdk/orm/testing/ormtest"

basketv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/server"
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket/mocks"
)

func TestKeeper_Basket(t *testing.T) {
ctx := ormtable.WrapContextDefault(ormtest.NewMemoryBackend())

// prepare database
db, err := ormdb.NewModuleDB(server.ModuleSchema, ormdb.ModuleDBOptions{})
stateStore, err := basketv1.NewStateStore(db)
require.NoError(t, err)

// setup test keeper
ctrl := gomock.NewController(t)
require.NoError(t, err)
bankKeeper := mocks.NewMockBankKeeper(ctrl)
ecocreditKeeper := mocks.NewMockEcocreditKeeper(ctrl)
k := basket.NewKeeper(db, ecocreditKeeper, bankKeeper)

// add a basket
basketDenom := "foo"
batchDenom := "bar"
err = stateStore.BasketStore().Insert(ctx, &basketv1.Basket{
BasketDenom: basketDenom,
})
require.NoError(t, err)

// query
res, err := k.Basket(ctx, &baskettypes.QueryBasketRequest{
BasketDenom: basketDenom,
})
require.NoError(t, err)
require.Equal(t, basketDenom, res.Basket.BasketDenom)

// bad query
res, err = k.Basket(ctx, &baskettypes.QueryBasketRequest{
BasketDenom: batchDenom,
})
require.Error(t, err)
}

0 comments on commit 2f8412b

Please sign in to comment.