forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use mocks for x/auth/vesting module unit tests (cosmos#13127)
* 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
1 parent
9948fb6
commit dd55693
Showing
7 changed files
with
339 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters