cosmos-sdk/x/crisis/keeper/msg_server_test.go
Facundo Medica ef25a4573b
refactor: use mocks for x/crisis keeper tests (#12558)
## Description

Closes: #12505



---

### 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/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)
2022-07-13 16:32:11 +00:00

100 lines
2.3 KiB
Go

package keeper_test
import (
"testing"
"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"
)
type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
keeper *keeper.Keeper
}
func (s *KeeperTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl)
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() {
// default params
constantFee := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000))
testCases := []struct {
name string
input *types.MsgUpdateParams
expErr bool
expErrMsg string
}{
{
name: "invalid authority",
input: &types.MsgUpdateParams{
Authority: "invalid",
ConstantFee: constantFee,
},
expErr: true,
expErrMsg: "invalid authority",
},
{
name: "invalid constant fee",
input: &types.MsgUpdateParams{
Authority: s.keeper.GetAuthority(),
ConstantFee: sdk.Coin{},
},
expErr: true,
},
{
name: "negative constant fee",
input: &types.MsgUpdateParams{
Authority: s.keeper.GetAuthority(),
ConstantFee: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(-1000)},
},
expErr: true,
},
{
name: "all good",
input: &types.MsgUpdateParams{
Authority: s.keeper.GetAuthority(),
ConstantFee: constantFee,
},
expErr: false,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
_, err := s.keeper.UpdateParams(s.ctx, tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
}
})
}
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}