Skip to content

Commit

Permalink
test: Add test for identical x/bank x/precisebank error and panics
Browse files Browse the repository at this point in the history
  • Loading branch information
drklee3 committed May 21, 2024
1 parent d12cc71 commit 47e5b50
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/precisebank/keeper/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (k Keeper) mintExtendedCoin(

// Update remainder with new integer coin.
// .Mod(conversionFactor) after the Add & Sub is not necessary as it
// will always be < conversionFactor
// will always be < conversionFactor because fractionalMintAmount > currentRemainder
newRemainder := currentRemainder.
Add(types.ConversionFactor()). // 1 integer coin worth of fractional amount added to reserve
Sub(fractionalMintAmount) // deduct remainder with additional minted fractional amount
Expand Down
79 changes: 79 additions & 0 deletions x/precisebank/keeper/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
Expand Down Expand Up @@ -50,6 +52,83 @@ func (suite *mintIntegrationTestSuite) TestBlockedRecipient() {
)
}

func (suite *mintIntegrationTestSuite) TestMintCoins_MatchingErrors() {
// x/precisebank MintCoins should be identical to x/bank MintCoins to
// consumers. This test ensures that the panics & errors returned by
// x/precisebank are identical to x/bank.

tests := []struct {
name string
recipientModule string
mintAmount sdk.Coins
wantErr string
wantPanic string
}{
{
"invalid module",
"notamodule",
cs(c("ukava", 1000)),
"",
"module account notamodule does not exist: unknown address",
},
{
"no mint permissions",
// Check app.go to ensure this module has no mint permissions
authtypes.FeeCollectorName,
cs(c("ukava", 1000)),
"",
"module account fee_collector does not have permissions to mint tokens: unauthorized",
},
{
"invalid amount",
minttypes.ModuleName,
sdk.Coins{sdk.Coin{Denom: "ukava", Amount: sdkmath.NewInt(-100)}},
"-100ukava: invalid coins",
"",
},
}

for _, tt := range tests {
suite.Run(tt.name, func() {
// Reset
suite.SetupTest()

if tt.wantErr == "" && tt.wantPanic == "" {
suite.Fail("test must specify either wantErr or wantPanic")
}

if tt.wantErr != "" {
// Check x/bank MintCoins for identical error
bankErr := suite.BankKeeper.MintCoins(suite.Ctx, tt.recipientModule, tt.mintAmount)
suite.Require().Error(bankErr)
suite.Require().EqualError(bankErr, tt.wantErr, "expected error should match x/bank MintCoins error")

pbankErr := suite.Keeper.MintCoins(suite.Ctx, tt.recipientModule, tt.mintAmount)
suite.Require().Error(pbankErr)
// Compare strings instead of errors, as error stack is still different
suite.Require().Equal(
bankErr.Error(),
pbankErr.Error(),
"x/precisebank error should match x/bank MintCoins error",
)
}

if tt.wantPanic != "" {
// First check the wantPanic string is correct.
// Actually specify the panic string in the test since it makes
// it more clear we are testing specific and different cases.
suite.Require().PanicsWithError(tt.wantPanic, func() {
_ = suite.BankKeeper.MintCoins(suite.Ctx, tt.recipientModule, tt.mintAmount)
}, "expected panic error should match x/bank MintCoins")

suite.Require().PanicsWithError(tt.wantPanic, func() {
_ = suite.Keeper.MintCoins(suite.Ctx, tt.recipientModule, tt.mintAmount)
}, "x/precisebank panic should match x/bank MintCoins")
}
})
}
}

func (suite *mintIntegrationTestSuite) TestMintCoins() {
type mintTest struct {
mintAmount sdk.Coins
Expand Down

0 comments on commit 47e5b50

Please sign in to comment.