Skip to content

Commit

Permalink
chore: improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed Apr 29, 2024
1 parent 946ef8a commit a8ca88d
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
6 changes: 5 additions & 1 deletion utils/mocks/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
)

func AuraKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
return AuraKeeperWithBank(t, BankKeeper{})
}

func AuraKeeperWithBank(t testing.TB, bank types.BankKeeper) (*keeper.Keeper, sdk.Context) {
logger := log.NewNopLogger()

key := storetypes.NewKVStoreKey(types.ModuleName)
Expand All @@ -28,6 +32,6 @@ func AuraKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
runtime.ProvideEventService(),
"ausdy",
AccountKeeper{},
BankKeeper{},
bank,
), wrapper.Ctx
}
41 changes: 40 additions & 1 deletion utils/mocks/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,65 @@ package mocks
import (
"context"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/codec"
"github.com/noble-assets/aura/x/aura/types"
)

var cdc = codec.NewBech32Codec("noble")

var _ types.BankKeeper = BankKeeper{}

type BankKeeper struct{}
type BankKeeper struct {
Balances map[string]sdk.Coins
}

func (k BankKeeper) BurnCoins(ctx context.Context, moduleName string, amt sdk.Coins) error {
balance := k.Balances[moduleName]
newBalance, negative := balance.SafeSub(amt...)
if negative {
return sdkerrors.Wrapf(errors.ErrInsufficientFunds, "spendable balance %s is smaller than %s", balance, amt)
}

k.Balances[moduleName] = newBalance

return nil
}

func (k BankKeeper) MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error {
k.Balances[moduleName] = k.Balances[moduleName].Add(amt...)

return nil
}

func (k BankKeeper) SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
sender, _ := cdc.BytesToString(senderAddr)

balance := k.Balances[sender]
newBalance, negative := balance.SafeSub(amt...)
if negative {
return sdkerrors.Wrapf(errors.ErrInsufficientFunds, "spendable balance %s is smaller than %s", balance, amt)
}

k.Balances[sender] = newBalance
k.Balances[recipientModule] = k.Balances[recipientModule].Add(amt...)

return nil
}

func (k BankKeeper) SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
recipient, _ := cdc.BytesToString(recipientAddr)

balance := k.Balances[senderModule]
newBalance, negative := balance.SafeSub(amt...)
if negative {
return sdkerrors.Wrapf(errors.ErrInsufficientFunds, "spendable balance %s is smaller than %s", balance, amt)
}

k.Balances[senderModule] = newBalance
k.Balances[recipient] = k.Balances[recipient].Add(amt...)

return nil
}
98 changes: 98 additions & 0 deletions x/aura/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,111 @@ package keeper_test
import (
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/noble-assets/aura/utils"
"github.com/noble-assets/aura/utils/mocks"
"github.com/noble-assets/aura/x/aura/keeper"
"github.com/noble-assets/aura/x/aura/types"
"github.com/stretchr/testify/require"
)

var ONE = math.NewInt(1_000_000_000_000_000_000)

func TestBurn(t *testing.T) {
bank := mocks.BankKeeper{
Balances: make(map[string]sdk.Coins),
}
k, ctx := mocks.AuraKeeperWithBank(t, bank)
server := keeper.NewMsgServer(k)

// ARRANGE: Set burner in state.
burner := utils.TestAccount()
require.NoError(t, k.Burners.Set(ctx, burner.Address))

// ACT: Attempt to burn with invalid signer.
_, err := server.Burn(ctx, &types.MsgBurn{
Signer: utils.TestAccount().Address,
})
// ASSERT: The action should've failed due to invalid signer.
require.ErrorContains(t, err, types.ErrInvalidBurner.Error())

// ACT: Attempt to burn with invalid account address.
_, err = server.Burn(ctx, &types.MsgBurn{
Signer: burner.Address,
From: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
})
// ASSERT: The action should've failed due to invalid account address.
require.ErrorContains(t, err, "unable to decode account address")

// ARRANGE: Generate a user account.
user := utils.TestAccount()

// ACT: Attempt to burn from user with insufficient funds.
_, err = server.Burn(ctx, &types.MsgBurn{
Signer: burner.Address,
From: user.Address,
Amount: ONE,
})
// ASSERT: The action should've failed due to insufficient funds.
require.ErrorContains(t, err, "unable to transfer from user to module")

// ARRANGE: Give user 1 $USDY.
bank.Balances[user.Address] = sdk.NewCoins(sdk.NewCoin(k.Denom, ONE))

// ACT: Attempt to burn.
_, err = server.Burn(ctx, &types.MsgBurn{
Signer: burner.Address,
From: user.Address,
Amount: ONE,
})
// ASSERT: The action should've succeeded.
require.NoError(t, err)
require.True(t, bank.Balances[user.Address].IsZero())
require.True(t, bank.Balances[types.ModuleName].IsZero())
}

func TestMint(t *testing.T) {
bank := mocks.BankKeeper{
Balances: make(map[string]sdk.Coins),
}
k, ctx := mocks.AuraKeeperWithBank(t, bank)
server := keeper.NewMsgServer(k)

// ARRANGE: Set minter in state.
minter := utils.TestAccount()
require.NoError(t, k.Minters.Set(ctx, minter.Address))

// ACT: Attempt to mint with invalid signer.
_, err := server.Mint(ctx, &types.MsgMint{
Signer: utils.TestAccount().Address,
})
// ASSERT: The action should've failed due to invalid signer.
require.ErrorContains(t, err, types.ErrInvalidMinter.Error())

// ACT: Attempt to mint with invalid account address.
_, err = server.Mint(ctx, &types.MsgMint{
Signer: minter.Address,
To: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
})
// ASSERT: The action should've failed due to invalid account address.
require.ErrorContains(t, err, "unable to decode account address")

// ARRANGE: Generate a user account.
user := utils.TestAccount()

// ACT: Attempt to mint.
_, err = server.Mint(ctx, &types.MsgMint{
Signer: minter.Address,
To: user.Address,
Amount: ONE,
})
// ASSERT: The action should've succeeded.
require.NoError(t, err)
require.Equal(t, ONE, bank.Balances[user.Address].AmountOf(k.Denom))
require.True(t, bank.Balances[types.ModuleName].IsZero())
}

func TestPause(t *testing.T) {
k, ctx := mocks.AuraKeeper(t)
server := keeper.NewMsgServer(k)
Expand Down

0 comments on commit a8ca88d

Please sign in to comment.