Skip to content

Commit

Permalink
fix tests: update tests in 04-channel/types , use expected errors (#7680
Browse files Browse the repository at this point in the history
)

* fix tests in 04-channel types

* fix test

* Update modules/core/04-channel/types/keys_test.go

* revert error

---------

Co-authored-by: Đông Liều <[email protected]>
Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent ebab69a commit d5e329c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
28 changes: 15 additions & 13 deletions modules/core/04-channel/types/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ func TestChannelValidateBasic(t *testing.T) {
testCases := []struct {
name string
channel types.Channel
expPass bool
expErr error
}{
{"valid channel", types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, connHops, version), true},
{"invalid state", types.NewChannel(types.UNINITIALIZED, types.ORDERED, counterparty, connHops, version), false},
{"invalid order", types.NewChannel(types.TRYOPEN, types.NONE, counterparty, connHops, version), false},
{"more than 1 connection hop", types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, []string{"connection1", "connection2"}, version), false},
{"invalid connection hop identifier", types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, []string{"(invalid)"}, version), false},
{"invalid counterparty", types.NewChannel(types.TRYOPEN, types.ORDERED, types.NewCounterparty("(invalidport)", "channelidone"), connHops, version), false},
{"valid channel", types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, connHops, version), nil},
{"invalid state", types.NewChannel(types.UNINITIALIZED, types.ORDERED, counterparty, connHops, version), types.ErrInvalidChannelState},
{"invalid order", types.NewChannel(types.TRYOPEN, types.NONE, counterparty, connHops, version), types.ErrInvalidChannelOrdering},
{"more than 1 connection hop", types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, []string{"connection1", "connection2"}, version), types.ErrTooManyConnectionHops},
{"invalid connection hop identifier", types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, []string{"(invalid)"}, version), host.ErrInvalidID},
{"invalid counterparty", types.NewChannel(types.TRYOPEN, types.ORDERED, types.NewCounterparty("(invalidport)", "channelidone"), connHops, version), host.ErrInvalidID},
}

for i, tc := range testCases {
tc := tc

err := tc.channel.ValidateBasic()
if tc.expPass {
if tc.expErr == nil {
require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)
} else {
require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)
require.ErrorIs(t, err, tc.expErr)
}
}
}
Expand All @@ -40,21 +41,22 @@ func TestCounterpartyValidateBasic(t *testing.T) {
testCases := []struct {
name string
counterparty types.Counterparty
expPass bool
expErr error
}{
{"valid counterparty", types.Counterparty{"portidone", "channelidone"}, true},
{"invalid port id", types.Counterparty{"(InvalidPort)", "channelidone"}, false},
{"invalid channel id", types.Counterparty{"portidone", "(InvalidChannel)"}, false},
{"valid counterparty", types.Counterparty{"portidone", "channelidone"}, nil},
{"invalid port id", types.Counterparty{"(InvalidPort)", "channelidone"}, host.ErrInvalidID},
{"invalid channel id", types.Counterparty{"portidone", "(InvalidChannel)"}, host.ErrInvalidID},
}

for i, tc := range testCases {
tc := tc

err := tc.counterparty.ValidateBasic()
if tc.expPass {
if tc.expErr == nil {
require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)
} else {
require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)
require.ErrorIs(t, err, tc.expErr)
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions modules/core/04-channel/types/keys_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package types_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

// tests ParseChannelSequence and IsValidChannelID
Expand All @@ -14,20 +16,20 @@ func TestParseChannelSequence(t *testing.T) {
name string
channelID string
expSeq uint64
expPass bool
expErr error
}{
{"valid 0", "channel-0", 0, true},
{"valid 1", "channel-1", 1, true},
{"valid large sequence", "channel-234568219356718293", 234568219356718293, true},
{"valid 0", "channel-0", 0, nil},
{"valid 1", "channel-1", 1, nil},
{"valid large sequence", "channel-234568219356718293", 234568219356718293, nil},
// one above uint64 max
{"invalid uint64", "channel-18446744073709551616", 0, false},
{"invalid uint64", "channel-18446744073709551616", 0, errors.New("invalid channel identifier: failed to parse identifier sequence")},
// uint64 == 20 characters
{"invalid large sequence", "channel-2345682193567182931243", 0, false},
{"capital prefix", "Channel-0", 0, false},
{"missing dash", "channel0", 0, false},
{"blank id", " ", 0, false},
{"empty id", "", 0, false},
{"negative sequence", "channel--1", 0, false},
{"invalid large sequence", "channel-2345682193567182931243", 0, host.ErrInvalidID},
{"capital prefix", "Channel-0", 0, host.ErrInvalidID},
{"missing dash", "channel0", 0, host.ErrInvalidID},
{"blank id", " ", 0, host.ErrInvalidID},
{"empty id", "", 0, host.ErrInvalidID},
{"negative sequence", "channel--1", 0, host.ErrInvalidID},
}

for _, tc := range testCases {
Expand All @@ -37,12 +39,13 @@ func TestParseChannelSequence(t *testing.T) {
valid := types.IsValidChannelID(tc.channelID)
require.Equal(t, tc.expSeq, seq)

if tc.expPass {
if tc.expErr == nil {
require.NoError(t, err, tc.name)
require.True(t, valid)
} else {
require.Error(t, err, tc.name)
require.False(t, valid)
require.ErrorContains(t, err, tc.expErr.Error())
}
}
}
8 changes: 2 additions & 6 deletions modules/core/04-channel/types/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (suite *TypesTestSuite) TestIsValid() {
testCases := []struct {
name string
malleate func()
expPass bool
isValid bool
}{
{
"success: valid timeout with height and timestamp",
Expand Down Expand Up @@ -51,11 +51,7 @@ func (suite *TypesTestSuite) TestIsValid() {
tc.malleate()

isValid := timeout.IsValid()
if tc.expPass {
suite.Require().True(isValid)
} else {
suite.Require().False(isValid)
}
suite.Require().Equal(tc.isValid, isValid)
})
}
}
Expand Down
15 changes: 8 additions & 7 deletions modules/core/04-channel/types/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,41 @@ func (suite *TypesTestSuite) TestUpgradeValidateBasic() {
testCases := []struct {
name string
malleate func()
expPass bool
expErr error
}{
{
"success",
func() {},
true,
nil,
},
{
"invalid ordering",
func() {
upgrade.Fields.Ordering = types.NONE
},
false,
types.ErrInvalidChannelOrdering,
},
{
"connection hops length not equal to 1",
func() {
upgrade.Fields.ConnectionHops = []string{"connection-0", "connection-1"}
},
false,
types.ErrTooManyConnectionHops,
},
{
"empty version",
func() {
upgrade.Fields.Version = " "
},
false,
types.ErrInvalidChannelVersion,
},
{
"invalid timeout",
func() {
upgrade.Timeout.Height = clienttypes.ZeroHeight()
upgrade.Timeout.Timestamp = 0
},
false,
types.ErrInvalidUpgrade,
},
}

Expand All @@ -69,10 +69,11 @@ func (suite *TypesTestSuite) TestUpgradeValidateBasic() {

err := upgrade.ValidateBasic()

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expErr)
}
})
}
Expand Down

0 comments on commit d5e329c

Please sign in to comment.