refactor: use mocks for x/crisis keeper tests (#12558)

## Description

Closes: #12505



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Facundo Medica 2022-07-13 13:32:11 -03:00 committed by GitHub
parent a2000ffb0b
commit ef25a4573b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 32 deletions

View File

@ -14,3 +14,4 @@ $mockgen_cmd -source=x/nft/expected_keepers.go -package testutil -destination x/
$mockgen_cmd -source=x/feegrant/expected_keepers.go -package testutil -destination x/feegrant/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/mint/types/expected_keepers.go -package testutil -destination x/mint/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/params/proposal_handler_test.go -package testutil -destination x/params/testutil/staking_keeper_mock.go
$mockgen_cmd -source=x/crisis/types/expected_keepers.go -package testutil -destination x/crisis/testutil/expected_keepers_mocks.go

View File

@ -3,48 +3,58 @@ package keeper_test
import (
"testing"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/crisis/keeper"
crisistestutil "github.com/cosmos/cosmos-sdk/x/crisis/testutil"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
)
func TestLogger(t *testing.T) {
app := simapp.Setup(t, false)
ctrl := gomock.NewController(t)
supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl)
ctx := app.NewContext(true, tmproto.Header{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
require.Equal(t,
ctx.Logger().With("module", "x/"+types.ModuleName),
app.CrisisKeeper.Logger(ctx))
testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName),
keeper.Logger(testCtx.Ctx))
}
func TestInvariants(t *testing.T) {
app := simapp.Setup(t, false)
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1}})
ctrl := gomock.NewController(t)
supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl)
require.Equal(t, app.CrisisKeeper.InvCheckPeriod(), uint(5))
key := sdk.NewKVStoreKey(types.StoreKey)
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
require.Equal(t, keeper.InvCheckPeriod(), uint(5))
// SimApp has 11 registered invariants
orgInvRoutes := app.CrisisKeeper.Routes()
app.CrisisKeeper.RegisterRoute("testModule", "testRoute", func(sdk.Context) (string, bool) { return "", false })
require.Equal(t, len(app.CrisisKeeper.Routes()), len(orgInvRoutes)+1)
orgInvRoutes := keeper.Routes()
keeper.RegisterRoute("testModule", "testRoute", func(sdk.Context) (string, bool) { return "", false })
require.Equal(t, len(keeper.Routes()), len(orgInvRoutes)+1)
}
func TestAssertInvariants(t *testing.T) {
app := simapp.Setup(t, false)
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1}})
ctrl := gomock.NewController(t)
supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl)
ctx := app.NewContext(true, tmproto.Header{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
app.CrisisKeeper.RegisterRoute("testModule", "testRoute1", func(sdk.Context) (string, bool) { return "", false })
require.NotPanics(t, func() { app.CrisisKeeper.AssertInvariants(ctx) })
keeper.RegisterRoute("testModule", "testRoute1", func(sdk.Context) (string, bool) { return "", false })
require.NotPanics(t, func() { keeper.AssertInvariants(testCtx.Ctx) })
app.CrisisKeeper.RegisterRoute("testModule", "testRoute2", func(sdk.Context) (string, bool) { return "", true })
require.Panics(t, func() { app.CrisisKeeper.AssertInvariants(ctx) })
keeper.RegisterRoute("testModule", "testRoute2", func(sdk.Context) (string, bool) { return "", true })
require.Panics(t, func() { keeper.AssertInvariants(testCtx.Ctx) })
}

View File

@ -3,13 +3,15 @@ package keeper_test
import (
"testing"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/crisis/keeper"
crisistestutil "github.com/cosmos/cosmos-sdk/x/crisis/testutil"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
type KeeperTestSuite struct {
@ -20,13 +22,16 @@ type KeeperTestSuite struct {
}
func (s *KeeperTestSuite) SetupTest() {
app := simapp.Setup(s.T(), false)
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1}})
ctx := app.NewContext(true, tmproto.Header{})
ctrl := gomock.NewController(s.T())
supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl)
s.ctx = ctx
s.keeper = app.CrisisKeeper
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
s.ctx = testCtx.Ctx
s.keeper = keeper
}
func (s *KeeperTestSuite) TestMsgUpdateParams() {

View File

@ -0,0 +1,49 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: x/crisis/types/expected_keepers.go
// Package testutil is a generated GoMock package.
package testutil
import (
reflect "reflect"
types "github.com/cosmos/cosmos-sdk/types"
gomock "github.com/golang/mock/gomock"
)
// MockSupplyKeeper is a mock of SupplyKeeper interface.
type MockSupplyKeeper struct {
ctrl *gomock.Controller
recorder *MockSupplyKeeperMockRecorder
}
// MockSupplyKeeperMockRecorder is the mock recorder for MockSupplyKeeper.
type MockSupplyKeeperMockRecorder struct {
mock *MockSupplyKeeper
}
// NewMockSupplyKeeper creates a new mock instance.
func NewMockSupplyKeeper(ctrl *gomock.Controller) *MockSupplyKeeper {
mock := &MockSupplyKeeper{ctrl: ctrl}
mock.recorder = &MockSupplyKeeperMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockSupplyKeeper) EXPECT() *MockSupplyKeeperMockRecorder {
return m.recorder
}
// SendCoinsFromAccountToModule mocks base method.
func (m *MockSupplyKeeper) SendCoinsFromAccountToModule(ctx types.Context, senderAddr types.AccAddress, recipientModule string, amt types.Coins) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SendCoinsFromAccountToModule", ctx, senderAddr, recipientModule, amt)
ret0, _ := ret[0].(error)
return ret0
}
// SendCoinsFromAccountToModule indicates an expected call of SendCoinsFromAccountToModule.
func (mr *MockSupplyKeeperMockRecorder) SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, amt interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoinsFromAccountToModule", reflect.TypeOf((*MockSupplyKeeper)(nil).SendCoinsFromAccountToModule), ctx, senderAddr, recipientModule, amt)
}