Skip to content

Commit 2220957

Browse files
Anmol1696zmanian
authored andcommittedDec 19, 2022
imp: Add AssertEvents which asserts events against expected event map. (cosmos#2829)
1 parent bb08f5e commit 2220957

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
9898
* (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.
9999
* (core/24-host) [\#2820](https://github.com/cosmos/ibc-go/pull/2820) Add `MustParseClientStatePath` which parses the clientID from a client state key path.
100100
* (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.
101+
* (testing) [\#2829](https://github.com/cosmos/ibc-go/pull/2829) Add `AssertEvents` which asserts events against expected event map.
101102

102103
### Bug Fixes
103104

‎modules/apps/transfer/keeper/msg_server_test.go

+17-36
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,15 @@ package keeper_test
33
import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
55
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
6+
ibctesting "github.com/cosmos/ibc-go/v6/testing"
67

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

10-
func (suite *KeeperTestSuite) assertTransferEvents(
11-
actualEvents sdk.Events,
12-
coin sdk.Coin,
13-
memo string,
14-
) {
15-
hasEvent := false
16-
17-
expEvent := map[string]string{
18-
sdk.AttributeKeySender: suite.chainA.SenderAccount.GetAddress().String(),
19-
types.AttributeKeyReceiver: suite.chainB.SenderAccount.GetAddress().String(),
20-
types.AttributeKeyAmount: coin.Amount.String(),
21-
types.AttributeKeyDenom: coin.Denom,
22-
types.AttributeKeyMemo: memo,
23-
}
24-
25-
for _, event := range actualEvents {
26-
if event.Type == types.EventTypeTransfer {
27-
hasEvent = true
28-
suite.Require().Len(event.Attributes, len(expEvent))
29-
for _, attr := range event.Attributes {
30-
expValue, found := expEvent[string(attr.Key)]
31-
suite.Require().True(found)
32-
suite.Require().Equal(expValue, string(attr.Value))
33-
}
34-
}
35-
}
36-
37-
suite.Require().True(hasEvent, "event: %s was not found in events", types.EventTypeTransfer)
38-
}
39-
4011
func (suite *KeeperTestSuite) TestMsgTransfer() {
41-
var msg *types.MsgTransfer
12+
var (
13+
msg *types.MsgTransfer
14+
)
4215

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

103+
// Verify events
104+
events := ctx.EventManager().Events()
105+
expEvents := ibctesting.EventsMap{
106+
"ibc_transfer": {
107+
"sender": suite.chainA.SenderAccount.GetAddress().String(),
108+
"receiver": suite.chainB.SenderAccount.GetAddress().String(),
109+
"amount": coin.Amount.String(),
110+
"denom": coin.Denom,
111+
"memo": "memo",
112+
},
113+
}
114+
130115
if tc.expPass {
131116
suite.Require().NoError(err)
132117
suite.Require().NotNil(res)
133118
suite.Require().NotEqual(res.Sequence, uint64(0))
134-
135-
events := ctx.EventManager().Events()
136-
suite.assertTransferEvents(events, coin, "memo")
119+
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
137120
} else {
138121
suite.Require().Error(err)
139122
suite.Require().Nil(res)
140-
141-
events := ctx.EventManager().Events()
142123
suite.Require().Len(events, 0)
143124
}
144125
})

‎testing/events.go

+33
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"strconv"
66

77
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/stretchr/testify/suite"
89

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

15+
type EventsMap map[string]map[string]string
16+
1417
// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the
1518
// client identifier.
1619
func ParseClientIDFromEvents(events sdk.Events) (string, error) {
@@ -129,3 +132,33 @@ func ParseAckFromEvents(events sdk.Events) ([]byte, error) {
129132
}
130133
return nil, fmt.Errorf("acknowledgement event attribute not found")
131134
}
135+
136+
// AssertEvents asserts that expected events are present in the actual events.
137+
// Expected map needs to be a subset of actual events to pass.
138+
func AssertEvents(
139+
suite *suite.Suite,
140+
expected EventsMap,
141+
actual sdk.Events,
142+
) {
143+
hasEvents := make(map[string]bool)
144+
for eventType := range expected {
145+
hasEvents[eventType] = false
146+
}
147+
148+
for _, event := range actual {
149+
expEvent, eventFound := expected[event.Type]
150+
if eventFound {
151+
hasEvents[event.Type] = true
152+
suite.Require().Len(event.Attributes, len(expEvent))
153+
for _, attr := range event.Attributes {
154+
expValue, found := expEvent[string(attr.Key)]
155+
suite.Require().True(found)
156+
suite.Require().Equal(expValue, string(attr.Value))
157+
}
158+
}
159+
}
160+
161+
for eventName, hasEvent := range hasEvents {
162+
suite.Require().True(hasEvent, "event: %s was not found in events", eventName)
163+
}
164+
}

0 commit comments

Comments
 (0)
Please sign in to comment.