Skip to content

Commit

Permalink
unittest for stakingmiddleware module
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleanh0902 committed Jan 23, 2024
1 parent 1dc4c25 commit 2d469e2
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 0 deletions.
113 changes: 113 additions & 0 deletions x/stakingmiddleware/keeper/keeper_test.go
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

Check failure on line 26 in x/stakingmiddleware/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

field `stakingmiddlewareKeeper` is unused (unused)
stakingKeeper banktypes.StakingKeeper

Check failure on line 27 in x/stakingmiddleware/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

field `stakingKeeper` is unused (unused)
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)
}
})
}
}
117 changes: 117 additions & 0 deletions x/stakingmiddleware/keeper/msg_server_test.go
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)
}
})
}
}

0 comments on commit 2d469e2

Please sign in to comment.