diff --git a/scripts/mockgen.sh b/scripts/mockgen.sh index f7bf4cefc7..a8f8144645 100755 --- a/scripts/mockgen.sh +++ b/scripts/mockgen.sh @@ -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 diff --git a/x/crisis/keeper/keeper_test.go b/x/crisis/keeper/keeper_test.go index f710a166a3..905cb99aee 100644 --- a/x/crisis/keeper/keeper_test.go +++ b/x/crisis/keeper/keeper_test.go @@ -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) }) } diff --git a/x/crisis/keeper/msg_server_test.go b/x/crisis/keeper/msg_server_test.go index 6d4ba8e546..2c2ac3ebeb 100644 --- a/x/crisis/keeper/msg_server_test.go +++ b/x/crisis/keeper/msg_server_test.go @@ -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() { diff --git a/x/crisis/testutil/expected_keepers_mocks.go b/x/crisis/testutil/expected_keepers_mocks.go new file mode 100644 index 0000000000..152ee6dce3 --- /dev/null +++ b/x/crisis/testutil/expected_keepers_mocks.go @@ -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) +}