Skip to content

Commit

Permalink
imp: Add AssertEvents which asserts events against expected event m…
Browse files Browse the repository at this point in the history
…ap. (#2829)
  • Loading branch information
Anmol1696 authored Dec 12, 2022
1 parent 58d7989 commit f24f41e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (light-clients/07-tendermint) [\#2800](https://github.com/cosmos/ibc-go/pull/2800) Add optional in-place store migration function to prune all expired tendermint consensus states.
* (core/24-host) [\#2820](https://github.com/cosmos/ibc-go/pull/2820) Add `MustParseClientStatePath` which parses the clientID from a client state key path.
* (testing/simapp) [\#2842](https://github.com/cosmos/ibc-go/pull/2842) Adding the new upgrade handler for v6 -> v7 to simapp which prunes expired Tendermint consensus states.
* (testing) [\#2829](https://github.com/cosmos/ibc-go/pull/2829) Add `AssertEvents` which asserts events against expected event map.

### Bug Fixes

Expand Down
53 changes: 17 additions & 36 deletions modules/apps/transfer/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,15 @@ package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
ibctesting "github.com/cosmos/ibc-go/v6/testing"

"github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
)

func (suite *KeeperTestSuite) assertTransferEvents(
actualEvents sdk.Events,
coin sdk.Coin,
memo string,
) {
hasEvent := false

expEvent := map[string]string{
sdk.AttributeKeySender: suite.chainA.SenderAccount.GetAddress().String(),
types.AttributeKeyReceiver: suite.chainB.SenderAccount.GetAddress().String(),
types.AttributeKeyAmount: coin.Amount.String(),
types.AttributeKeyDenom: coin.Denom,
types.AttributeKeyMemo: memo,
}

for _, event := range actualEvents {
if event.Type == types.EventTypeTransfer {
hasEvent = 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))
}
}
}

suite.Require().True(hasEvent, "event: %s was not found in events", types.EventTypeTransfer)
}

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

testCases := []struct {
name string
Expand Down Expand Up @@ -127,18 +100,26 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
ctx := suite.chainA.GetContext()
res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg)

// Verify events
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",
},
}

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NotEqual(res.Sequence, uint64(0))

events := ctx.EventManager().Events()
suite.assertTransferEvents(events, coin, "memo")
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
} else {
suite.Require().Error(err)
suite.Require().Nil(res)

events := ctx.EventManager().Events()
suite.Require().Len(events, 0)
}
})
Expand Down
33 changes: 33 additions & 0 deletions testing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"

clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
)

type EventsMap map[string]map[string]string

// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the
// client identifier.
func ParseClientIDFromEvents(events sdk.Events) (string, error) {
Expand Down Expand Up @@ -129,3 +132,33 @@ func ParseAckFromEvents(events sdk.Events) ([]byte, error) {
}
return nil, fmt.Errorf("acknowledgement event attribute not found")
}

// AssertEvents asserts that expected events are present in the actual events.
// Expected map needs to be a subset of actual events to pass.
func AssertEvents(
suite *suite.Suite,
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 eventName, hasEvent := range hasEvents {
suite.Require().True(hasEvent, "event: %s was not found in events", eventName)
}
}

0 comments on commit f24f41e

Please sign in to comment.