Skip to content

Commit

Permalink
[1699]: clean up some of the expected event creation stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Oct 30, 2023
1 parent 8cf8c1d commit 63d9301
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 69 deletions.
21 changes: 3 additions & 18 deletions x/exchange/keeper/fulfillment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions x/exchange/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/gogo/protobuf/proto"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -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
Expand Down
44 changes: 16 additions & 28 deletions x/exchange/keeper/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions x/exchange/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

"github.com/gogo/protobuf/proto"

abci "github.com/tendermint/tendermint/abci/types"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -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))}
Expand Down
25 changes: 11 additions & 14 deletions x/exchange/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 63d9301

Please sign in to comment.