From 72cade5f83576d410084f2e3fc4e32fc334182b4 Mon Sep 17 00:00:00 2001 From: bmo Date: Wed, 11 Dec 2024 08:27:29 +0000 Subject: [PATCH] chores: fix more connection tests --- .../03-connection/types/connection_test.go | 44 ++++++++++--------- .../core/03-connection/types/genesis_test.go | 28 ++++++------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/modules/core/03-connection/types/connection_test.go b/modules/core/03-connection/types/connection_test.go index ba88f05b8e9..d789662c77a 100644 --- a/modules/core/03-connection/types/connection_test.go +++ b/modules/core/03-connection/types/connection_test.go @@ -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" ) @@ -24,32 +26,32 @@ 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, }, } @@ -57,10 +59,10 @@ func TestConnectionValidateBasic(t *testing.T) { 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) } } } @@ -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) } } } @@ -93,17 +95,17 @@ 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, }, } @@ -111,10 +113,10 @@ func TestIdentifiedConnectionValidateBasic(t *testing.T) { 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) } } -} +} \ No newline at end of file diff --git a/modules/core/03-connection/types/genesis_test.go b/modules/core/03-connection/types/genesis_test.go index 39a930c1433..96c43cebaf3 100644 --- a/modules/core/03-connection/types/genesis_test.go +++ b/modules/core/03-connection/types/genesis_test.go @@ -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" @@ -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", @@ -34,7 +36,7 @@ func TestValidateGenesis(t *testing.T) { 0, types.DefaultParams(), ), - expPass: true, + expError: nil, }, { name: "invalid connection", @@ -48,7 +50,7 @@ func TestValidateGenesis(t *testing.T) { 0, types.DefaultParams(), ), - expPass: false, + expError: host.ErrInvalidID, }, { name: "invalid client id", @@ -62,7 +64,7 @@ func TestValidateGenesis(t *testing.T) { 0, types.DefaultParams(), ), - expPass: false, + expError: host.ErrInvalidID, }, { name: "invalid path", @@ -76,7 +78,7 @@ func TestValidateGenesis(t *testing.T) { 0, types.DefaultParams(), ), - expPass: false, + expError: host.ErrInvalidID, }, { name: "invalid connection identifier", @@ -90,7 +92,7 @@ func TestValidateGenesis(t *testing.T) { 0, types.DefaultParams(), ), - expPass: false, + expError: host.ErrInvalidID, }, { name: "localhost connection identifier", @@ -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", @@ -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", @@ -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()) } } -} +} \ No newline at end of file