Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed Sep 10, 2024
1 parent 279ddd2 commit 392207f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
36 changes: 23 additions & 13 deletions x/bank/v2/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (suite *KeeperTestSuite) mockMintCoins(moduleAcc *authtypes.ModuleAccount)
suite.authKeeper.EXPECT().GetModuleAccount(suite.ctx, moduleAcc.Name).Return(moduleAcc)
}

func (suite *KeeperTestSuite) mockSendCoinsFromModuleToAccount(moduleAcc *authtypes.ModuleAccount, _ sdk.AccAddress) {
func (suite *KeeperTestSuite) mockSendCoinsFromModuleToAccount(moduleAcc *authtypes.ModuleAccount, recv sdk.AccAddress) {
suite.authKeeper.EXPECT().GetModuleAddress(moduleAcc.Name).Return(moduleAcc.GetAddress())
}

Expand Down Expand Up @@ -127,17 +127,20 @@ func (suite *KeeperTestSuite) TestSendCoins_Acount_To_Account() {
balances := sdk.NewCoins(newFooCoin(100), newBarCoin(50))
sendAmt := sdk.NewCoins(newFooCoin(10), newBarCoin(10))

acc0Str, _ := suite.addressCodec.BytesToString(accAddrs[0])
acc1Str, _ := suite.addressCodec.BytesToString(accAddrs[1])

// Try send with empty balances
err := suite.bankKeeper.SendCoins(ctx, acc0Str, acc1Str, sendAmt)
suite.authKeeper.EXPECT().GetModuleAddress(string(accAddrs[0])).Return(nil)
suite.authKeeper.EXPECT().GetModuleAddress(string(accAddrs[1])).Return(nil)
err := suite.bankKeeper.SendCoins(ctx, accAddrs[0], accAddrs[1], sendAmt)
require.Error(err)

// Set balances for acc0 and then try send to acc1
suite.mockFundAccount(accAddrs[0])
require.NoError(banktestutil.FundAccount(ctx, suite.bankKeeper, acc0Str, balances))
require.NoError(suite.bankKeeper.SendCoins(ctx, acc0Str, acc1Str, sendAmt))
suite.authKeeper.EXPECT().GetModuleAddress(string(accAddrs[0])).Return(nil)
require.NoError(banktestutil.FundAccount(ctx, suite.bankKeeper, accAddrs[0], balances))

suite.authKeeper.EXPECT().GetModuleAddress(string(accAddrs[0])).Return(nil)
suite.authKeeper.EXPECT().GetModuleAddress(string(accAddrs[1])).Return(nil)
require.NoError(suite.bankKeeper.SendCoins(ctx, accAddrs[0], accAddrs[1], sendAmt))

// Check balances
acc0FooBalance := suite.bankKeeper.GetBalance(ctx, accAddrs[0], fooDenom)
Expand All @@ -162,12 +165,14 @@ func (suite *KeeperTestSuite) TestSendCoins_Module_To_Account() {

// Try send from invalid module
suite.authKeeper.EXPECT().GetModuleAddress("invalid").Return(nil)
err := suite.bankKeeper.SendCoins(ctx, "invalid", acc4Str, balances)
suite.authKeeper.EXPECT().GetModuleAddress(acc4Str).Return(nil)
err := suite.bankKeeper.SendCoins(ctx, []byte("invalid"), []byte(acc4Str), balances)
require.Error(err)

// Send from mint module
suite.authKeeper.EXPECT().GetModuleAddress(mintAcc.Name).Return(mintAcc.GetAddress())
err = suite.bankKeeper.SendCoins(ctx, banktypes.MintModuleName, acc4Str, balances)
suite.authKeeper.EXPECT().GetModuleAddress(string(accAddrs[4])).Return(nil)
err = suite.bankKeeper.SendCoins(ctx, []byte(banktypes.MintModuleName), accAddrs[4], balances)
require.NoError(err)

// Check balances
Expand All @@ -184,28 +189,33 @@ func (suite *KeeperTestSuite) TestSendCoins_Module_To_Account() {
func (suite *KeeperTestSuite) TestSendCoins_Module_To_Module() {
ctx := suite.ctx
require := suite.Require()
balances := sdk.NewCoins(newFooCoin(100), newBarCoin(50))
balances := sdk.NewCoins(newFooCoin(200), newBarCoin(100))

suite.mockMintCoins(mintAcc)
require.NoError(suite.bankKeeper.MintCoins(ctx, banktypes.MintModuleName, balances))

// Try send to invalid module
// In this case it will create a new account
suite.authKeeper.EXPECT().GetModuleAddress(mintAcc.Name).Return(mintAcc.GetAddress())
suite.authKeeper.EXPECT().GetModuleAddress("invalid").Return(nil)
err := suite.bankKeeper.SendCoins(ctx, mintAcc.Name, "invalid", balances)
require.Error(err)
err := suite.bankKeeper.SendCoins(ctx, []byte(mintAcc.Name), []byte("invalid"), sdk.NewCoins(newFooCoin(100), newBarCoin(50)))
require.NoError(err)

// Send from mint module to burn module
suite.authKeeper.EXPECT().GetModuleAddress(mintAcc.Name).Return(mintAcc.GetAddress())
suite.authKeeper.EXPECT().GetModuleAddress(burnerAcc.Name).Return(burnerAcc.GetAddress())
err = suite.bankKeeper.SendCoins(ctx, mintAcc.Name, burnerAcc.Name, balances)
err = suite.bankKeeper.SendCoins(ctx, []byte(mintAcc.Name), []byte(burnerAcc.Name), sdk.NewCoins(newFooCoin(100), newBarCoin(50)))
require.NoError(err)

// Check balances
burnerFooBalance := suite.bankKeeper.GetBalance(ctx, burnerAcc.GetAddress(), fooDenom)
require.Equal(burnerFooBalance.Amount, math.NewInt(100))
burnerBarBalance := suite.bankKeeper.GetBalance(ctx, burnerAcc.GetAddress(), barDenom)
require.Equal(burnerBarBalance.Amount, math.NewInt(50))
invalidFooBalance := suite.bankKeeper.GetBalance(ctx, []byte("invalid"), fooDenom)
require.Equal(invalidFooBalance.Amount, math.NewInt(100))
invalidBarBalance := suite.bankKeeper.GetBalance(ctx, []byte("invalid"), barDenom)
require.Equal(invalidBarBalance.Amount, math.NewInt(50))
mintFooBalance := suite.bankKeeper.GetBalance(ctx, mintAcc.GetAddress(), fooDenom)
require.Equal(mintFooBalance.Amount, math.NewInt(0))
mintBarBalance := suite.bankKeeper.GetBalance(ctx, mintAcc.GetAddress(), barDenom)
Expand Down
5 changes: 2 additions & 3 deletions x/bank/v2/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
// FundAccount is a utility function that funds an account by minting and
// sending the coins to the address. This should be used for testing purposes
// only!
func FundAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, addr string, amounts sdk.Coins) error {
func FundAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, addr []byte, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, types.MintModuleName, amounts); err != nil {
return err
}

return bankKeeper.SendCoins(ctx, types.MintModuleName, addr, amounts)
return bankKeeper.SendCoins(ctx, []byte(types.MintModuleName), addr, amounts)
}

0 comments on commit 392207f

Please sign in to comment.