-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unittest for stakingmiddleware module
- Loading branch information
1 parent
1dc4c25
commit 2d469e2
Showing
2 changed files
with
230 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
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" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/notional-labs/composable/v6/app" | ||
"github.com/notional-labs/composable/v6/app/helpers" | ||
banktypes "github.com/notional-labs/composable/v6/custom/bank/types" | ||
stakingmiddlewarekeeper "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper" | ||
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
suite.Suite | ||
|
||
ctx sdk.Context | ||
// querier sdk.Querier | ||
app *app.ComposableApp | ||
stakingmiddlewareKeeper stakingmiddlewarekeeper.Keeper | ||
stakingKeeper banktypes.StakingKeeper | ||
msgServer stakingmiddlewaretypes.MsgServer | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupTest() { | ||
suite.app = helpers.SetupComposableAppWithValSet(suite.T()) | ||
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "centauri-1", Time: time.Now().UTC()}) | ||
encCfg := moduletestutil.MakeTestEncodingConfig() | ||
key := suite.app.GetKey(stakingmiddlewaretypes.StoreKey) | ||
keeper := stakingmiddlewarekeeper.NewKeeper( | ||
encCfg.Codec, | ||
key, | ||
suite.app.AccountKeeper, | ||
suite.app.BankKeeper, | ||
authtypes.NewModuleAddress(govtypes.ModuleName).String(), | ||
) | ||
keeper.RegisterKeepers(suite.app.StakingKeeper) | ||
suite.msgServer = stakingmiddlewarekeeper.NewMsgServerImpl(keeper) | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} | ||
|
||
var ( | ||
newParams = stakingmiddlewaretypes.Params{ | ||
BlocksPerEpoch: 10, | ||
AllowUnbondAfterEpochProgressBlockNumber: 7, | ||
} | ||
failParams = stakingmiddlewaretypes.Params{ | ||
BlocksPerEpoch: 3, | ||
AllowUnbondAfterEpochProgressBlockNumber: 3, | ||
} | ||
failAllowParams = stakingmiddlewaretypes.Params{ | ||
BlocksPerEpoch: 6, | ||
AllowUnbondAfterEpochProgressBlockNumber: 10, | ||
} | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestSetParams() { | ||
for _, tc := range []struct { | ||
desc string | ||
expectedParams stakingmiddlewaretypes.Params | ||
malleate func() error | ||
shouldErr bool | ||
expectedErr string | ||
}{ | ||
{ | ||
desc: "Case success", | ||
expectedParams: newParams, | ||
malleate: func() error { | ||
return suite.app.StakingMiddlewareKeeper.SetParams(suite.ctx, newParams) | ||
}, | ||
shouldErr: false, | ||
}, | ||
{ | ||
desc: "Case fail: BlocksPerEpoch < 5", | ||
expectedParams: failParams, | ||
malleate: func() error { | ||
return suite.app.StakingMiddlewareKeeper.SetParams(suite.ctx, failParams) | ||
}, | ||
shouldErr: true, | ||
expectedErr: "BlocksPerEpoch must be greater than or equal to 5", | ||
}, | ||
{ | ||
desc: "Case fail: BlocksPerEpoch < AllowUnbondAfterEpochProgressBlockNumber", | ||
expectedParams: failAllowParams, | ||
malleate: func() error { | ||
return suite.app.StakingMiddlewareKeeper.SetParams(suite.ctx, failAllowParams) | ||
}, | ||
shouldErr: true, | ||
expectedErr: "AllowUnbondAfterEpochProgressBlockNumber must be less than or equal to BlocksPerEpoch", | ||
}, | ||
} { | ||
tc := tc | ||
suite.Run(tc.desc, func() { | ||
suite.SetupTest() | ||
err := tc.malleate() | ||
if !tc.shouldErr { | ||
res := suite.app.StakingMiddlewareKeeper.GetParams(suite.ctx) | ||
suite.Equal(res, tc.expectedParams) | ||
} else { | ||
suite.Equal(err.Error(), tc.expectedErr) | ||
} | ||
}) | ||
} | ||
} |
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,117 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" | ||
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types" | ||
) | ||
|
||
func (s *KeeperTestSuite) TestMsgUpdateEpochParams() { | ||
ctx, msgServer := s.ctx, s.msgServer | ||
require := s.Require() | ||
testCases := []struct { | ||
name string | ||
input *stakingmiddlewaretypes.MsgUpdateEpochParams | ||
expErr bool | ||
expErrMsg string | ||
}{ | ||
{ | ||
name: "valid params", | ||
input: &stakingmiddlewaretypes.MsgUpdateEpochParams{ | ||
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), | ||
Params: stakingmiddlewaretypes.Params{ | ||
BlocksPerEpoch: 10, | ||
AllowUnbondAfterEpochProgressBlockNumber: 7, | ||
}, | ||
}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "invalid authority", | ||
input: &stakingmiddlewaretypes.MsgUpdateEpochParams{ | ||
Authority: "invalid", | ||
Params: stakingmiddlewaretypes.Params{ | ||
BlocksPerEpoch: 10, | ||
AllowUnbondAfterEpochProgressBlockNumber: 7, | ||
}, | ||
}, | ||
expErr: true, | ||
expErrMsg: "invalid authority", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
s.T().Run(tc.name, func(t *testing.T) { | ||
_, err := msgServer.UpdateEpochParams(ctx, tc.input) | ||
if tc.expErr { | ||
require.Error(err) | ||
require.Contains(err.Error(), tc.expErrMsg) | ||
} else { | ||
require.NoError(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestAddRevenueFundsToStaking() { | ||
accAddrs := []sdk.AccAddress{ | ||
sdk.AccAddress([]byte("addr1_______________")), | ||
} | ||
ctx, msgServer := s.ctx, s.msgServer | ||
require := s.Require() | ||
feeCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(150)) | ||
feeAmount := sdk.NewCoins(feeCoin) | ||
s.app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, feeAmount) | ||
s.app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, accAddrs[0], feeAmount) | ||
testCases := []struct { | ||
name string | ||
input *stakingmiddlewaretypes.MsgAddRevenueFundsToStakingParams | ||
expErr bool | ||
expErrMsg string | ||
}{ | ||
{ | ||
name: "success", | ||
input: &stakingmiddlewaretypes.MsgAddRevenueFundsToStakingParams{ | ||
FromAddress: accAddrs[0].String(), | ||
Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1))), | ||
}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "invalid coin", | ||
input: &stakingmiddlewaretypes.MsgAddRevenueFundsToStakingParams{ | ||
FromAddress: accAddrs[0].String(), | ||
Amount: sdk.NewCoins(sdk.NewCoin("test", sdk.NewInt(1))), | ||
}, | ||
expErr: true, | ||
expErrMsg: "Invalid coin", | ||
}, | ||
{ | ||
name: "invalid coin: two different coin denom", | ||
input: &stakingmiddlewaretypes.MsgAddRevenueFundsToStakingParams{ | ||
FromAddress: accAddrs[0].String(), | ||
Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)), sdk.NewCoin("test", sdk.NewInt(1))), | ||
}, | ||
expErr: true, | ||
expErrMsg: "Invalid coin", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
s.T().Run(tc.name, func(t *testing.T) { | ||
_, err := msgServer.AddRevenueFundsToStaking(ctx, tc.input) | ||
if tc.expErr { | ||
require.Error(err) | ||
require.Contains(err.Error(), tc.expErrMsg) | ||
} else { | ||
require.NoError(err) | ||
} | ||
}) | ||
} | ||
} |