Skip to content

Commit

Permalink
test(x/ecocredit): marketplace integration tests (#626)
Browse files Browse the repository at this point in the history
* feat: buy/sell query server

* revert basket proto

* fix whitespace

* fix empty request

* add integration tests

* fix whitespace

* test(x/ecocredit): marketplace integration tests

* allow ask denom

* test cleanup and add todos

* verify owner and add tests

* cleanup

* Update x/ecocredit/server/msg_server.go

Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>

* Apply suggestions from code review

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

* add test cases

* clean up todos

* expected balances

Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>
Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 9, 2021
1 parent 98bb2b1 commit 55d39dc
Show file tree
Hide file tree
Showing 6 changed files with 720 additions and 33 deletions.
9 changes: 6 additions & 3 deletions x/ecocredit/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (

// AccountKeeper defines the expected interface needed to create and retrieve accounts.
type AccountKeeper interface {
// Return a new account with the next account number. Does not save the new account to the store.
// NewAccount returns a new account with the next account number. Does not save the new account to the store.
NewAccount(sdk.Context, authtypes.AccountI) authtypes.AccountI

// Retrieve an account from the store.
// GetAccount retrieves an account from the store.
GetAccount(sdk.Context, sdk.AccAddress) authtypes.AccountI

// Set an account in the store.
// GetModuleAddress retrieves a module account address from the store.
GetModuleAddress(moduleName string) sdk.AccAddress

// SetAccount sets an account in the store.
SetAccount(sdk.Context, authtypes.AccountI)
}

Expand Down
19 changes: 13 additions & 6 deletions x/ecocredit/server/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/regen-network/regen-ledger/orm"
Expand Down Expand Up @@ -587,7 +586,11 @@ func (s serverImpl) UpdateSellOrders(goCtx context.Context, req *ecocredit.MsgUp

sellOrder, err := s.getSellOrder(ctx, update.SellOrderId)
if err != nil {
return nil, err
return nil, ecocredit.ErrInvalidSellOrder.Wrapf("sell order id %d not found", update.SellOrderId)
}

if req.Owner != sellOrder.Owner {
return nil, sdkerrors.ErrUnauthorized.Wrapf("signer is not the owner of sell order id %d", update.SellOrderId)
}

// TODO: Verify that NewAskPrice.Denom is in AllowAskDenom #624
Expand Down Expand Up @@ -660,7 +663,7 @@ func (s serverImpl) Buy(goCtx context.Context, req *ecocredit.MsgBuy) (*ecocredi

// verify buyer has sufficient balance in coin
if balanceAmount.LT(coinToSend.Amount) {
return nil, ecocredit.ErrInsufficientFunds.Wrapf("insufficient balance: got %s, needed at least: %s", balanceAmount.String(), coinToSend.Amount.String())
return nil, sdkerrors.ErrInsufficientFunds.Wrapf("insufficient balance: got %s, needed at least: %s", balanceAmount.String(), coinToSend.Amount.String())
}

switch order.Selection.Sum.(type) {
Expand Down Expand Up @@ -747,7 +750,9 @@ func (s serverImpl) Buy(goCtx context.Context, req *ecocredit.MsgBuy) (*ecocredi
if creditsRemaining.IsZero() {

// delete sell order if no remaining credits
s.sellOrderTable.Delete(ctx, sellOrder.OrderId)
if err := s.sellOrderTable.Delete(ctx, sellOrder.OrderId); err != nil {
return nil, err
}

} else {
sellOrder.Quantity = creditsRemaining.String()
Expand Down Expand Up @@ -803,8 +808,10 @@ func (s serverImpl) Buy(goCtx context.Context, req *ecocredit.MsgBuy) (*ecocredi
func (s serverImpl) AllowAskDenom(goCtx context.Context, req *ecocredit.MsgAllowAskDenom) (*ecocredit.MsgAllowAskDenomResponse, error) {
ctx := types.UnwrapSDKContext(goCtx)

if req.RootAddress != authtypes.NewModuleAddress(govtypes.ModuleName).String() {
return nil, sdkerrors.ErrUnauthorized.Wrap("root address must be governance module address")
rootAddress := s.accountKeeper.GetModuleAddress(govtypes.ModuleName).String()

if req.RootAddress != rootAddress {
return nil, sdkerrors.ErrUnauthorized.Wrapf("root address must be governance module address, got: %s, expected: %s", req.RootAddress, rootAddress)
}

err := s.askDenomTable.Create(ctx, &ecocredit.AskDenom{
Expand Down
6 changes: 4 additions & 2 deletions x/ecocredit/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package server_test
import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
Expand All @@ -12,12 +14,12 @@ import (
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
params "github.com/cosmos/cosmos-sdk/x/params/types/proposal"

"github.com/regen-network/regen-ledger/types/module"
"github.com/regen-network/regen-ledger/types/module/server"
ecocredittypes "github.com/regen-network/regen-ledger/x/ecocredit"
ecocredit "github.com/regen-network/regen-ledger/x/ecocredit/module"
"github.com/regen-network/regen-ledger/x/ecocredit/server/testsuite"
"github.com/stretchr/testify/suite"
)

func TestServer(t *testing.T) {
Expand Down Expand Up @@ -59,6 +61,6 @@ func TestServer(t *testing.T) {
ecocreditModule := ecocredit.NewModule(ecocreditSubspace, accountKeeper, bankKeeper)
ff.SetModules([]module.Module{ecocreditModule})

s := testsuite.NewIntegrationTestSuite(ff, ecocreditSubspace, bankKeeper)
s := testsuite.NewIntegrationTestSuite(ff, ecocreditSubspace, bankKeeper, accountKeeper)
suite.Run(t, s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func (s *IntegrationTestSuite) TestQueryClasses() {
require := s.Require()

testCases := []struct {
name string
Expand All @@ -28,7 +29,6 @@ func (s *IntegrationTestSuite) TestQueryClasses() {
},
}

require := s.Require()
for _, tc := range testCases {
s.Run(fmt.Sprintf("Case %s", tc.name), func() {
_, err := s.queryClient.Classes(s.ctx, tc.request)
Expand Down
32 changes: 11 additions & 21 deletions x/ecocredit/server/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"time"

"github.com/regen-network/regen-ledger/types/testutil"
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
params "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
"github.com/stretchr/testify/suite"

"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/ecocredit"
)

Expand All @@ -30,18 +31,20 @@ type IntegrationTestSuite struct {
paramsQueryClient params.QueryClient
signers []sdk.AccAddress

paramSpace paramstypes.Subspace
bankKeeper bankkeeper.Keeper
paramSpace paramstypes.Subspace
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper

genesisCtx types.Context
blockTime time.Time
}

func NewIntegrationTestSuite(fixtureFactory testutil.FixtureFactory, paramSpace paramstypes.Subspace, bankKeeper bankkeeper.BaseKeeper) *IntegrationTestSuite {
func NewIntegrationTestSuite(fixtureFactory testutil.FixtureFactory, paramSpace paramstypes.Subspace, bankKeeper bankkeeper.BaseKeeper, accountKeeper authkeeper.AccountKeeper) *IntegrationTestSuite {
return &IntegrationTestSuite{
fixtureFactory: fixtureFactory,
paramSpace: paramSpace,
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
}
}

Expand Down Expand Up @@ -976,14 +979,10 @@ func (s *IntegrationTestSuite) TestScenario() {
// reset the space to avoid corrupting other tests
s.paramSpace.Set(s.sdkCtx, ecocredit.KeyCreditTypes, ecocredit.DefaultParams().CreditTypes)

// TODO: test create sell order #627

askPrice := sdk.NewInt64Coin("stake", 1)

expectedSellOrderIds := []uint64{1, 2}

createSellOrder, err := s.msgClient.Sell(s.ctx, &ecocredit.MsgSell{
Owner: addr3,
Owner: addr3,
Orders: []*ecocredit.MsgSell_Order{
{
BatchDenom: batchDenom,
Expand All @@ -1002,19 +1001,13 @@ func (s *IntegrationTestSuite) TestScenario() {
s.Require().Nil(err)
s.Require().Equal(expectedSellOrderIds, createSellOrder.SellOrderIds)

// TODO: test update sell order #627

// TODO: test create buy order #627

expectedBuyOrderIds := []uint64{1}

selection := &ecocredit.MsgBuy_Order_Selection{
Sum: &ecocredit.MsgBuy_Order_Selection_SellOrderId{SellOrderId: 2},
}

createBuyOrder, err := s.msgClient.Buy(s.ctx, &ecocredit.MsgBuy{
Buyer: admin.String(),
Orders: []*ecocredit.MsgBuy_Order{
Buyer: admin.String(),
Orders: []*ecocredit.MsgBuy_Order{
{
Selection: selection,
Quantity: "1.0",
Expand All @@ -1026,7 +1019,4 @@ func (s *IntegrationTestSuite) TestScenario() {
})
s.Require().Nil(err)
s.Require().Equal(expectedBuyOrderIds, createBuyOrder.BuyOrderIds)

// TODO: test allow ask denom #627

}
Loading

0 comments on commit 55d39dc

Please sign in to comment.