Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Aug 17, 2023
1 parent 875aef9 commit fa4d36c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
42 changes: 42 additions & 0 deletions testutil/mock/types_mock_appmodule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ type Manager struct {
func NewManager(modules ...AppModule) *Manager {
moduleMap := make(map[string]interface{})
modulesStr := make([]string, 0, len(modules))
preBeginModulesStr := make([]string, 0, len(modules))
preBeginModulesStr := make([]string, 0)
for _, module := range modules {
moduleMap[module.Name()] = module
modulesStr = append(modulesStr, module.Name())
Expand All @@ -309,9 +309,13 @@ func NewManager(modules ...AppModule) *Manager {
func NewManagerFromMap(moduleMap map[string]appmodule.AppModule) *Manager {
simpleModuleMap := make(map[string]interface{})
modulesStr := make([]string, 0, len(simpleModuleMap))
preBeginModulesStr := make([]string, 0)
for name, module := range moduleMap {
simpleModuleMap[name] = module
modulesStr = append(modulesStr, name)
if _, ok := module.(appmodule.HasPreBlocker); ok {
preBeginModulesStr = append(preBeginModulesStr, name)
}
}

// Sort the modules by name. Given that we are using a map above we can't guarantee the order.
Expand All @@ -321,6 +325,7 @@ func NewManagerFromMap(moduleMap map[string]appmodule.AppModule) *Manager {
Modules: simpleModuleMap,
OrderInitGenesis: modulesStr,
OrderExportGenesis: modulesStr,
PreBlockers: preBeginModulesStr,
OrderBeginBlockers: modulesStr,
OrderEndBlockers: modulesStr,
OrderPrecommiters: modulesStr,
Expand Down
34 changes: 34 additions & 0 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,40 @@ func TestCoreAPIManagerOrderSetters(t *testing.T) {
require.Equal(t, []string{"module3", "module2", "module1"}, mm.OrderPrecommiters)
}

func TestCoreAPIManager_PreBlock(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockAppModule1 := mock.NewMockCoreModuleWithPreBlock(mockCtrl)
mm := module.NewManagerFromMap(map[string]appmodule.AppModule{
"module1": mockAppModule1,
"module2": mock.NewMockCoreAppModule(mockCtrl),
})
require.NotNil(t, mm)
require.Equal(t, 2, len(mm.Modules))
require.Equal(t, 1, len(mm.PreBlockers))

mockAppModule1.EXPECT().PreBlock(gomock.Any()).Times(1).Return(sdk.ResponsePreBlock{
ConsensusParamsChanged: true,
}, nil)
res, err := mm.PreBlock(sdk.Context{})
require.NoError(t, err)
require.True(t, res.ConsensusParamsChanged)

// test false
mockAppModule1.EXPECT().PreBlock(gomock.Any()).Times(1).Return(sdk.ResponsePreBlock{
ConsensusParamsChanged: false,
}, nil)
res, err = mm.PreBlock(sdk.Context{})
require.NoError(t, err)
require.False(t, res.ConsensusParamsChanged)

// test error
mockAppModule1.EXPECT().PreBlock(gomock.Any()).Times(1).Return(sdk.ResponsePreBlock{}, errors.New("some error"))
_, err = mm.PreBlock(sdk.Context{})
require.EqualError(t, err, "some error")
}

func TestCoreAPIManager_BeginBlock(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
Expand Down

0 comments on commit fa4d36c

Please sign in to comment.