From 7a43de0d5d26d078801859037beb46536f8e6656 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Sun, 12 Jun 2022 21:49:26 -0700 Subject: [PATCH 1/2] test(x/ecocredit): update unit test setup for generate list of addresses (#922) --- x/ecocredit/server/marketplace/keeper_test.go | 16 +++++++++------- .../server/marketplace/msg_buy_direct_test.go | 6 +++--- .../marketplace/msg_cancel_sell_order_test.go | 6 +++--- .../server/marketplace/msg_sell_test.go | 4 ++-- .../msg_update_sell_orders_test.go | 6 +++--- x/ecocredit/server/marketplace/prune_test.go | 12 ++++++------ .../marketplace/query_allowed_denoms_test.go | 2 +- .../marketplace/query_sell_order_test.go | 6 +++--- .../marketplace/query_sell_orders_test.go | 19 +++++++++---------- 9 files changed, 39 insertions(+), 38 deletions(-) diff --git a/x/ecocredit/server/marketplace/keeper_test.go b/x/ecocredit/server/marketplace/keeper_test.go index 1ce274f952..9f8174c43b 100644 --- a/x/ecocredit/server/marketplace/keeper_test.go +++ b/x/ecocredit/server/marketplace/keeper_test.go @@ -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 @@ -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 } @@ -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", @@ -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, })) @@ -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", diff --git a/x/ecocredit/server/marketplace/msg_buy_direct_test.go b/x/ecocredit/server/marketplace/msg_buy_direct_test.go index 7a74b06a86..f264bbc1b1 100644 --- a/x/ecocredit/server/marketplace/msg_buy_direct_test.go +++ b/x/ecocredit/server/marketplace/msg_buy_direct_test.go @@ -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), diff --git a/x/ecocredit/server/marketplace/msg_cancel_sell_order_test.go b/x/ecocredit/server/marketplace/msg_cancel_sell_order_test.go index 30286009b0..cf57c52d44 100644 --- a/x/ecocredit/server/marketplace/msg_cancel_sell_order_test.go +++ b/x/ecocredit/server/marketplace/msg_cancel_sell_order_test.go @@ -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" diff --git a/x/ecocredit/server/marketplace/msg_sell_test.go b/x/ecocredit/server/marketplace/msg_sell_test.go index e14c3d1283..1c98724e5f 100644 --- a/x/ecocredit/server/marketplace/msg_sell_test.go +++ b/x/ecocredit/server/marketplace/msg_sell_test.go @@ -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" diff --git a/x/ecocredit/server/marketplace/msg_update_sell_orders_test.go b/x/ecocredit/server/marketplace/msg_update_sell_orders_test.go index 765c8f0762..bf7e4a8829 100644 --- a/x/ecocredit/server/marketplace/msg_update_sell_orders_test.go +++ b/x/ecocredit/server/marketplace/msg_update_sell_orders_test.go @@ -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" diff --git a/x/ecocredit/server/marketplace/prune_test.go b/x/ecocredit/server/marketplace/prune_test.go index 2180bdda1a..2ecbfff200 100644 --- a/x/ecocredit/server/marketplace/prune_test.go +++ b/x/ecocredit/server/marketplace/prune_test.go @@ -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") @@ -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: ¬Expired}, @@ -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. @@ -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") @@ -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}, diff --git a/x/ecocredit/server/marketplace/query_allowed_denoms_test.go b/x/ecocredit/server/marketplace/query_allowed_denoms_test.go index a2c1d1d9a8..3abec7a9b2 100644 --- a/x/ecocredit/server/marketplace/query_allowed_denoms_test.go +++ b/x/ecocredit/server/marketplace/query_allowed_denoms_test.go @@ -13,7 +13,7 @@ import ( func TestQueryAllowedDenoms(t *testing.T) { t.Parallel() - s := setupBase(t) + s := setupBase(t, 0) allowedDenom := api.AllowedDenom{ BankDenom: "uregen", diff --git a/x/ecocredit/server/marketplace/query_sell_order_test.go b/x/ecocredit/server/marketplace/query_sell_order_test.go index 7fdf52732c..c9ef0477a9 100644 --- a/x/ecocredit/server/marketplace/query_sell_order_test.go +++ b/x/ecocredit/server/marketplace/query_sell_order_test.go @@ -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, @@ -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) diff --git a/x/ecocredit/server/marketplace/query_sell_orders_test.go b/x/ecocredit/server/marketplace/query_sell_orders_test.go index fd29653de6..3345a47fb7 100644 --- a/x/ecocredit/server/marketplace/query_sell_orders_test.go +++ b/x/ecocredit/server/marketplace/query_sell_orders_test.go @@ -30,12 +30,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}, @@ -52,7 +51,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 @@ -65,8 +64,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{ @@ -98,17 +97,17 @@ func TestSellOrdersByDenom(t *testing.T) { func TestSellOrdersBySeller(t *testing.T) { t.Parallel() - s := setupBase(t) + s := setupBase(t, 2) s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], classId, start, end, creditType) _, _, otherAddr := testdata.KeyTestPubAddr() _, _, noOrdersAddr := testdata.KeyTestPubAddr() - 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) From 0447396571c7f34373ccd935af598b5e07d9a4f6 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Wed, 15 Jun 2022 22:09:44 -0700 Subject: [PATCH 2/2] pr feedback, clean up another example of explicit address creation in tests --- x/ecocredit/server/marketplace/query_sell_orders_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x/ecocredit/server/marketplace/query_sell_orders_test.go b/x/ecocredit/server/marketplace/query_sell_orders_test.go index 3345a47fb7..0849bef6b0 100644 --- a/x/ecocredit/server/marketplace/query_sell_orders_test.go +++ b/x/ecocredit/server/marketplace/query_sell_orders_test.go @@ -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" @@ -97,11 +96,11 @@ func TestSellOrdersByDenom(t *testing.T) { func TestSellOrdersBySeller(t *testing.T) { t.Parallel() - s := setupBase(t, 2) + 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.addrs[0], 1) order2 := insertSellOrder(t, s, otherAddr, 1)