Skip to content

Commit

Permalink
chore: (x/mint) improve code cov (#14066) (#14094)
Browse files Browse the repository at this point in the history
## Description

missed the test coverage in last audit.
ref: #13988 #13456

---

### 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/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)

(cherry picked from commit e087566)

Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
  • Loading branch information
mergify[bot] and atheeshp authored Nov 30, 2022
1 parent 7f50dfa commit 7217de3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
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

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 @@ -11,6 +11,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

0 comments on commit 7217de3

Please sign in to comment.