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

refactor: add support for multiple events of same type in AssertEvents #2995

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 17 additions & 18 deletions modules/apps/29-fee/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
abcitypes "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/ibc-go/v6/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
Expand Down Expand Up @@ -143,23 +142,23 @@ func (suite *KeeperTestSuite) TestDistributeFeeEvent() {
suite.Require().NoError(err)
suite.Require().NotNil(res)

// calculate the total paid out fees using "distribute_fee" events
var totalPaid sdk.Coins
for _, event := range res.Events {
if event.Type == types.EventTypeDistributeFee {
for _, attr := range event.Attributes {
switch string(attr.Key) {
case types.AttributeKeyFee:
coins, err := sdk.ParseCoinsNormalized(string(attr.Value))
suite.Require().NoError(err)

totalPaid = totalPaid.Add(coins...)
}
}

}
events := res.GetEvents()
expEvents := ibctesting.EventsMap{
"distribute_fee": {
{
"receiver": suite.chainA.SenderAccount.GetAddress().String(),
"fee": defaultRecvFee.String(),
},
{
"receiver": suite.chainA.SenderAccount.GetAddress().String(),
"fee": defaultAckFee.String(),
},
{
"receiver": suite.chainA.SenderAccount.GetAddress().String(),
"fee": defaultTimeoutFee.String(),
},
},
}

// assert the total fees paid out equal the total incentivization fees escrowed
suite.Require().Equal(fee.Total(), totalPaid)
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
}
16 changes: 8 additions & 8 deletions modules/apps/transfer/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
)

func (suite *KeeperTestSuite) TestMsgTransfer() {
var (
msg *types.MsgTransfer
)
var msg *types.MsgTransfer

testCases := []struct {
name string
Expand Down Expand Up @@ -104,11 +102,13 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
events := ctx.EventManager().Events()
expEvents := ibctesting.EventsMap{
"ibc_transfer": {
"sender": suite.chainA.SenderAccount.GetAddress().String(),
"receiver": suite.chainB.SenderAccount.GetAddress().String(),
"amount": coin.Amount.String(),
"denom": coin.Denom,
"memo": "memo",
{
"sender": suite.chainA.SenderAccount.GetAddress().String(),
"receiver": suite.chainB.SenderAccount.GetAddress().String(),
"amount": coin.Amount.String(),
"denom": coin.Denom,
"memo": "memo",
},
},
}

Expand Down
39 changes: 20 additions & 19 deletions testing/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ibctesting

import (
"encoding/json"
"fmt"
"strconv"

Expand All @@ -12,7 +13,7 @@ import (
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
)

type EventsMap map[string]map[string]string
type EventsMap map[string][]map[string]string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need this type, its kind of complex. I would probably opt for just using the sdk.Events type.
Can't we just write the events assertion like this:

events := res.GetEvents()
expected := sdk.Events{
	sdk.NewEvent(
		types.EventTypeDistributeFee,
		sdk.NewAttribute(types.AttributeKeyReceiver, suite.chainA.SenderAccount.GetAddress().String()),
		sdk.NewAttribute(types.AttributeKeyFee, defaultRecvFee.String()),
	),
	sdk.NewEvent(
		types.EventTypeDistributeFee,
		sdk.NewAttribute(types.AttributeKeyReceiver, suite.chainA.SenderAccount.GetAddress().String()),
		sdk.NewAttribute(types.AttributeKeyFee, defaultAckFee.String()),
	),
	sdk.NewEvent(
		types.EventTypeDistributeFee,
		sdk.NewAttribute(types.AttributeKeyReceiver, suite.chainA.SenderAccount.GetAddress().String()),
		sdk.NewAttribute(types.AttributeKeyFee, defaultTimeoutFee.String()),
	),
}

for _, evt := range expected {
	suite.Require().Contains(events, evt)
}

Copy link
Contributor Author

@crodriguezvega crodriguezvega Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Much simpler! :) I guess we don't need AssertEvents anymore then... I will refactor that in a following PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. The only reason to have something like a map is to have an easy to read expected values with less boiler plate, because all attribute keys and values are basically only strings.


// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the
// client identifier.
Expand Down Expand Up @@ -140,25 +141,25 @@ func AssertEvents(
expected EventsMap,
actual sdk.Events,
) {
hasEvents := make(map[string]bool)
for eventType := range expected {
hasEvents[eventType] = false
}

for _, event := range actual {
expEvent, eventFound := expected[event.Type]
if eventFound {
hasEvents[event.Type] = true
suite.Require().Len(event.Attributes, len(expEvent))
for _, attr := range event.Attributes {
expValue, found := expEvent[string(attr.Key)]
suite.Require().True(found)
suite.Require().Equal(expValue, string(attr.Value))
for eventType, expEvents := range expected {
for _, expEvent := range expEvents {
var expEventsOk []bool

for _, event := range actual {
if event.Type == eventType {
ok := suite.EqualValues(len(event.Attributes), len(expEvent))
for _, attr := range event.Attributes {
expValue, found := expEvent[string(attr.Key)]
ok = ok && suite.EqualValues(true, found)
ok = ok && suite.EqualValues(expValue, string(attr.Value))
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
}
expEventsOk = append(expEventsOk, ok)
}
}
}
}

for eventName, hasEvent := range hasEvents {
suite.Require().True(hasEvent, "event: %s was not found in events", eventName)
expEventBz, err := json.Marshal(expEvent)
suite.Require().NoError(err)
suite.Require().True(suite.Contains(expEventsOk, true), "event %s of type %s was not found in events", expEventBz, eventType)
}
}
}