Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NagaTulasi committed Dec 10, 2024
1 parent f627b43 commit 3b5e9fa
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 100 deletions.
16 changes: 9 additions & 7 deletions modules/core/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ibc_test

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -62,12 +63,12 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
testCases := []struct {
name string
genState *types.GenesisState
expPass bool
expError error
}{
{
name: "default",
genState: types.DefaultGenesisState(),
expPass: true,
expError: nil,
},
{
name: "valid genesis",
Expand Down Expand Up @@ -145,7 +146,7 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
channeltypes.Params{UpgradeTimeout: channeltypes.DefaultTimeout},
),
},
expPass: true,
expError: nil,
},
{
name: "invalid client genesis",
Expand All @@ -172,7 +173,7 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
),
ConnectionGenesis: connectiontypes.DefaultGenesisState(),
},
expPass: false,
expError: errors.New("genesis metadata key cannot be empty"),
},
{
name: "invalid connection genesis",
Expand All @@ -189,7 +190,7 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
connectiontypes.Params{},
),
},
expPass: false,
expError: errors.New("invalid connection"),
},
{
name: "invalid channel genesis",
Expand All @@ -202,17 +203,18 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
},
},
},
expPass: false,
expError: errors.New("invalid acknowledgement"),
},
}

for _, tc := range testCases {
tc := tc
err := tc.genState.Validate()
if tc.expPass {
if tc.expError == nil {
suite.Require().NoError(err, tc.name)
} else {
suite.Require().Error(err, tc.name)
suite.Require().Contains(err.Error(), tc.expError.Error())
}
}
}
Expand Down
73 changes: 44 additions & 29 deletions modules/core/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,39 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
testCases := []struct {
name string
malleate func()
expPass bool
expPanic string
}{
{"failure: empty upgrade keeper value", func() {
emptyUpgradeKeeperValue := upgradekeeper.Keeper{}

upgradeKeeper = emptyUpgradeKeeperValue
}, false},
{"failure: empty upgrade keeper pointer", func() {
emptyUpgradeKeeperPointer := &upgradekeeper.Keeper{}

upgradeKeeper = emptyUpgradeKeeperPointer
}, false},
{"failure: empty authority", func() {
newIBCKeeperFn = func() {
ibckeeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)),
suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName),
upgradeKeeper,
"", // authority
)
}
}, false},
{
name: "failure: empty upgrade keeper value",
malleate: func() {
emptyUpgradeKeeperValue := upgradekeeper.Keeper{}
upgradeKeeper = emptyUpgradeKeeperValue
},
expPanic: "cannot initialize IBC keeper: empty upgrade keeper",
},
{
name: "failure: empty upgrade keeper pointer",
malleate: func() {
emptyUpgradeKeeperPointer := &upgradekeeper.Keeper{}
upgradeKeeper = emptyUpgradeKeeperPointer
},
expPanic: "cannot initialize IBC keeper: empty upgrade keeper",
},
{
name: "failure: empty authority",
malleate: func() {
newIBCKeeperFn = func() {
ibckeeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)),
suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName),
upgradeKeeper,
"", // authority
)
}
},
expPanic: "authority cannot be empty",
},
}

for _, tc := range testCases {
Expand All @@ -96,14 +106,19 @@ func (suite *KeeperTestSuite) TestNewKeeper() {

tc.malleate()

if tc.expPass {
suite.Require().NotPanics(
newIBCKeeperFn,
)
if tc.expPanic != "" {
suite.Require().Panics(func() {
newIBCKeeperFn()
}, "expected panic but no panic occurred")

defer func() {
if r := recover(); r != nil {
suite.Require().Contains(r.(error).Error(), tc.expPanic, "unexpected panic message")
}
}()

} else {
suite.Require().Panics(
newIBCKeeperFn,
)
suite.Require().NotPanics(newIBCKeeperFn, "unexpected panic occurred")
}
})
}
Expand Down
Loading

0 comments on commit 3b5e9fa

Please sign in to comment.