Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: (x/mint) improve code cov #14066

Merged
merged 9 commits into from
Nov 30, 2022
83 changes: 83 additions & 0 deletions x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package keeper_test

import (
"testing"

"cosmossdk.io/math"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"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"
"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"
)

var minterAcc = authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter)

type GenesisTestSuite struct {
suite.Suite

sdkCtx sdk.Context
keeper keeper.Keeper
cdc codec.BinaryCodec
accountKeeper types.AccountKeeper
key *storetypes.KVStoreKey
}

func TestGenesisTestSuite(t *testing.T) {
suite.Run(t, new(GenesisTestSuite))
}

func (s *GenesisTestSuite) SetupTest() {
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})

// gomock initializations
ctrl := gomock.NewController(s.T())
s.cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry)
s.sdkCtx = testCtx.Ctx
s.key = key

stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl)
accountKeeper := minttestutil.NewMockAccountKeeper(ctrl)
bankKeeper := minttestutil.NewMockBankKeeper(ctrl)
s.accountKeeper = accountKeeper
accountKeeper.EXPECT().GetModuleAddress(minterAcc.Name).Return(minterAcc.GetAddress())
accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc)

s.keeper = keeper.NewKeeper(s.cdc, key, stakingKeeper, accountKeeper, bankKeeper, "", "")
}

func (s *GenesisTestSuite) TestImportExportGenesis() {
genesisState := types.DefaultGenesisState()
genesisState.Minter = types.NewMinter(sdk.NewDecWithPrec(20, 2), math.LegacyNewDec(1))
genesisState.Params = types.NewParams(
"testDenom",
sdk.NewDecWithPrec(15, 2),
sdk.NewDecWithPrec(22, 2),
sdk.NewDecWithPrec(9, 2),
sdk.NewDecWithPrec(69, 2),
uint64(60*60*8766/5),
)

s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState)

minter := s.keeper.GetMinter(s.sdkCtx)
s.Require().Equal(genesisState.Minter, minter)

invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, sdk.NewTransientStoreKey("transient_test"))
s.Require().Panics(func() { s.keeper.GetMinter(invalidCtx.Ctx) }, "stored minter should not have been nil")
params := s.keeper.GetParams(s.sdkCtx)
s.Require().Equal(genesisState.Params, params)

genesisState2 := s.keeper.ExportGenesis(s.sdkCtx)
s.Require().Equal(genesisState, genesisState2)
}
2 changes: 1 addition & 1 deletion x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {
return
}

// GetMinter sets the minter.
// SetMinter sets the minter.
func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&minter)
Expand Down
32 changes: 29 additions & 3 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
type IntegrationTestSuite struct {
suite.Suite

mintKeeper keeper.Keeper
ctx sdk.Context
msgServer types.MsgServer
mintKeeper keeper.Keeper
ctx sdk.Context
msgServer types.MsgServer
stakingKeeper *minttestutil.MockStakingKeeper
bankKeeper *minttestutil.MockBankKeeper
}

func TestKeeperTestSuite(t *testing.T) {
Expand Down Expand Up @@ -52,6 +54,11 @@ func (s *IntegrationTestSuite) SetupTest() {
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
s.stakingKeeper = stakingKeeper
s.bankKeeper = bankKeeper
atheeshp marked this conversation as resolved.
Show resolved Hide resolved

s.Require().Equal(testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName),
s.mintKeeper.Logger(testCtx.Ctx))

err := s.mintKeeper.SetParams(s.ctx, types.DefaultParams())
s.Require().NoError(err)
Expand Down Expand Up @@ -110,3 +117,22 @@ func (s *IntegrationTestSuite) TestParams() {
})
}
}

func (s *IntegrationTestSuite) TestAliasFunctions() {
stakingTokenSupply := sdk.NewIntFromUint64(100000000000)
s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(stakingTokenSupply)
s.Require().Equal(s.mintKeeper.StakingTokenSupply(s.ctx), stakingTokenSupply)

bondedRatio := sdk.NewDecWithPrec(15, 2)
s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio)
s.Require().Equal(s.mintKeeper.BondedRatio(s.ctx), bondedRatio)

coins := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000000)))
s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, coins).Return(nil)
s.Require().Equal(s.mintKeeper.MintCoins(s.ctx, sdk.NewCoins()), nil)
s.Require().Nil(s.mintKeeper.MintCoins(s.ctx, coins))

fees := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000)))
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, fees).Return(nil)
s.Require().Nil(s.mintKeeper.AddCollectedFees(s.ctx, fees))
}
1 change: 1 addition & 0 deletions x/mint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewParams returns Params instance with the given values.
func NewParams(mintDenom string, inflationRateChange, inflationMax, inflationMin, goalBonded sdk.Dec, blocksPerYear uint64) Params {
return Params{
MintDenom: mintDenom,
Expand Down