Skip to content

Commit

Permalink
refactor: Use mocks for x/mint testing (#12468)
Browse files Browse the repository at this point in the history
## Description

Ref: #12398



---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [x] 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)
  • Loading branch information
facundomedica authored Jul 8, 2022
1 parent 5ef43f4 commit 411c272
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 109 deletions.
3 changes: 2 additions & 1 deletion scripts/mockgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ $mockgen_cmd -package mocks -destination tests/mocks/grpc_server.go github.com/g
$mockgen_cmd -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/tendermint/tendermint/libs/log Logger
$mockgen_cmd -source=orm/model/ormtable/hooks.go -package ormmocks -destination orm/testing/ormmocks/hooks.go
$mockgen_cmd -source=x/nft/expected_keepers.go -package testutil -destination x/nft/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/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
43 changes: 31 additions & 12 deletions x/mint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
gocontext "context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
minttestutil "github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

Expand All @@ -25,20 +28,36 @@ type MintTestSuite struct {
}

func (suite *MintTestSuite) SetupTest() {
var interfaceRegistry codectypes.InterfaceRegistry
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx

app, err := simtestutil.Setup(testutil.AppConfig,
&interfaceRegistry,
&suite.mintKeeper,
// gomock initializations
ctrl := gomock.NewController(suite.T())
accountKeeper := minttestutil.NewMockAccountKeeper(ctrl)
bankKeeper := minttestutil.NewMockBankKeeper(ctrl)
stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl)

accountKeeper.EXPECT().GetModuleAddress("mint").Return(sdk.AccAddress{})

suite.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
key,
stakingKeeper,
accountKeeper,
bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
suite.Require().NoError(err)

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
err := suite.mintKeeper.SetParams(suite.ctx, types.DefaultParams())
suite.Require().NoError(err)
suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter())

queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, suite.mintKeeper)

suite.ctx = ctx
suite.queryClient = types.NewQueryClient(queryHelper)
}

Expand Down
54 changes: 40 additions & 14 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,62 @@ package keeper_test

import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"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"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttestutil "github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

type IntegrationTestSuite struct {
suite.Suite

app *simapp.SimApp
ctx sdk.Context
msgServer types.MsgServer
mintKeeper keeper.Keeper
ctx sdk.Context
msgServer types.MsgServer
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

func (s *IntegrationTestSuite) SetupTest() {
app := simapp.Setup(s.T(), false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
s.ctx = testCtx.Ctx

s.app = app
s.ctx = ctx
s.msgServer = keeper.NewMsgServerImpl(s.app.MintKeeper)
// gomock initializations
ctrl := gomock.NewController(s.T())
accountKeeper := minttestutil.NewMockAccountKeeper(ctrl)
bankKeeper := minttestutil.NewMockBankKeeper(ctrl)
stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl)

accountKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(sdk.AccAddress{})

s.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
key,
stakingKeeper,
accountKeeper,
bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

err := s.mintKeeper.SetParams(s.ctx, types.DefaultParams())
s.Require().NoError(err)
s.mintKeeper.SetMinter(s.ctx, types.DefaultInitialMinter())

s.msgServer = keeper.NewMsgServerImpl(s.mintKeeper)
}

func (s *IntegrationTestSuite) TestParams() {
Expand Down Expand Up @@ -70,16 +96,16 @@ func (s *IntegrationTestSuite) TestParams() {
tc := tc

s.Run(tc.name, func() {
expected := s.app.MintKeeper.GetParams(s.ctx)
err := s.app.MintKeeper.SetParams(s.ctx, tc.input)
expected := s.mintKeeper.GetParams(s.ctx)
err := s.mintKeeper.SetParams(s.ctx, tc.input)
if tc.expectErr {
s.Require().Error(err)
} else {
expected = tc.input
s.Require().NoError(err)
}

p := s.app.MintKeeper.GetParams(s.ctx)
p := s.mintKeeper.GetParams(s.ctx)
s.Require().Equal(expected, p)
})
}
Expand Down
4 changes: 2 additions & 2 deletions x/mint/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s *IntegrationTestSuite) TestUpdateParams() {
{
name: "set invalid params",
request: &types.MsgUpdateParams{
Authority: s.app.MintKeeper.GetAuthority(),
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationRateChange: sdk.NewDecWithPrec(-13, 2),
Expand All @@ -36,7 +36,7 @@ func (s *IntegrationTestSuite) TestUpdateParams() {
{
name: "set full valid params",
request: &types.MsgUpdateParams{
Authority: s.app.MintKeeper.GetAuthority(),
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationRateChange: sdk.NewDecWithPrec(8, 2),
Expand Down
40 changes: 30 additions & 10 deletions x/mint/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package keeper_test
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
keep "github.com/cosmos/cosmos-sdk/x/mint/keeper"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
minttestutil "github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

Expand All @@ -26,15 +30,31 @@ type MintKeeperTestSuite struct {
}

func (suite *MintKeeperTestSuite) SetupTest() {
app, err := simtestutil.Setup(testutil.AppConfig,
&suite.legacyAmino,
&suite.mintKeeper,
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx

// gomock initializations
ctrl := gomock.NewController(suite.T())
accountKeeper := minttestutil.NewMockAccountKeeper(ctrl)
bankKeeper := minttestutil.NewMockBankKeeper(ctrl)
stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl)

accountKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(sdk.AccAddress{})

suite.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
key,
stakingKeeper,
accountKeeper,
bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
suite.Require().NoError(err)

suite.ctx = app.BaseApp.NewContext(true, tmproto.Header{})

suite.mintKeeper.SetParams(suite.ctx, types.DefaultParams())
err := suite.mintKeeper.SetParams(suite.ctx, types.DefaultParams())
suite.Require().NoError(err)
suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter())
}

Expand Down
13 changes: 5 additions & 8 deletions x/mint/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ import (

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/depinject"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/mint/simulation"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

func TestDecodeStore(t *testing.T) {
var cdc codec.Codec
err := depinject.Inject(testutil.AppConfig, &cdc)
require.NoError(t, err)
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})

dec := simulation.NewDecodeStore(cdc)
dec := simulation.NewDecodeStore(encCfg.Codec)

minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15))

kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{Key: types.MinterKey, Value: cdc.MustMarshal(&minter)},
{Key: types.MinterKey, Value: encCfg.Codec.MustMarshal(&minter)},
{Key: []byte{0x99}, Value: []byte{0x99}},
},
}
Expand Down
14 changes: 6 additions & 8 deletions x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@ import (
"github.com/stretchr/testify/require"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/mint/simulation"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
// Abonormal scenarios are not tested here.
func TestRandomizedGenState(t *testing.T) {
interfaceRegistry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})

s := rand.NewSource(1)
r := rand.New(s)

simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Cdc: encCfg.Codec,
Rand: r,
NumBonded: 3,
Accounts: simtypes.RandomAccounts(r, 3),
Expand Down Expand Up @@ -59,8 +58,7 @@ func TestRandomizedGenState(t *testing.T) {

// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
func TestRandomizedGenState1(t *testing.T) {
interfaceRegistry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})

s := rand.NewSource(1)
r := rand.New(s)
Expand All @@ -74,7 +72,7 @@ func TestRandomizedGenState1(t *testing.T) {
{ // panic => reason: incomplete initialization of the simState
module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Cdc: encCfg.Codec,
Rand: r,
}, "assignment to entry in nil map"},
}
Expand Down
47 changes: 0 additions & 47 deletions x/mint/testutil/app.yaml

This file was deleted.

Loading

0 comments on commit 411c272

Please sign in to comment.