Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: expect specific errors in apps/transfer #7205

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions modules/apps/transfer/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"errors"
"fmt"

sdkmath "cosmossdk.io/math"
Expand All @@ -21,7 +22,7 @@ func (suite *KeeperTestSuite) TestQueryDenom() {
testCases := []struct {
msg string
malleate func()
expPass bool
expErr error
}{
{
"success: correct ibc denom",
Expand All @@ -37,7 +38,7 @@ func (suite *KeeperTestSuite) TestQueryDenom() {
Hash: expDenom.IBCDenom(),
}
},
true,
nil,
},
{
"success: correct hex hash",
Expand All @@ -53,7 +54,7 @@ func (suite *KeeperTestSuite) TestQueryDenom() {
Hash: expDenom.Hash().String(),
}
},
true,
nil,
},
{
"failure: invalid hash",
Expand All @@ -62,7 +63,7 @@ func (suite *KeeperTestSuite) TestQueryDenom() {
Hash: "!@#!@#!",
}
},
false,
errors.New("invalid denom trace hash"),
},
{
"failure: not found denom trace",
Expand All @@ -77,7 +78,7 @@ func (suite *KeeperTestSuite) TestQueryDenom() {
Hash: expDenom.IBCDenom(),
}
},
false,
errors.New("denomination not found"),
},
}

Expand All @@ -91,12 +92,12 @@ func (suite *KeeperTestSuite) TestQueryDenom() {

res, err := suite.chainA.GetSimApp().TransferKeeper.Denom(ctx, req)

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(&expDenom, res.Denom)
} else {
suite.Require().Error(err)
ibctesting.RequireErrorIsOrContains(suite.T(), err, tc.expErr, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in a separate PR we should update other tests where this function could be used... More refactoring of tests, yay!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely! But I think after these I'll take a break from refactoring tests :D

}
})
}
Expand All @@ -111,14 +112,14 @@ func (suite *KeeperTestSuite) TestQueryDenoms() {
testCases := []struct {
msg string
malleate func()
expPass bool
expErr error
}{
{
"empty pagination",
func() {
req = &types.QueryDenomsRequest{}
},
true,
nil,
},
{
"success",
Expand All @@ -138,7 +139,7 @@ func (suite *KeeperTestSuite) TestQueryDenoms() {
},
}
},
true,
nil,
},
}

Expand All @@ -152,12 +153,12 @@ func (suite *KeeperTestSuite) TestQueryDenoms() {

res, err := suite.chainA.GetSimApp().TransferKeeper.Denoms(ctx, req)

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expDenoms.Sort(), res.Denoms)
} else {
suite.Require().Error(err)
ibctesting.RequireErrorIsOrContains(suite.T(), err, tc.expErr, err.Error())
}
})
}
Expand All @@ -181,16 +182,21 @@ func (suite *KeeperTestSuite) TestQueryDenomHash() {
testCases := []struct {
msg string
malleate func()
expPass bool
expErr error
}{
{
"success",
func() {},
nil,
},
{
"invalid trace",
func() {
req = &types.QueryDenomHashRequest{
Trace: "transfer/channelToA/transfer/",
Trace: "transfer%%/channel-1/transfer/channel-1/uatom",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was not triggering the "invalid trace" error but the "denom not found one". Thanks @crodriguezvega for the help!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the %% what triggers invalid trace here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my understanding yes, but this is all @crodriguezvega!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct. The %% triggers invalid trace error.

}
},
false,
errors.New("invalid trace"),
},
{
"not found denom trace",
Expand All @@ -199,12 +205,7 @@ func (suite *KeeperTestSuite) TestQueryDenomHash() {
Trace: "transfer/channelToC/uatom",
}
},
false,
},
{
"success",
func() {},
true,
Comment on lines -204 to -207
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved on top

errors.New("denomination not found"),
},
}

Expand All @@ -223,12 +224,12 @@ func (suite *KeeperTestSuite) TestQueryDenomHash() {

res, err := suite.chainA.GetSimApp().TransferKeeper.DenomHash(ctx, req)

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expHash, res.Hash)
} else {
suite.Require().Error(err)
ibctesting.RequireErrorIsOrContains(suite.T(), err, tc.expErr, err.Error())
}
})
}
Expand All @@ -241,7 +242,7 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {
testCases := []struct {
msg string
malleate func()
expPass bool
expErr error
}{
{
"success",
Expand All @@ -251,7 +252,7 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {
ChannelId: path.EndpointA.ChannelID,
}
},
true,
nil,
},
{
"failure - channel not found",
Expand All @@ -261,7 +262,7 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {
ChannelId: ibctesting.FirstChannelID,
}
},
false,
errors.New("channel not found"),
},
{
"failure - empty channelID",
Expand All @@ -271,7 +272,7 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {
ChannelId: "",
}
},
false,
errors.New("identifier cannot be blank"),
},
{
"failure - empty portID",
Expand All @@ -281,7 +282,7 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {
ChannelId: ibctesting.FirstChannelID,
}
},
false,
errors.New("identifier cannot be blank"),
},
}

Expand All @@ -297,12 +298,12 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {

res, err := suite.chainA.GetSimApp().TransferKeeper.EscrowAddress(ctx, req)

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
expected := types.GetEscrowAddress(ibctesting.TransferPort, path.EndpointA.ChannelID).String()
suite.Require().Equal(expected, res.EscrowAddress)
} else {
suite.Require().Error(err)
ibctesting.RequireErrorIsOrContains(suite.T(), err, tc.expErr, err.Error())
}
})
}
Expand All @@ -317,7 +318,7 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {
testCases := []struct {
msg string
malleate func()
expPass bool
expErr error
}{
{
"valid native denom with escrow amount < 2^63",
Expand All @@ -329,7 +330,7 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {
expEscrowAmount = sdkmath.NewInt(100)
suite.chainA.GetSimApp().TransferKeeper.SetTotalEscrowForDenom(suite.chainA.GetContext(), sdk.NewCoin(sdk.DefaultBondDenom, expEscrowAmount))
},
true,
nil,
},
{
"valid ibc denom with escrow amount > 2^63",
Expand All @@ -345,7 +346,7 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {
Denom: denom.IBCDenom(),
}
},
true,
nil,
},
{
"valid ibc denom treated as native denom",
Expand All @@ -356,7 +357,7 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {
Denom: denom.IBCDenom(),
}
},
true, // denom trace is not found, thus the denom is considered a native token
nil, // denom trace is not found, thus the denom is considered a native token
},
{
"invalid ibc denom treated as valid native denom",
Expand All @@ -365,7 +366,7 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {
Denom: "ibc/123",
}
},
true, // the ibc denom does not contain a valid hash, thus the denom is considered a native token
nil, // the ibc denom does not contain a valid hash, thus the denom is considered a native token
},
{
"invalid denom",
Expand All @@ -374,7 +375,7 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {
Denom: "??𓃠🐾??",
}
},
false,
errors.New("invalid denom"),
},
}

Expand All @@ -389,10 +390,11 @@ func (suite *KeeperTestSuite) TestTotalEscrowForDenom() {

res, err := suite.chainA.GetSimApp().TransferKeeper.TotalEscrowForDenom(ctx, req)

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
suite.Require().Equal(expEscrowAmount, res.Amount.Amount)
} else {
ibctesting.RequireErrorIsOrContains(suite.T(), err, tc.expErr, err.Error())
suite.Require().Error(err)
}
})
Expand Down
31 changes: 16 additions & 15 deletions modules/apps/transfer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
testCases := []struct {
name string
instantiateFn func()
expPass bool
panicMsg string
}{
{"success", func() {
keeper.NewKeeper(
Expand All @@ -67,7 +67,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
suite.chainA.GetSimApp().ScopedTransferKeeper,
suite.chainA.GetSimApp().ICAControllerKeeper.GetAuthority(),
)
}, true},
}, ""},
{"failure: transfer module account does not exist", func() {
keeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
Expand All @@ -81,7 +81,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
suite.chainA.GetSimApp().ScopedTransferKeeper,
suite.chainA.GetSimApp().ICAControllerKeeper.GetAuthority(),
)
}, false},
}, "the IBC transfer module account has not been set"},
{"failure: empty authority", func() {
keeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
Expand All @@ -95,20 +95,21 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
suite.chainA.GetSimApp().ScopedTransferKeeper,
"", // authority
)
}, false},
}, "authority must be non-empty"},
}

for _, tc := range testCases {
tc := tc
suite.SetupTest()

suite.Run(tc.name, func() {
if tc.expPass {
if tc.panicMsg == "" {
suite.Require().NotPanics(
tc.instantiateFn,
)
} else {
suite.Require().Panics(
suite.Require().PanicsWithError(
tc.panicMsg,
tc.instantiateFn,
)
}
Expand Down Expand Up @@ -325,15 +326,15 @@ func (suite *KeeperTestSuite) TestGetAllForwardedPackets() {

func (suite *KeeperTestSuite) TestParams() {
testCases := []struct {
name string
input types.Params
expPass bool
name string
input types.Params
panicMsg string
}{
// it is not possible to set invalid booleans
{"success: set params false-false", types.NewParams(false, false), true},
{"success: set params false-true", types.NewParams(false, true), true},
{"success: set params true-false", types.NewParams(true, false), true},
{"success: set params true-true", types.NewParams(true, true), true},
{"success: set params false-false", types.NewParams(false, false), ""},
{"success: set params false-true", types.NewParams(false, true), ""},
{"success: set params true-false", types.NewParams(true, false), ""},
{"success: set params true-true", types.NewParams(true, true), ""},
}

for _, tc := range testCases {
Expand All @@ -342,13 +343,13 @@ func (suite *KeeperTestSuite) TestParams() {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
ctx := suite.chainA.GetContext()
if tc.expPass {
if tc.panicMsg == "" {
suite.chainA.GetSimApp().TransferKeeper.SetParams(ctx, tc.input)
expected := tc.input
p := suite.chainA.GetSimApp().TransferKeeper.GetParams(ctx)
suite.Require().Equal(expected, p)
} else {
suite.Require().Panics(func() {
suite.Require().PanicsWithError(tc.panicMsg, func() {
suite.chainA.GetSimApp().TransferKeeper.SetParams(ctx, tc.input)
})
}
Expand Down
Loading
Loading