From d146ffdc7ad881bc240b6d5489e880c2e43496ce Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 24 Aug 2022 16:50:38 -0700 Subject: [PATCH] test(x/ecocredit): add tests for basket events (#1408) * test: basket event checks * docs: util godocs * style: feature file formatting * style: step ordering Co-authored-by: tyler <{ID}+{username}@users.noreply.github.com> --- types/testutil/events.go | 3 +- .../server/basket/features/msg_create.feature | 14 ++++ .../server/basket/features/msg_put.feature | 38 ++++++++++ .../server/basket/features/msg_take.feature | 60 ++++++++++++++++ x/ecocredit/server/basket/invariants_test.go | 5 +- x/ecocredit/server/basket/keeper_test.go | 7 +- x/ecocredit/server/basket/msg_create_test.go | 22 +++++- x/ecocredit/server/basket/msg_put_test.go | 40 ++++++++++- x/ecocredit/server/basket/msg_take_test.go | 70 ++++++++++++++++++- .../basket/msg_update_basket_fee_test.go | 2 +- .../server/basket/query_balance_test.go | 2 +- .../server/basket/query_balances_test.go | 2 +- .../server/basket/query_basket_test.go | 2 +- .../server/basket/query_baskets_test.go | 2 +- x/ecocredit/server/basket/util_test.go | 2 +- 15 files changed, 254 insertions(+), 17 deletions(-) diff --git a/types/testutil/events.go b/types/testutil/events.go index 88826d54a3..964f3ec1b0 100644 --- a/types/testutil/events.go +++ b/types/testutil/events.go @@ -24,8 +24,9 @@ func MatchEvent(expected proto.Message, emitted sdk.Event) error { // GetEvent searches through the sdk.Events and attempts to return the sdk.Event whose type matches // the proto.MessageName of the passed msg. func GetEvent(msg proto.Message, events []sdk.Event) (e sdk.Event, found bool) { + eventName := proto.MessageName(msg) for _, e := range events { - if proto.MessageName(msg) == e.Type { + if eventName == e.Type { return e, true } } diff --git a/x/ecocredit/server/basket/features/msg_create.feature b/x/ecocredit/server/basket/features/msg_create.feature index cd2fb3c14a..75ccda1c90 100644 --- a/x/ecocredit/server/basket/features/msg_create.feature +++ b/x/ecocredit/server/basket/features/msg_create.feature @@ -237,3 +237,17 @@ Feature: Msg/Create """ # no failing scenario - response should always be empty when message execution fails + + Rule: Event is emitted + + Scenario: EventCreate is emitted + Given a credit type with abbreviation "C" and precision "6" + And alice's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6" + When alice attempts to create a basket with name "NCT" + Then expect event with properties + """ + { + "curator": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6", + "basket_denom": "eco.uC.NCT" + } + """ \ No newline at end of file diff --git a/x/ecocredit/server/basket/features/msg_put.feature b/x/ecocredit/server/basket/features/msg_put.feature index f0c958fcc4..2585aa4d05 100644 --- a/x/ecocredit/server/basket/features/msg_put.feature +++ b/x/ecocredit/server/basket/features/msg_put.feature @@ -283,3 +283,41 @@ Feature: Msg/Put | precision non-zero, amount decimal | 6 | 2.5 | 2500000 | # no failing scenario - response should always be empty when message execution fails + + Rule: Events are emitted + + Background: + Given a credit type with abbreviation "C" and precision "6" + And a basket with credit type "C" + And Alice's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6" + And Ecocredit module's address "regen1k82wewrfkhdmegw6uxrgwwzrsd7593t8tej2d5" + And Alice owns credit amount "2" + + Scenario: EventTransfer is emitted + When alice attempts to put credit amount "2" into the basket + Then expect event transfer with properties + """ + { + "sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6", + "recipient": "regen1k82wewrfkhdmegw6uxrgwwzrsd7593t8tej2d5", + "batch_denom": "C01-001-20200101-20210101-001", + "tradable_amount": "2" + } + """ + + Scenario: EventPut is emitted + When alice attempts to put credit amount "2" into the basket + Then expect event put with properties + """ + { + "owner": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6", + "basket_denom": "eco.uC.NCT", + "credits": [ + { + "batch_denom": "C01-001-20200101-20210101-001", + "amount": "2" + } + ], + "amount": "2000000" + } + """ \ No newline at end of file diff --git a/x/ecocredit/server/basket/features/msg_take.feature b/x/ecocredit/server/basket/features/msg_take.feature index 23a4640d6f..702c4f0701 100644 --- a/x/ecocredit/server/basket/features/msg_take.feature +++ b/x/ecocredit/server/basket/features/msg_take.feature @@ -184,3 +184,63 @@ Feature: Msg/Take | precision non-zero, credits decimal | 6 | 2500000 | 2.500000 | # no failing scenario - response should always be empty when message execution fails + + Rule: Events are emitted + + Background: + Given a credit type with abbreviation "C" and precision "6" + And a basket with credit type "C" and disable auto retire "true" + And basket token supply amount "2000000" + And Alice's address "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp" + And Ecocredit module's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6" + And Alice owns basket token amount "2000000" + + Scenario: EventTake is emitted + When alice attempts to take credits with basket token amount "2000000" + Then expect event take with properties + """ + { + "owner": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp", + "basket_denom": "eco.uC.NCT", + "credits": [ + {"batch_denom": "C01-001-20200101-20210101-001", "amount": "2.000000"} + ], + "amount": "2000000" + } + """ + + Scenario: EventRetire is emitted + When alice attempts to take credits with basket token amount "2000000" and retire on take "true" from "US-WA" + Then expect event retire with properties + """ + { + "owner": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp", + "batch_denom": "C01-001-20200101-20210101-001", + "amount": "2.000000", + "jurisdiction": "US-WA" + } + """ + + Scenario: EventTransfer is emitted when retire on take is true + When alice attempts to take credits with basket token amount "2000000" and retire on take "true" from "US-WA" + Then expect event transfer with properties + """ + { + "sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6", + "recipient": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp", + "batch_denom": "C01-001-20200101-20210101-001", + "retired_amount": "2.000000" + } + """ + + Scenario: EventTransfer is emitted when retire on take is false + When alice attempts to take credits with basket token amount "2000000" and retire on take "false" + Then expect event transfer with properties + """ + { + "sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6", + "recipient": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp", + "batch_denom": "C01-001-20200101-20210101-001", + "tradable_amount": "2.000000" + } + """ \ No newline at end of file diff --git a/x/ecocredit/server/basket/invariants_test.go b/x/ecocredit/server/basket/invariants_test.go index da48300250..d815e08140 100644 --- a/x/ecocredit/server/basket/invariants_test.go +++ b/x/ecocredit/server/basket/invariants_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "testing" @@ -9,7 +9,6 @@ import ( api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" "github.com/regen-network/regen-ledger/types/math" - "github.com/regen-network/regen-ledger/x/ecocredit/server/basket" ) type BasketWithSupply struct { @@ -69,7 +68,7 @@ func TestBasketSupplyInvarint(t *testing.T) { for _, tc := range tcs { tc.bank.GetSupply(s.sdkCtx, "abc") - msg, _ := basket.SupplyInvariant(s.sdkCtx, store, tc.bank, basketBalances) + msg, _ := SupplyInvariant(s.sdkCtx, store, tc.bank, basketBalances) if tc.msg != "" { require.Contains(msg, tc.msg, tc.name) } else { diff --git a/x/ecocredit/server/basket/keeper_test.go b/x/ecocredit/server/basket/keeper_test.go index d447d00921..6f37196aed 100644 --- a/x/ecocredit/server/basket/keeper_test.go +++ b/x/ecocredit/server/basket/keeper_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "context" @@ -24,7 +24,6 @@ import ( ecoApi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" "github.com/regen-network/regen-ledger/x/ecocredit" "github.com/regen-network/regen-ledger/x/ecocredit/mocks" - "github.com/regen-network/regen-ledger/x/ecocredit/server/basket" ) const ( @@ -36,7 +35,7 @@ type baseSuite struct { t gocuke.TestingT db ormdb.ModuleDB ctx context.Context - k basket.Keeper + k Keeper ctrl *gomock.Controller addrs []sdk.AccAddress stateStore api.StateStore @@ -77,7 +76,7 @@ func setupBase(t gocuke.TestingT) *baseSuite { authority, err := sdk.AccAddressFromBech32("regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68") assert.NilError(t, err) - s.k = basket.NewKeeper(s.stateStore, s.coreStore, s.bankKeeper, s.paramsKeeper, moduleAddress, authority) + s.k = NewKeeper(s.stateStore, s.coreStore, s.bankKeeper, s.paramsKeeper, moduleAddress, authority) s.coreStore, err = ecoApi.NewStateStore(s.db) assert.NilError(t, err) diff --git a/x/ecocredit/server/basket/msg_create_test.go b/x/ecocredit/server/basket/msg_create_test.go index 8ee8e01155..ef68cc7fc4 100644 --- a/x/ecocredit/server/basket/msg_create_test.go +++ b/x/ecocredit/server/basket/msg_create_test.go @@ -1,7 +1,8 @@ //nolint:revive,stylecheck -package basket_test +package basket import ( + "encoding/json" "strconv" "testing" @@ -14,6 +15,7 @@ import ( api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" coreapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" + "github.com/regen-network/regen-ledger/types/testutil" "github.com/regen-network/regen-ledger/x/ecocredit/basket" "github.com/regen-network/regen-ledger/x/ecocredit/core" ) @@ -97,6 +99,12 @@ func (s *createSuite) ACreditTypeWithAbbreviationAndPrecision(a string, b string require.NoError(s.t, err) } +func (s *createSuite) AlicesAddress(a string) { + addr, err := sdk.AccAddressFromBech32(a) + require.NoError(s.t, err) + s.alice = addr +} + func (s *createSuite) ACreditClassWithId(a string) { creditTypeAbbrev := core.GetCreditTypeAbbrevFromClassID(a) @@ -222,6 +230,18 @@ func (s *createSuite) ExpectTheResponse(a gocuke.DocString) { require.Equal(s.t, res, s.res) } +func (s *createSuite) ExpectEventWithProperties(a gocuke.DocString) { + var event basket.EventCreate + err := json.Unmarshal([]byte(a.Content), &event) + require.NoError(s.t, err) + + sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events()) + require.True(s.t, found) + + err = testutil.MatchEvent(&event, sdkEvent) + require.NoError(s.t, err) +} + func (s *createSuite) createExpectCalls() { var expectedFee sdk.Coin diff --git a/x/ecocredit/server/basket/msg_put_test.go b/x/ecocredit/server/basket/msg_put_test.go index 71a0f15073..83b067f415 100644 --- a/x/ecocredit/server/basket/msg_put_test.go +++ b/x/ecocredit/server/basket/msg_put_test.go @@ -1,6 +1,7 @@ -package basket_test +package basket import ( + "encoding/json" "strconv" "strings" "testing" @@ -18,6 +19,7 @@ import ( coreapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" "github.com/regen-network/regen-ledger/types" "github.com/regen-network/regen-ledger/types/math" + "github.com/regen-network/regen-ledger/types/testutil" "github.com/regen-network/regen-ledger/x/ecocredit/basket" "github.com/regen-network/regen-ledger/x/ecocredit/core" ) @@ -236,6 +238,18 @@ func (s *putSuite) ACreditBatchWithDenom(a string) { require.NoError(s.t, err) } +func (s *putSuite) AlicesAddress(a string) { + addr, err := sdk.AccAddressFromBech32(a) + require.NoError(s.t, err) + s.alice = addr +} + +func (s *putSuite) EcocreditModulesAddress(a string) { + addr, err := sdk.AccAddressFromBech32(a) + require.NoError(s.t, err) + s.k.moduleAddress = addr +} + func (s *putSuite) AliceOwnsCredits() { classID := core.GetClassIDFromBatchDenom(s.batchDenom) creditTypeAbbrev := core.GetCreditTypeAbbrevFromClassID(classID) @@ -504,6 +518,30 @@ func (s *putSuite) ExpectTheResponse(a gocuke.DocString) { require.Equal(s.t, res, s.res) } +func (s *putSuite) ExpectEventTransferWithProperties(a gocuke.DocString) { + var event core.EventTransfer + err := json.Unmarshal([]byte(a.Content), &event) + require.NoError(s.t, err) + + sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events()) + require.True(s.t, found) + + err = testutil.MatchEvent(&event, sdkEvent) + require.NoError(s.t, err) +} + +func (s *putSuite) ExpectEventPutWithProperties(a gocuke.DocString) { + var event basket.EventPut + err := json.Unmarshal([]byte(a.Content), &event) + require.NoError(s.t, err) + + sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events()) + require.True(s.t, found) + + err = testutil.MatchEvent(&event, sdkEvent) + require.NoError(s.t, err) +} + func (s *putSuite) putExpectCalls() { sendCoin := s.calculateExpectedCoin(s.tradableCredits) sendCoins := sdk.NewCoins(sendCoin) diff --git a/x/ecocredit/server/basket/msg_take_test.go b/x/ecocredit/server/basket/msg_take_test.go index 8feea8b30b..5428e031cb 100644 --- a/x/ecocredit/server/basket/msg_take_test.go +++ b/x/ecocredit/server/basket/msg_take_test.go @@ -1,6 +1,7 @@ -package basket_test +package basket import ( + "encoding/json" "strconv" "testing" @@ -12,6 +13,7 @@ import ( api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" coreapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" + "github.com/regen-network/regen-ledger/types/testutil" "github.com/regen-network/regen-ledger/x/ecocredit/basket" "github.com/regen-network/regen-ledger/x/ecocredit/core" ) @@ -166,6 +168,18 @@ func (s *takeSuite) ABasketWithCreditTypeAndCreditBalance(a string, b string) { s.addBasketClassAndBalance(basketID, b) } +func (s *takeSuite) AlicesAddress(a string) { + addr, err := sdk.AccAddressFromBech32(a) + require.NoError(s.t, err) + s.alice = addr +} + +func (s *takeSuite) EcocreditModulesAddress(a string) { + addr, err := sdk.AccAddressFromBech32(a) + require.NoError(s.t, err) + s.k.moduleAddress = addr +} + func (s *takeSuite) AliceOwnsBasketTokens() { amount, ok := sdk.NewIntFromString(s.tokenAmount) require.True(s.t, ok) @@ -320,6 +334,60 @@ func (s *takeSuite) ExpectTheResponse(a gocuke.DocString) { require.Equal(s.t, res, s.res) } +func (s *takeSuite) ExpectEventTakeWithProperties(a gocuke.DocString) { + var event basket.EventTake + err := json.Unmarshal([]byte(a.Content), &event) + require.NoError(s.t, err) + + sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events()) + require.True(s.t, found) + + err = testutil.MatchEvent(&event, sdkEvent) + require.NoError(s.t, err) +} + +func (s *takeSuite) AliceAttemptsToTakeCreditsWithBasketTokenAmountAndRetireOnTakeFrom(a, b, c string) { + s.tokenAmount = a + + retireOnTake, err := strconv.ParseBool(b) + require.NoError(s.t, err) + + s.takeExpectCalls() + + s.res, s.err = s.k.Take(s.ctx, &basket.MsgTake{ + Owner: s.alice.String(), + BasketDenom: s.basketDenom, + Amount: s.tokenAmount, + RetirementJurisdiction: c, + RetireOnTake: retireOnTake, + }) + require.NoError(s.t, err) +} + +func (s *takeSuite) ExpectEventRetireWithProperties(a gocuke.DocString) { + var event core.EventRetire + err := json.Unmarshal([]byte(a.Content), &event) + require.NoError(s.t, err) + + sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events()) + require.True(s.t, found) + + err = testutil.MatchEvent(&event, sdkEvent) + require.NoError(s.t, err) +} + +func (s *takeSuite) ExpectEventTransferWithProperties(a gocuke.DocString) { + var event core.EventTransfer + err := json.Unmarshal([]byte(a.Content), &event) + require.NoError(s.t, err) + + sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events()) + require.True(s.t, found) + + err = testutil.MatchEvent(&event, sdkEvent) + require.NoError(s.t, err) +} + func (s *takeSuite) addBasketClassAndBalance(basketID uint64, creditAmount string) { err := s.stateStore.BasketClassTable().Insert(s.ctx, &api.BasketClass{ BasketId: basketID, diff --git a/x/ecocredit/server/basket/msg_update_basket_fee_test.go b/x/ecocredit/server/basket/msg_update_basket_fee_test.go index 2afa01db91..5a6be5c663 100644 --- a/x/ecocredit/server/basket/msg_update_basket_fee_test.go +++ b/x/ecocredit/server/basket/msg_update_basket_fee_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "encoding/json" diff --git a/x/ecocredit/server/basket/query_balance_test.go b/x/ecocredit/server/basket/query_balance_test.go index c1e436cd84..d4fac719df 100644 --- a/x/ecocredit/server/basket/query_balance_test.go +++ b/x/ecocredit/server/basket/query_balance_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "testing" diff --git a/x/ecocredit/server/basket/query_balances_test.go b/x/ecocredit/server/basket/query_balances_test.go index 3f45d8e2a4..18a31828fb 100644 --- a/x/ecocredit/server/basket/query_balances_test.go +++ b/x/ecocredit/server/basket/query_balances_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "testing" diff --git a/x/ecocredit/server/basket/query_basket_test.go b/x/ecocredit/server/basket/query_basket_test.go index e867e0cbde..b024a4ba4a 100644 --- a/x/ecocredit/server/basket/query_basket_test.go +++ b/x/ecocredit/server/basket/query_basket_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "testing" diff --git a/x/ecocredit/server/basket/query_baskets_test.go b/x/ecocredit/server/basket/query_baskets_test.go index 7d3b24d44d..26019f558f 100644 --- a/x/ecocredit/server/basket/query_baskets_test.go +++ b/x/ecocredit/server/basket/query_baskets_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "testing" diff --git a/x/ecocredit/server/basket/util_test.go b/x/ecocredit/server/basket/util_test.go index bbe4ef8be5..848dda78bb 100644 --- a/x/ecocredit/server/basket/util_test.go +++ b/x/ecocredit/server/basket/util_test.go @@ -1,4 +1,4 @@ -package basket_test +package basket import ( "testing"