diff --git a/go.mod b/go.mod index 10a0e381..b6c0f5e9 100644 --- a/go.mod +++ b/go.mod @@ -150,7 +150,6 @@ require ( github.com/spf13/afero v1.9.5 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/viper v1.16.0 // indirect - github.com/stretchr/objx v0.5.0 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect github.com/tendermint/go-amino v0.16.0 // indirect diff --git a/testutil/events.go b/testutil/events.go new file mode 100644 index 00000000..6702183a --- /dev/null +++ b/testutil/events.go @@ -0,0 +1,31 @@ +package testutil + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func FindEventsByType(events []sdk.Event, eventType string) (foundEvents []sdk.Event, found bool) { + for _, event := range events { + if event.Type == eventType { + foundEvents = append(foundEvents, event) + } + } + found = len(foundEvents) > 0 + return foundEvents, found +} + +func EventHasAttributes(event sdk.Event, attributes map[string]string) bool { + for key, value := range attributes { + found := false + for _, attr := range event.Attributes { + if attr.Key == key && attr.Value == value { + found = true + break + } + } + if !found { + return false + } + } + return true +} diff --git a/x/epochs/keeper/hooks_test.go b/x/epochs/keeper/hooks_test.go index 71e76a95..0050a05c 100644 --- a/x/epochs/keeper/hooks_test.go +++ b/x/epochs/keeper/hooks_test.go @@ -3,6 +3,7 @@ package keeper_test import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mycel-domain/mycel/testutil" "github.com/mycel-domain/mycel/x/epochs/types" "strconv" "time" @@ -40,22 +41,47 @@ func (h *MockHooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, ep } } +type ExpEvent struct { + EpochNumber string +} + func (suite *KeeperTestSuite) TestAfterEpochHooks() { var ( - now = time.Now() - // oneDayDuration = time.Hour * 24 + now = time.Now() + oneDayDuration = time.Hour*24 + time.Second ) testCases := []struct { - expEpochNumber string - expEventIndex int - expEventType string - - fn func() + expEpochNumber string + expBeforeEpochStartEvents []ExpEvent + expAfterEpochEndEvents []ExpEvent + fn func() }{ { - expEpochNumber: "1", - expEventIndex: 1, - expEventType: BeforeEpochStartEventType, + expBeforeEpochStartEvents: []ExpEvent{ + { + EpochNumber: "1", + }, + }, + fn: func() { + // Begin first block + suite.ctx = suite.ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second)) + suite.app.EpochsKeeper.BeginBlocker(suite.ctx) + + // Check if curent epoch is expected + epochInfo, found := suite.app.EpochsKeeper.GetEpochInfo(suite.ctx, types.DayEpochId) + suite.Require().True(found) + suite.Require().Equal(int64(1), epochInfo.CurrentEpoch) + }, + }, + { + expBeforeEpochStartEvents: []ExpEvent{ + { + EpochNumber: "1", + }, + { + EpochNumber: "2", + }, + }, fn: func() { // Begin first block suite.ctx = suite.ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second)) @@ -65,6 +91,10 @@ func (suite *KeeperTestSuite) TestAfterEpochHooks() { epochInfo, found := suite.app.EpochsKeeper.GetEpochInfo(suite.ctx, types.DayEpochId) suite.Require().True(found) suite.Require().Equal(int64(1), epochInfo.CurrentEpoch) + + // Begin second block + suite.ctx = suite.ctx.WithBlockHeight(3).WithBlockTime(now.Add(oneDayDuration)) + suite.app.EpochsKeeper.BeginBlocker(suite.ctx) }, }, } @@ -85,11 +115,32 @@ func (suite *KeeperTestSuite) TestAfterEpochHooks() { // Run test Case tc.fn() - // Check events - event := suite.ctx.EventManager().Events()[tc.expEventIndex] - suite.Require().Equal(tc.expEventType, event.Type) - suite.Require().Equal(EpochIdentifier, event.Attributes[0].Value) - suite.Require().Equal(tc.expEpochNumber, event.Attributes[1].Value) + // Check before epoch start events + if len(tc.expBeforeEpochStartEvents) != 0 { + beforeEpochStartEvents, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), BeforeEpochStartEventType) + suite.Require().True(found) + for i, expEvent := range tc.expBeforeEpochStartEvents { + event := beforeEpochStartEvents[i] + suite.Require().Equal(BeforeEpochStartEventType, event.Type) + suite.Require().Equal(EpochIdentifier, event.Attributes[0].Value) + suite.Require().Equal(expEvent.EpochNumber, event.Attributes[1].Value) + suite.Require().True(found) + } + } + + if len(tc.expAfterEpochEndEvents) != 0 { + afterEpochEndEvents, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), AfterEpochEndEventType) + suite.Require().True(found) + for i, expEvent := range tc.expBeforeEpochStartEvents { + event := afterEpochEndEvents[i] + suite.Require().Equal(AfterEpochEndEventType, event.Type) + suite.Require().Equal(EpochIdentifier, event.Attributes[0].Value) + suite.Require().Equal(expEvent.EpochNumber, event.Attributes[1].Value) + suite.Require().True(found) + } + + } + }) } diff --git a/x/epochs/module.go b/x/epochs/module.go index 318382e5..9d451a2e 100644 --- a/x/epochs/module.go +++ b/x/epochs/module.go @@ -140,9 +140,11 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw func (AppModule) ConsensusVersion() uint64 { return 1 } // BeginBlock contains the logic that is automatically triggered at the beginning of each block -func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} +func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) { +} // EndBlock contains the logic that is automatically triggered at the end of each block -func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + // am.keeper.EndBlocker(ctx) return []abci.ValidatorUpdate{} }