From 63d93019c7cd2b3abfc4d4be1076597541ad9646 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Mon, 30 Oct 2023 11:38:42 -0600 Subject: [PATCH] [1699]: clean up some of the expected event creation stuff. --- x/exchange/keeper/fulfillment_test.go | 21 ++----------- x/exchange/keeper/keeper_test.go | 19 ++++++++++++ x/exchange/keeper/market_test.go | 44 ++++++++++----------------- x/exchange/keeper/msg_server_test.go | 9 ------ x/exchange/keeper/orders_test.go | 25 +++++++-------- 5 files changed, 49 insertions(+), 69 deletions(-) diff --git a/x/exchange/keeper/fulfillment_test.go b/x/exchange/keeper/fulfillment_test.go index 93f4dcb845..f4131775f7 100644 --- a/x/exchange/keeper/fulfillment_test.go +++ b/x/exchange/keeper/fulfillment_test.go @@ -615,12 +615,7 @@ func (s *TestSuite) TestKeeper_FillBids() { tc.holdKeeper = NewMockHoldKeeper() } - expEvents := make(sdk.Events, len(tc.expEvents)) - for i, expEvent := range tc.expEvents { - event, err := sdk.TypedEventToEvent(expEvent) - s.Require().NoError(err, "[%d]TypedEventToEvent") - expEvents[i] = event - } + expEvents := untypeEvents(s, tc.expEvents) em := sdk.NewEventManager() ctx := s.ctx.WithEventManager(em) @@ -1267,12 +1262,7 @@ func (s *TestSuite) TestKeeper_FillAsks() { tc.holdKeeper = NewMockHoldKeeper() } - expEvents := make(sdk.Events, len(tc.expEvents)) - for i, expEvent := range tc.expEvents { - event, err := sdk.TypedEventToEvent(expEvent) - s.Require().NoError(err, "[%d]TypedEventToEvent") - expEvents[i] = event - } + expEvents := untypeEvents(s, tc.expEvents) em := sdk.NewEventManager() ctx := s.ctx.WithEventManager(em) @@ -1836,12 +1826,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() { tc.holdKeeper = NewMockHoldKeeper() } - expEvents := make(sdk.Events, len(tc.expEvents)) - for i, expEvent := range tc.expEvents { - event, err := sdk.TypedEventToEvent(expEvent) - s.Require().NoError(err, "[%d]TypedEventToEvent(%T)", expEvent) - expEvents[i] = event - } + expEvents := untypeEvents(s, tc.expEvents) em := sdk.NewEventManager() ctx := s.ctx.WithEventManager(em) diff --git a/x/exchange/keeper/keeper_test.go b/x/exchange/keeper/keeper_test.go index bc246ed15f..bc20aa446e 100644 --- a/x/exchange/keeper/keeper_test.go +++ b/x/exchange/keeper/keeper_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/gogo/protobuf/proto" "github.com/rs/zerolog" "github.com/stretchr/testify/suite" @@ -410,6 +411,24 @@ func (s *TestSuite) requirePanicEquals(f assertions.PanicTestFunc, expected stri assertions.RequirePanicEquals(s.T(), f, expected, msgAndArgs...) } +// untypeEvent returns sdk.TypedEventToEvent(tev) requiring it to not error. +func (s *TestSuite) untypeEvent(tev proto.Message) sdk.Event { + rv, err := sdk.TypedEventToEvent(tev) + s.Require().NoError(err, "TypedEventToEvent(%T)", tev) + return rv +} + +// untypeEvents applies sdk.TypedEventToEvent(tev) to each of the provided things, requiring it to not error. +func untypeEvents[P proto.Message](s *TestSuite, tevs []P) sdk.Events { + rv := make(sdk.Events, len(tevs)) + for i, tev := range tevs { + event, err := sdk.TypedEventToEvent(tev) + s.Require().NoError(err, "[%d]TypedEventToEvent(%T)", i, tev) + rv[i] = event + } + return rv +} + func (s *TestSuite) TestKeeper_GetAuthority() { expected := authtypes.NewModuleAddress(govtypes.ModuleName).String() var actual string diff --git a/x/exchange/keeper/market_test.go b/x/exchange/keeper/market_test.go index 5017004023..fec583780c 100644 --- a/x/exchange/keeper/market_test.go +++ b/x/exchange/keeper/market_test.go @@ -3183,9 +3183,8 @@ func (s *TestSuite) TestKeeper_UpdateFees() { var expectedEvents sdk.Events if tc.msg != nil { - expEvent, err := sdk.TypedEventToEvent(exchange.NewEventMarketFeesUpdated(tc.msg.MarketId)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketFeesUpdated(%d)) error", tc.msg.MarketId) - expectedEvents = append(expectedEvents, expEvent) + event := exchange.NewEventMarketFeesUpdated(tc.msg.MarketId) + expectedEvents = append(expectedEvents, s.untypeEvent(event)) } em := sdk.NewEventManager() @@ -3464,10 +3463,8 @@ func (s *TestSuite) TestKeeper_UpdateMarketActive() { var expEvents sdk.Events if len(tc.expErr) == 0 { - expEvent, err := sdk.TypedEventToEvent(exchange.NewEventMarketActiveUpdated(tc.marketID, tc.updatedBy, tc.active)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketActiveUpdated(%d, %s, %t)", - tc.marketID, string(tc.updatedBy), tc.active) - expEvents = append(expEvents, expEvent) + event := exchange.NewEventMarketActiveUpdated(tc.marketID, tc.updatedBy, tc.active) + expEvents = append(expEvents, s.untypeEvent(event)) } em := sdk.NewEventManager() @@ -3648,10 +3645,8 @@ func (s *TestSuite) TestKeeper_UpdateUserSettlementAllowed() { var expEvents sdk.Events if len(tc.expErr) == 0 { - expEvent, err := sdk.TypedEventToEvent(exchange.NewEventMarketUserSettleUpdated(tc.marketID, tc.updatedBy, tc.allow)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketUserSettleUpdated(%d, %s, %t)", - tc.marketID, string(tc.updatedBy), tc.allow) - expEvents = append(expEvents, expEvent) + event := exchange.NewEventMarketUserSettleUpdated(tc.marketID, tc.updatedBy, tc.allow) + expEvents = append(expEvents, s.untypeEvent(event)) } em := sdk.NewEventManager() @@ -4577,10 +4572,8 @@ func (s *TestSuite) TestKeeper_UpdatePermissions() { var expEvents sdk.Events if len(tc.expPanic) == 0 && len(tc.expErr) == 0 { - event, err := sdk.TypedEventToEvent(exchange.NewEventMarketPermissionsUpdated(tc.msg.MarketId, tc.msg.Admin)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketPermissionsUpdated(%d, %q)", - tc.msg.MarketId, tc.msg.Admin) - expEvents = append(expEvents, event) + event := exchange.NewEventMarketPermissionsUpdated(tc.msg.MarketId, tc.msg.Admin) + expEvents = append(expEvents, s.untypeEvent(event)) } em := sdk.NewEventManager() @@ -5568,9 +5561,8 @@ func (s *TestSuite) TestKeeper_UpdateReqAttrs() { var expEvents sdk.Events if len(tc.expPanic) == 0 && len(tc.expErr) == 0 { - event, err := sdk.TypedEventToEvent(exchange.NewEventMarketReqAttrUpdated(tc.msg.MarketId, tc.msg.Admin)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketReqAttrUpdated(%d, %q))", tc.msg.MarketId, tc.msg.Admin) - expEvents = append(expEvents, event) + event := exchange.NewEventMarketReqAttrUpdated(tc.msg.MarketId, tc.msg.Admin) + expEvents = append(expEvents, s.untypeEvent(event)) } em := sdk.NewEventManager() @@ -5876,9 +5868,8 @@ func (s *TestSuite) TestKeeper_UpdateMarketDetails() { } if tc.expSetAccCall != nil { expCalls.SetAccount = append(expCalls.SetAccount, tc.expSetAccCall) - event, err := sdk.TypedEventToEvent(exchange.NewEventMarketDetailsUpdated(tc.marketID, tc.updatedBy)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketDetailsUpdated(%d, %q))", tc.marketID, tc.updatedBy) - expEvents = append(expEvents, event) + event := exchange.NewEventMarketDetailsUpdated(tc.marketID, tc.updatedBy) + expEvents = append(expEvents, s.untypeEvent(event)) } if tc.accKeeper == nil { @@ -6059,9 +6050,8 @@ func (s *TestSuite) TestKeeper_CreateMarket() { expCalls.NewAccount = append(expCalls.NewAccount, expMarketAcc) expCalls.SetAccount = append(expCalls.SetAccount, expMarketAcc) - event, err := sdk.TypedEventToEvent(exchange.NewEventMarketCreated(tc.expMarketID)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketCreated(%d))", tc.expMarketID) - expEvents = append(expEvents, event) + event := exchange.NewEventMarketCreated(tc.expMarketID) + expEvents = append(expEvents, s.untypeEvent(event)) } em := sdk.NewEventManager() @@ -6593,10 +6583,8 @@ func (s *TestSuite) TestKeeper_WithdrawMarketFunds() { var expEvents sdk.Events if len(tc.expErr) == 0 { - event, err := sdk.TypedEventToEvent(exchange.NewEventMarketWithdraw(tc.marketID, tc.amount, tc.toAddr, tc.withdrawnBy)) - s.Require().NoError(err, "TypedEventToEvent(NewEventMarketWithdraw(%d, %s, %q, %q)", - tc.marketID, tc.amount, s.getAddrName(tc.toAddr), tc.withdrawnBy) - expEvents = append(expEvents, event) + event := exchange.NewEventMarketWithdraw(tc.marketID, tc.amount, tc.toAddr, tc.withdrawnBy) + expEvents = append(expEvents, s.untypeEvent(event)) } if tc.bankKeeper == nil { diff --git a/x/exchange/keeper/msg_server_test.go b/x/exchange/keeper/msg_server_test.go index 84d36ad0b0..85afd81884 100644 --- a/x/exchange/keeper/msg_server_test.go +++ b/x/exchange/keeper/msg_server_test.go @@ -4,8 +4,6 @@ import ( "context" "fmt" - "github.com/gogo/protobuf/proto" - abci "github.com/tendermint/tendermint/abci/types" sdkmath "cosmossdk.io/math" @@ -100,13 +98,6 @@ func runMsgServerTestCase[R any, S any, F any](s *TestSuite, td msgServerTestDef td.followup(&tc.msg, tc.fArgs) } -// untypeEvent returns sdk.TypedEventToEvent(tev) requiring it to not error. -func (s *TestSuite) untypeEvent(tev proto.Message) sdk.Event { - rv, err := sdk.TypedEventToEvent(tev) - s.Require().NoError(err, "TypedEventToEvent(%T)", tev) - return rv -} - // newAttr creates a new EventAttribute with the provided key and the quoted value. func (s *TestSuite) newAttr(key, value string) abci.EventAttribute { return abci.EventAttribute{Key: []byte(key), Value: []byte(fmt.Sprintf("%q", value))} diff --git a/x/exchange/keeper/orders_test.go b/x/exchange/keeper/orders_test.go index 13b9aa32b2..8e15e8453b 100644 --- a/x/exchange/keeper/orders_test.go +++ b/x/exchange/keeper/orders_test.go @@ -892,9 +892,8 @@ func (s *TestSuite) TestKeeper_CreateAskOrder() { var expEvents sdk.Events if len(tc.expErr) == 0 { expOrder = exchange.NewOrder(tc.expOrderID).WithAsk(&tc.askOrder) - event, err := sdk.TypedEventToEvent(exchange.NewEventOrderCreated(expOrder)) - s.Require().NoError(err, "TypedEventToEvent") - expEvents = append(expEvents, event) + event := exchange.NewEventOrderCreated(expOrder) + expEvents = append(expEvents, s.untypeEvent(event)) } if tc.attrKeeper == nil { @@ -1394,9 +1393,8 @@ func (s *TestSuite) TestKeeper_CreateBidOrder() { var expEvents sdk.Events if len(tc.expErr) == 0 { expOrder = exchange.NewOrder(tc.expOrderID).WithBid(&tc.bidOrder) - event, err := sdk.TypedEventToEvent(exchange.NewEventOrderCreated(expOrder)) - s.Require().NoError(err, "TypedEventToEvent") - expEvents = append(expEvents, event) + event := exchange.NewEventOrderCreated(expOrder) + expEvents = append(expEvents, s.untypeEvent(event)) } if tc.attrKeeper == nil { @@ -1682,9 +1680,8 @@ func (s *TestSuite) TestKeeper_CancelOrder() { var expEvents sdk.Events var expDelKVs []sdk.KVPair if cancelledOrder != nil { - event, err := sdk.TypedEventToEvent(exchange.NewEventOrderCancelled(cancelledOrder, tc.signer)) - s.Require().NoError(err, "TypedEventToEvent") - expEvents = append(expEvents, event) + event := exchange.NewEventOrderCancelled(cancelledOrder, tc.signer) + expEvents = append(expEvents, s.untypeEvent(event)) expDelKVs = keeper.CreateConstantIndexEntries(*cancelledOrder) extIDKV := keeper.CreateMarketExternalIDToOrderEntry(cancelledOrder) @@ -2002,14 +1999,14 @@ func (s *TestSuite) TestKeeper_SetOrderExternalID() { var expEvents sdk.Events var expOrder *exchange.Order if len(tc.expErr) == 0 { - event, err := sdk.TypedEventToEvent(&exchange.EventOrderExternalIDUpdated{ + event := &exchange.EventOrderExternalIDUpdated{ OrderId: tc.orderID, MarketId: tc.marketID, ExternalId: tc.newExternalID, - }) - s.Require().NoError(err, "TypedEventToEvent(EventOrderExternalIDUpdated(%d, %d, %q)", - tc.orderID, tc.marketID, tc.newExternalID) - expEvents = append(expEvents, event) + } + expEvents = append(expEvents, s.untypeEvent(event)) + + var err error expOrder, err = s.k.GetOrder(s.ctx, tc.orderID) s.Require().NoError(err, "GetOrder(%d) before anything", tc.orderID) switch {