Skip to content

Commit e087566

Browse files
authored
chore: (x/mint) improve code cov (cosmos#14066)
## Description missed the test coverage in last audit. ref: cosmos#13988 cosmos#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)
1 parent 4ebe3aa commit e087566

File tree

4 files changed

+114
-4
lines changed

4 files changed

+114
-4
lines changed

x/mint/keeper/genesis_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package keeper_test
2+
3+
import (
4+
"testing"
5+
6+
"cosmossdk.io/math"
7+
"github.com/golang/mock/gomock"
8+
"github.com/stretchr/testify/suite"
9+
10+
"github.com/cosmos/cosmos-sdk/codec"
11+
storetypes "github.com/cosmos/cosmos-sdk/store/types"
12+
"github.com/cosmos/cosmos-sdk/testutil"
13+
sdk "github.com/cosmos/cosmos-sdk/types"
14+
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
15+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
16+
"github.com/cosmos/cosmos-sdk/x/mint"
17+
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
18+
minttestutil "github.com/cosmos/cosmos-sdk/x/mint/testutil"
19+
"github.com/cosmos/cosmos-sdk/x/mint/types"
20+
)
21+
22+
var minterAcc = authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter)
23+
24+
type GenesisTestSuite struct {
25+
suite.Suite
26+
27+
sdkCtx sdk.Context
28+
keeper keeper.Keeper
29+
cdc codec.BinaryCodec
30+
accountKeeper types.AccountKeeper
31+
key *storetypes.KVStoreKey
32+
}
33+
34+
func TestGenesisTestSuite(t *testing.T) {
35+
suite.Run(t, new(GenesisTestSuite))
36+
}
37+
38+
func (s *GenesisTestSuite) SetupTest() {
39+
key := sdk.NewKVStoreKey(types.StoreKey)
40+
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
41+
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
42+
43+
// gomock initializations
44+
ctrl := gomock.NewController(s.T())
45+
s.cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry)
46+
s.sdkCtx = testCtx.Ctx
47+
s.key = key
48+
49+
stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl)
50+
accountKeeper := minttestutil.NewMockAccountKeeper(ctrl)
51+
bankKeeper := minttestutil.NewMockBankKeeper(ctrl)
52+
s.accountKeeper = accountKeeper
53+
accountKeeper.EXPECT().GetModuleAddress(minterAcc.Name).Return(minterAcc.GetAddress())
54+
accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc)
55+
56+
s.keeper = keeper.NewKeeper(s.cdc, key, stakingKeeper, accountKeeper, bankKeeper, "", "")
57+
}
58+
59+
func (s *GenesisTestSuite) TestImportExportGenesis() {
60+
genesisState := types.DefaultGenesisState()
61+
genesisState.Minter = types.NewMinter(sdk.NewDecWithPrec(20, 2), math.LegacyNewDec(1))
62+
genesisState.Params = types.NewParams(
63+
"testDenom",
64+
sdk.NewDecWithPrec(15, 2),
65+
sdk.NewDecWithPrec(22, 2),
66+
sdk.NewDecWithPrec(9, 2),
67+
sdk.NewDecWithPrec(69, 2),
68+
uint64(60*60*8766/5),
69+
)
70+
71+
s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState)
72+
73+
minter := s.keeper.GetMinter(s.sdkCtx)
74+
s.Require().Equal(genesisState.Minter, minter)
75+
76+
invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, sdk.NewTransientStoreKey("transient_test"))
77+
s.Require().Panics(func() { s.keeper.GetMinter(invalidCtx.Ctx) }, "stored minter should not have been nil")
78+
params := s.keeper.GetParams(s.sdkCtx)
79+
s.Require().Equal(genesisState.Params, params)
80+
81+
genesisState2 := s.keeper.ExportGenesis(s.sdkCtx)
82+
s.Require().Equal(genesisState, genesisState2)
83+
}

x/mint/keeper/keeper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {
7272
return
7373
}
7474

75-
// GetMinter sets the minter.
75+
// SetMinter sets the minter.
7676
func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) {
7777
store := ctx.KVStore(k.storeKey)
7878
bz := k.cdc.MustMarshal(&minter)

x/mint/keeper/keeper_test.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
type IntegrationTestSuite struct {
2121
suite.Suite
2222

23-
mintKeeper keeper.Keeper
24-
ctx sdk.Context
25-
msgServer types.MsgServer
23+
mintKeeper keeper.Keeper
24+
ctx sdk.Context
25+
msgServer types.MsgServer
26+
stakingKeeper *minttestutil.MockStakingKeeper
27+
bankKeeper *minttestutil.MockBankKeeper
2628
}
2729

2830
func TestKeeperTestSuite(t *testing.T) {
@@ -52,6 +54,11 @@ func (s *IntegrationTestSuite) SetupTest() {
5254
authtypes.FeeCollectorName,
5355
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
5456
)
57+
s.stakingKeeper = stakingKeeper
58+
s.bankKeeper = bankKeeper
59+
60+
s.Require().Equal(testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName),
61+
s.mintKeeper.Logger(testCtx.Ctx))
5562

5663
err := s.mintKeeper.SetParams(s.ctx, types.DefaultParams())
5764
s.Require().NoError(err)
@@ -110,3 +117,22 @@ func (s *IntegrationTestSuite) TestParams() {
110117
})
111118
}
112119
}
120+
121+
func (s *IntegrationTestSuite) TestAliasFunctions() {
122+
stakingTokenSupply := sdk.NewIntFromUint64(100000000000)
123+
s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(stakingTokenSupply)
124+
s.Require().Equal(s.mintKeeper.StakingTokenSupply(s.ctx), stakingTokenSupply)
125+
126+
bondedRatio := sdk.NewDecWithPrec(15, 2)
127+
s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio)
128+
s.Require().Equal(s.mintKeeper.BondedRatio(s.ctx), bondedRatio)
129+
130+
coins := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000000)))
131+
s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, coins).Return(nil)
132+
s.Require().Equal(s.mintKeeper.MintCoins(s.ctx, sdk.NewCoins()), nil)
133+
s.Require().Nil(s.mintKeeper.MintCoins(s.ctx, coins))
134+
135+
fees := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000)))
136+
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, fees).Return(nil)
137+
s.Require().Nil(s.mintKeeper.AddCollectedFees(s.ctx, fees))
138+
}

x/mint/types/params.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
sdk "github.com/cosmos/cosmos-sdk/types"
1111
)
1212

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

0 commit comments

Comments
 (0)