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

test(x/ecocredit): update unit test setup for generate addresses #1182

Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions x/ecocredit/server/marketplace/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ type baseSuite struct {
ctx context.Context
k Keeper
ctrl *gomock.Controller
addr sdk.AccAddress // TODO: addr1 (#922 / #1042)
addr2 sdk.AccAddress
addrs []sdk.AccAddress
bankKeeper *mocks.MockBankKeeper
paramsKeeper *mocks.MockParamKeeper
storeKey *sdk.KVStoreKey
sdkCtx sdk.Context
}

func setupBase(t gocuke.TestingT) *baseSuite {
func setupBase(t gocuke.TestingT, numAddresses int) *baseSuite {
// prepare database
s := &baseSuite{t: t}
var err error
Expand Down Expand Up @@ -77,8 +76,10 @@ func setupBase(t gocuke.TestingT) *baseSuite {
s.k = NewKeeper(s.marketStore, s.coreStore, s.bankKeeper, s.paramsKeeper)

// set test accounts
_, _, s.addr = testdata.KeyTestPubAddr() // TODO: addr1 (#922 / #1042)
_, _, s.addr2 = testdata.KeyTestPubAddr()
for i := 0; i < numAddresses; i++ {
var _, _, addr = testdata.KeyTestPubAddr()
s.addrs = append(s.addrs, addr)
}

return s
}
Expand Down Expand Up @@ -170,6 +171,7 @@ func assertCreditsEscrowed(t gocuke.TestingT, balanceBefore, balanceAfter *ecoAp

// testSellSetup sets up a batch, class, market, and issues a balance of 100 retired and tradable to the base suite's addr.
func (s *baseSuite) testSellSetup(batchDenom, bankDenom, displayDenom, classId string, start, end *timestamppb.Timestamp, creditType core.CreditType) {
assert.Check(s.t, len(s.addrs) > 0, "When calling `testSellSetup`, the base suite must have a non-empty `addrs`.")
assert.NilError(s.t, s.coreStore.CreditTypeTable().Insert(s.ctx, &ecoApi.CreditType{
Abbreviation: "C",
Name: "carbon",
Expand All @@ -179,7 +181,7 @@ func (s *baseSuite) testSellSetup(batchDenom, bankDenom, displayDenom, classId s

assert.NilError(s.t, s.coreStore.ClassTable().Insert(s.ctx, &ecoApi.Class{
Id: classId,
Admin: s.addr,
Admin: s.addrs[0],
Metadata: "",
CreditTypeAbbrev: creditType.Abbreviation,
}))
Expand All @@ -203,7 +205,7 @@ func (s *baseSuite) testSellSetup(batchDenom, bankDenom, displayDenom, classId s
}))
assert.NilError(s.t, s.k.coreStore.BatchBalanceTable().Insert(s.ctx, &ecoApi.BatchBalance{
BatchKey: 1,
Address: s.addr,
Address: s.addrs[0],
TradableAmount: "100",
RetiredAmount: "100",
EscrowedAmount: "0",
Expand Down
6 changes: 3 additions & 3 deletions x/ecocredit/server/marketplace/msg_buy_direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func TestBuyDirect(t *testing.T) {
}

func (s *buyDirectSuite) Before(t gocuke.TestingT) {
s.baseSuite = setupBase(t)
s.alice = s.addr
s.bob = s.addr2
s.baseSuite = setupBase(t, 2)
s.alice = s.addrs[0]
s.bob = s.addrs[1]
s.aliceBankBalance = sdk.Coin{
Denom: "regen",
Amount: sdk.NewInt(100),
Expand Down
6 changes: 3 additions & 3 deletions x/ecocredit/server/marketplace/msg_cancel_sell_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestCancelSellOrder(t *testing.T) {
}

func (s *cancelSellOrder) Before(t gocuke.TestingT) {
s.baseSuite = setupBase(t)
s.alice = s.addr
s.bob = s.addr2
s.baseSuite = setupBase(t, 2)
s.alice = s.addrs[0]
s.bob = s.addrs[1]
s.creditTypeAbbrev = "C"
s.classId = "C01"
s.batchDenom = "C01-001-20200101-20210101-001"
Expand Down
4 changes: 2 additions & 2 deletions x/ecocredit/server/marketplace/msg_sell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestSell(t *testing.T) {
}

func (s *sellSuite) Before(t gocuke.TestingT) {
s.baseSuite = setupBase(t)
s.alice = s.addr
s.baseSuite = setupBase(t, 1)
s.alice = s.addrs[0]
s.aliceTradableAmount = "200"
s.creditTypeAbbrev = "C"
s.classId = "C01"
Expand Down
6 changes: 3 additions & 3 deletions x/ecocredit/server/marketplace/msg_update_sell_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func TestUpdateSellOrders(t *testing.T) {
}

func (s *updateSellOrdersSuite) Before(t gocuke.TestingT) {
s.baseSuite = setupBase(t)
s.alice = s.addr
s.bob = s.addr2
s.baseSuite = setupBase(t, 2)
s.alice = s.addrs[0]
s.bob = s.addrs[1]
s.aliceTradableAmount = "200"
s.creditTypeAbbrev = "C"
s.classId = "C01"
Expand Down
12 changes: 6 additions & 6 deletions x/ecocredit/server/marketplace/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestSell_Prune(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 1)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], "C01", start, end, creditType)

blockTime, err := types.ParseDate("block time", "2020-01-01")
Expand All @@ -26,7 +26,7 @@ func TestSell_Prune(t *testing.T) {
assert.NilError(t, err)

res, err := s.k.Sell(s.ctx, &marketplace.MsgSell{
Seller: s.addr.String(),
Seller: s.addrs[0].String(),
Orders: []*marketplace.MsgSell_Order{
{BatchDenom: batchDenom, Quantity: "10", AskPrice: &ask, Expiration: &expired},
{BatchDenom: batchDenom, Quantity: "10", AskPrice: &ask, Expiration: &notExpired},
Expand All @@ -39,14 +39,14 @@ func TestSell_Prune(t *testing.T) {
s.ctx = sdk.WrapSDKContext(s.sdkCtx)

// get the balance before pruning
balBefore, err := s.coreStore.BatchBalanceTable().Get(s.ctx, s.addr, 1)
balBefore, err := s.coreStore.BatchBalanceTable().Get(s.ctx, s.addrs[0], 1)
assert.NilError(t, err)

// prune the orders
err = s.k.PruneSellOrders(s.ctx)
assert.NilError(t, err)

balAfter, err := s.coreStore.BatchBalanceTable().Get(s.ctx, s.addr, 1)
balAfter, err := s.coreStore.BatchBalanceTable().Get(s.ctx, s.addrs[0], 1)
assert.NilError(t, err)

// we can reuse this function and pass the negated amount to get our desired behavior.
Expand All @@ -66,7 +66,7 @@ func TestSell_Prune(t *testing.T) {
// TestPrune_NilExpiration tests that sell orders with nil expirations are not deleted when PruneOrders is called.
func TestPrune_NilExpiration(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 1)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], "C01", start, end, creditType)

blockTime, err := types.ParseDate("block time", "2020-01-01")
Expand All @@ -75,7 +75,7 @@ func TestPrune_NilExpiration(t *testing.T) {
assert.NilError(t, err)

msg := &marketplace.MsgSell{
Seller: s.addr.String(),
Seller: s.addrs[0].String(),
Orders: []*marketplace.MsgSell_Order{
{BatchDenom: batchDenom, Quantity: "5", AskPrice: &ask, Expiration: nil},
{BatchDenom: batchDenom, Quantity: "10", AskPrice: &ask, Expiration: &expired},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestQueryAllowedDenoms(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 0)

allowedDenom := api.AllowedDenom{
BankDenom: "uregen",
Expand Down
6 changes: 3 additions & 3 deletions x/ecocredit/server/marketplace/query_sell_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (

func TestQuery_SellOrder(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 1)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], "C01", start, end, creditType)

// make a sell order
order := api.SellOrder{
Seller: s.addr,
Seller: s.addrs[0],
BatchKey: 1,
Quantity: "15.32",
MarketId: 1,
Expand All @@ -37,7 +37,7 @@ func TestQuery_SellOrder(t *testing.T) {

res, err := s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: id})
assert.NilError(t, err)
assert.Equal(t, s.addr.String(), res.SellOrder.Seller)
assert.Equal(t, s.addrs[0].String(), res.SellOrder.Seller)
assert.Equal(t, batchDenom, res.SellOrder.BatchDenom)
assert.Equal(t, order.Quantity, res.SellOrder.Quantity)
assert.Equal(t, ask.Denom, res.SellOrder.AskDenom)
Expand Down
24 changes: 11 additions & 13 deletions x/ecocredit/server/marketplace/query_sell_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"gotest.tools/v3/assert"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

Expand All @@ -30,12 +29,11 @@ var (

func TestSellOrders(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 2)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], classId, start, end, creditType)
_, _, addr2 := testdata.KeyTestPubAddr()

order1 := insertSellOrder(t, s, s.addr, 1)
order2 := insertSellOrder(t, s, addr2, 1)
order1 := insertSellOrder(t, s, s.addrs[0], 1)
order2 := insertSellOrder(t, s, s.addrs[1], 1)

res, err := s.k.SellOrders(s.ctx, &marketplace.QuerySellOrdersRequest{
Pagination: &query.PageRequest{Limit: 1, CountTotal: true},
Expand All @@ -52,7 +50,7 @@ func TestSellOrders(t *testing.T) {

func TestSellOrdersByDenom(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 2)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], classId, start, end, creditType)

// make another batch
Expand All @@ -65,8 +63,8 @@ func TestSellOrdersByDenom(t *testing.T) {
EndDate: nil,
}))

order1 := insertSellOrder(t, s, s.addr, 1)
order2 := insertSellOrder(t, s, s.addr, 2)
order1 := insertSellOrder(t, s, s.addrs[0], 1)
order2 := insertSellOrder(t, s, s.addrs[0], 2)

// query the first denom
res, err := s.k.SellOrdersByBatchDenom(s.ctx, &marketplace.QuerySellOrdersByBatchDenomRequest{
Expand Down Expand Up @@ -98,17 +96,17 @@ func TestSellOrdersByDenom(t *testing.T) {

func TestSellOrdersBySeller(t *testing.T) {
t.Parallel()
s := setupBase(t)
s := setupBase(t, 3)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], classId, start, end, creditType)

_, _, otherAddr := testdata.KeyTestPubAddr()
_, _, noOrdersAddr := testdata.KeyTestPubAddr()
otherAddr := s.addrs[1]
noOrdersAddr := s.addrs[2]

order1 := insertSellOrder(t, s, s.addr, 1)
order1 := insertSellOrder(t, s, s.addrs[0], 1)
order2 := insertSellOrder(t, s, otherAddr, 1)

res, err := s.k.SellOrdersBySeller(s.ctx, &marketplace.QuerySellOrdersBySellerRequest{
Seller: s.addr.String(),
Seller: s.addrs[0].String(),
Pagination: &query.PageRequest{CountTotal: true},
})
assert.NilError(t, err)
Expand Down