Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Sep 20, 2023
1 parent 8e7c5d0 commit d7f5212
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions testutil/events.go
Original file line number Diff line number Diff line change
@@ -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
}
81 changes: 66 additions & 15 deletions x/epochs/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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)
},
},
}
Expand All @@ -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)
}

}

})
}

Expand Down
6 changes: 4 additions & 2 deletions x/epochs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

0 comments on commit d7f5212

Please sign in to comment.