Skip to content

Commit

Permalink
chores: fix more connection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
biemoh committed Dec 11, 2024
1 parent fe835d5 commit 72cade5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
44 changes: 23 additions & 21 deletions modules/core/03-connection/types/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

"github.com/stretchr/testify/require"

host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
)

Expand All @@ -24,43 +26,43 @@ func TestConnectionValidateBasic(t *testing.T) {
testCases := []struct {
name string
connection types.ConnectionEnd
expPass bool
expError error
}{
{
"valid connection",
types.ConnectionEnd{clientID, []*types.Version{ibctesting.ConnectionVersion}, types.INIT, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, 500},
true,
nil,
},
{
"invalid client id",
types.ConnectionEnd{"(clientID1)", []*types.Version{ibctesting.ConnectionVersion}, types.INIT, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, 500},
false,
host.ErrInvalidID,
},
{
"empty versions",
types.ConnectionEnd{clientID, nil, types.INIT, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, 500},
false,
ibcerrors.ErrInvalidVersion,
},
{
"invalid version",
types.ConnectionEnd{clientID, []*types.Version{{}}, types.INIT, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, 500},
false,
types.ErrInvalidVersion,
},
{
"invalid counterparty",
types.ConnectionEnd{clientID, []*types.Version{ibctesting.ConnectionVersion}, types.INIT, types.Counterparty{clientID2, connectionID2, emptyPrefix}, 500},
false,
types.ErrInvalidCounterparty,
},
}

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

err := tc.connection.ValidateBasic()
if tc.expPass {
if tc.expError == 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.expError)
}
}
}
Expand All @@ -69,22 +71,22 @@ func TestCounterpartyValidateBasic(t *testing.T) {
testCases := []struct {
name string
counterparty types.Counterparty
expPass bool
expError error
}{
{"valid counterparty", types.Counterparty{clientID, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, true},
{"invalid client id", types.Counterparty{"(InvalidClient)", connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, false},
{"invalid connection id", types.Counterparty{clientID, "(InvalidConnection)", commitmenttypes.NewMerklePrefix([]byte("prefix"))}, false},
{"invalid prefix", types.Counterparty{clientID, connectionID2, emptyPrefix}, false},
{"valid counterparty", types.Counterparty{clientID, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, nil},
{"invalid client id", types.Counterparty{"(InvalidClient)", connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, host.ErrInvalidID},
{"invalid connection id", types.Counterparty{clientID, "(InvalidConnection)", commitmenttypes.NewMerklePrefix([]byte("prefix"))}, host.ErrInvalidID},
{"invalid prefix", types.Counterparty{clientID, connectionID2, emptyPrefix}, types.ErrInvalidCounterparty},
}

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

err := tc.counterparty.ValidateBasic()
if tc.expPass {
if tc.expError == 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.expError)
}
}
}
Expand All @@ -93,28 +95,28 @@ func TestIdentifiedConnectionValidateBasic(t *testing.T) {
testCases := []struct {
name string
connection types.IdentifiedConnection
expPass bool
expError error
}{
{
"valid connection",
types.NewIdentifiedConnection(clientID, types.ConnectionEnd{clientID, []*types.Version{ibctesting.ConnectionVersion}, types.INIT, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, 500}),
true,
nil,
},
{
"invalid connection id",
types.NewIdentifiedConnection("(connectionIDONE)", types.ConnectionEnd{clientID, []*types.Version{ibctesting.ConnectionVersion}, types.INIT, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, 500}),
false,
host.ErrInvalidID,
},
}

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

err := tc.connection.ValidateBasic()
if tc.expPass {
if tc.expError == 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.expError)
}
}
}
}
28 changes: 15 additions & 13 deletions modules/core/03-connection/types/genesis_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/03-connection/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
Expand All @@ -15,12 +17,12 @@ func TestValidateGenesis(t *testing.T) {
testCases := []struct {
name string
genState types.GenesisState
expPass bool
expError error
}{
{
name: "default",
genState: types.DefaultGenesisState(),
expPass: true,
expError: nil,
},
{
name: "valid genesis",
Expand All @@ -34,7 +36,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: true,
expError: nil,
},
{
name: "invalid connection",
Expand All @@ -48,7 +50,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: false,
expError: host.ErrInvalidID,
},
{
name: "invalid client id",
Expand All @@ -62,7 +64,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: false,
expError: host.ErrInvalidID,
},
{
name: "invalid path",
Expand All @@ -76,7 +78,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: false,
expError: host.ErrInvalidID,
},
{
name: "invalid connection identifier",
Expand All @@ -90,7 +92,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: false,
expError: host.ErrInvalidID,
},
{
name: "localhost connection identifier",
Expand All @@ -104,7 +106,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: true,
expError: nil,
},
{
name: "next connection sequence is not greater than maximum connection identifier sequence provided",
Expand All @@ -118,7 +120,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.DefaultParams(),
),
expPass: false,
expError: errors.New("next connection sequence 0 must be greater than maximum sequence used in connection identifier 10"),
},
{
name: "invalid params",
Expand All @@ -132,17 +134,17 @@ func TestValidateGenesis(t *testing.T) {
0,
types.Params{},
),
expPass: false,
expError: errors.New("MaxExpectedTimePerBlock cannot be zero"),
},
}

for _, tc := range testCases {
tc := tc
err := tc.genState.Validate()
if tc.expPass {
if tc.expError == nil {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
require.ErrorContains(t, err, tc.expError.Error())
}
}
}
}

0 comments on commit 72cade5

Please sign in to comment.