Skip to content

Commit

Permalink
refactor: use mocks for x/auth/vesting module unit tests (cosmos#13127)
Browse files Browse the repository at this point in the history
* add msg_server test for x/auth/vesting

* Update msg_server_test.go

* Update x/auth/vesting/msg_server_test.go

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* Update x/auth/vesting/msg_server_test.go

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

Co-authored-by: Marko <[email protected]>
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
  • Loading branch information
3 people authored Sep 2, 2022
1 parent 9948fb6 commit dd55693
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#13048](https://github.com/cosmos/cosmos-sdk/pull/13048) Add handling of AccountNumberStoreKeyPrefix to the x/auth simulation decoder.
* [#13101](https://github.com/cosmos/cosmos-sdk/pull/13101) Remove weights from `simapp/params` and `testutil/sims`. They are now in their respective modules.
* (simapp) [#13107](https://github.com/cosmos/cosmos-sdk/pull/13107) Call `SetIAVLCacheSize` with the configured value in simapp.

* [#12398](https://github.com/cosmos/cosmos-sdk/issues/12398) Refactor all `x` modules to unit-test via mocks and decouple `simapp`.

### State Machine Breaking

Expand Down
1 change: 1 addition & 0 deletions scripts/mockgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ $mockgen_cmd -source=x/slashing/types/expected_keepers.go -package testutil -des
$mockgen_cmd -source=x/genutil/types/expected_keepers.go -package testutil -destination x/genutil/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/gov/testutil/expected_keepers.go -package testutil -destination x/gov/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/staking/types/expected_keepers.go -package testutil -destination x/staking/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/auth/vesting/types/expected_keepers.go -package testutil -destination x/auth/vesting/testutil/expected_keepers_mocks.go
3 changes: 1 addition & 2 deletions tests/e2e/auth/vesting/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
testutil2 "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/testutil"
)

func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 1
suite.Run(t, testutil2.NewIntegrationTestSuite(cfg))
suite.Run(t, NewIntegrationTestSuite(cfg))
}
File renamed without changes.
254 changes: 254 additions & 0 deletions x/auth/vesting/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package vesting_test

import (
"testing"
"time"

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

sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
vestingtestutil "github.com/cosmos/cosmos-sdk/x/auth/vesting/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)

var (
fromAddr = sdk.AccAddress([]byte("from1________________"))
to1Addr = sdk.AccAddress([]byte("to1__________________"))
to2Addr = sdk.AccAddress([]byte("to2__________________"))
to3Addr = sdk.AccAddress([]byte("to3__________________"))
fooCoin = sdk.NewInt64Coin("foo", 100)
periodCoin = sdk.NewInt64Coin("foo", 20)
)

type VestingTestSuite struct {
suite.Suite

ctx sdk.Context
accountKeeper authkeeper.AccountKeeper
bankKeeper *vestingtestutil.MockBankKeeper
msgServer vestingtypes.MsgServer
}

func (s *VestingTestSuite) SetupTest() {
key := sdk.NewKVStoreKey(authtypes.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
s.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig()

maccPerms := map[string][]string{}

ctrl := gomock.NewController(s.T())
s.bankKeeper = vestingtestutil.NewMockBankKeeper(ctrl)
s.accountKeeper = authkeeper.NewAccountKeeper(
encCfg.Codec,
key,
authtypes.ProtoBaseAccount,
maccPerms,
"cosmos",
authtypes.NewModuleAddress("gov").String(),
)

vestingtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
authtypes.RegisterInterfaces(encCfg.InterfaceRegistry)
s.msgServer = vesting.NewMsgServerImpl(s.accountKeeper, s.bankKeeper)
}

func (s *VestingTestSuite) TestCreateVestingAccount() {
testCases := map[string]struct {
preRun func()
input *vestingtypes.MsgCreateVestingAccount
expErr bool
expErrMsg string
}{
"create for existing account": {
preRun: func() {
toAcc := s.accountKeeper.NewAccountWithAddress(s.ctx, to1Addr)
s.bankKeeper.EXPECT().IsSendEnabledCoins(gomock.Any(), fooCoin).Return(nil)
s.accountKeeper.SetAccount(s.ctx, toAcc)
s.bankKeeper.EXPECT().BlockedAddr(to1Addr).Return(false)
},
input: vestingtypes.NewMsgCreateVestingAccount(
fromAddr,
to1Addr,
sdk.Coins{fooCoin},
time.Now().Unix(),
true,
),
expErr: true,
expErrMsg: "already exists",
},
"create a valid delayed vesting account": {
preRun: func() {
s.bankKeeper.EXPECT().IsSendEnabledCoins(gomock.Any(), fooCoin).Return(nil)
s.bankKeeper.EXPECT().BlockedAddr(to2Addr).Return(false)
s.bankKeeper.EXPECT().SendCoins(gomock.Any(), fromAddr, to2Addr, sdk.Coins{fooCoin}).Return(nil)
},
input: vestingtypes.NewMsgCreateVestingAccount(
fromAddr,
to2Addr,
sdk.Coins{fooCoin},
time.Now().Unix(),
true,
),
expErr: false,
expErrMsg: "",
},
"create a valid continuous vesting account": {
preRun: func() {
s.bankKeeper.EXPECT().IsSendEnabledCoins(gomock.Any(), fooCoin).Return(nil)
s.bankKeeper.EXPECT().BlockedAddr(to3Addr).Return(false)
s.bankKeeper.EXPECT().SendCoins(gomock.Any(), fromAddr, to3Addr, sdk.Coins{fooCoin}).Return(nil)
},
input: vestingtypes.NewMsgCreateVestingAccount(
fromAddr,
to3Addr,
sdk.Coins{fooCoin},
time.Now().Unix(),
false,
),
expErr: false,
expErrMsg: "",
},
}

for name, tc := range testCases {
s.Run(name, func() {
tc.preRun()
_, err := s.msgServer.CreateVestingAccount(s.ctx, tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
}
})
}
}

func (s *VestingTestSuite) TestCreatePermanentLockedAccount() {
testCases := map[string]struct {
preRun func()
input *vestingtypes.MsgCreatePermanentLockedAccount
expErr bool
expErrMsg string
}{
"create for existing account": {
preRun: func() {
toAcc := s.accountKeeper.NewAccountWithAddress(s.ctx, to1Addr)
s.bankKeeper.EXPECT().IsSendEnabledCoins(gomock.Any(), fooCoin).Return(nil)
s.bankKeeper.EXPECT().BlockedAddr(to1Addr).Return(false)
s.accountKeeper.SetAccount(s.ctx, toAcc)
},
input: vestingtypes.NewMsgCreatePermanentLockedAccount(
fromAddr,
to1Addr,
sdk.Coins{fooCoin},
),
expErr: true,
expErrMsg: "already exists",
},
"create a valid permanent locked account": {
preRun: func() {
s.bankKeeper.EXPECT().IsSendEnabledCoins(gomock.Any(), fooCoin).Return(nil)
s.bankKeeper.EXPECT().BlockedAddr(to2Addr).Return(false)
s.bankKeeper.EXPECT().SendCoins(gomock.Any(), fromAddr, to2Addr, sdk.Coins{fooCoin}).Return(nil)
},
input: vestingtypes.NewMsgCreatePermanentLockedAccount(
fromAddr,
to2Addr,
sdk.Coins{fooCoin},
),
expErr: false,
expErrMsg: "",
},
}

for name, tc := range testCases {
s.Run(name, func() {
tc.preRun()
_, err := s.msgServer.CreatePermanentLockedAccount(s.ctx, tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
}
})
}
}

func (s *VestingTestSuite) TestCreatePeriodicVestingAccount() {
testCases := map[string]struct {
preRun func()
input *vestingtypes.MsgCreatePeriodicVestingAccount
expErr bool
expErrMsg string
}{
"create for existing account": {
preRun: func() {
toAcc := s.accountKeeper.NewAccountWithAddress(s.ctx, to1Addr)
s.accountKeeper.SetAccount(s.ctx, toAcc)
},
input: vestingtypes.NewMsgCreatePeriodicVestingAccount(
fromAddr,
to1Addr,
time.Now().Unix(),
[]vestingtypes.Period{
{
Length: 10,
Amount: sdk.NewCoins(periodCoin),
},
},
),
expErr: true,
expErrMsg: "already exists",
},
"create a valid periodic vesting account": {
preRun: func() {
s.bankKeeper.EXPECT().SendCoins(gomock.Any(), fromAddr, to2Addr, gomock.Any()).Return(nil)
},
input: vestingtypes.NewMsgCreatePeriodicVestingAccount(
fromAddr,
to2Addr,
time.Now().Unix(),
[]vestingtypes.Period{
{
Length: 10,
Amount: sdk.NewCoins(periodCoin),
},
{
Length: 20,
Amount: sdk.NewCoins(fooCoin),
},
},
),
expErr: false,
expErrMsg: "",
},
}

for name, tc := range testCases {
s.Run(name, func() {
tc.preRun()
_, err := s.msgServer.CreatePeriodicVestingAccount(s.ctx, tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
}
})
}
}

func TestVestingTestSuite(t *testing.T) {
suite.Run(t, new(VestingTestSuite))
}
82 changes: 82 additions & 0 deletions x/auth/vesting/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion x/staking/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.BondedPoolName))

require.NotNil(t, acc)

acc = accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.NotBondedPoolName))
Expand Down

0 comments on commit dd55693

Please sign in to comment.