Skip to content

Commit

Permalink
(chore) change tests in 23-commitment/types to expect specific error (#…
Browse files Browse the repository at this point in the history
…7652)

* fix: fixed tests

* Update modules/core/23-commitment/types/codec_test.go

---------

Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
NagaTulasi and DimitrisJim authored Dec 10, 2024
1 parent 30352fb commit 5cce994
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
20 changes: 11 additions & 9 deletions modules/core/23-commitment/types/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types_test

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"

Expand All @@ -13,27 +15,27 @@ func (suite *MerkleTestSuite) TestCodecTypeRegistration() {
testCases := []struct {
name string
typeURL string
expPass bool
expErr error
}{
{
"success: MerkleRoot",
sdk.MsgTypeURL(&types.MerkleRoot{}),
true,
nil,
},
{
"success: MerklePrefix",
sdk.MsgTypeURL(&types.MerklePrefix{}),
true,
nil,
},
{
"success: MerklePath",
sdk.MsgTypeURL(&v2.MerklePath{}),
true,
nil,
},
{
"type not registered on codec",
"ibc.invalid.MsgTypeURL",
false,
fmt.Errorf("unable to resolve type URL ibc.invalid.MsgTypeURL"),
},
}

Expand All @@ -44,12 +46,12 @@ func (suite *MerkleTestSuite) TestCodecTypeRegistration() {
encodingCfg := moduletestutil.MakeTestEncodingConfig(ibc.AppModuleBasic{})
msg, err := encodingCfg.Codec.InterfaceRegistry().Resolve(tc.typeURL)

if tc.expPass {
suite.Require().NotNil(msg)
if tc.expErr == nil {
suite.NotNil(msg)
suite.Require().NoError(err)
} else {
suite.Require().Nil(msg)
suite.Require().Error(err)
suite.Nil(msg)
suite.Require().ErrorContains(err, tc.expErr.Error())
}
})
}
Expand Down
17 changes: 9 additions & 8 deletions modules/core/23-commitment/types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (suite *MerkleTestSuite) TestConvertProofs() {
name string
malleate func()
keyExists bool
expPass bool
expErr error
}{
{
"success for ExistenceProof",
Expand All @@ -41,13 +41,13 @@ func (suite *MerkleTestSuite) TestConvertProofs() {

proofOps = res.ProofOps
},
true, true,
true, nil,
},
{
"success for NonexistenceProof",
func() {
res, err := suite.store.Query(&storetypes.RequestQuery{
Path: fmt.Sprintf("/%s/key", suite.storeKey.Name()), // required path to get key/value+proof
Path: fmt.Sprintf("/%s/key", suite.storeKey.Name()),
Data: []byte("NOTMYKEY"),
Prove: true,
})
Expand All @@ -56,14 +56,14 @@ func (suite *MerkleTestSuite) TestConvertProofs() {

proofOps = res.ProofOps
},
false, true,
false, nil,
},
{
"nil proofOps",
func() {
proofOps = nil
},
true, false,
true, types.ErrInvalidMerkleProof,
},
{
"proof op data is nil",
Expand All @@ -79,7 +79,7 @@ func (suite *MerkleTestSuite) TestConvertProofs() {
proofOps = res.ProofOps
proofOps.Ops[0].Data = nil
},
true, false,
true, types.ErrInvalidMerkleProof,
},
}

Expand All @@ -89,17 +89,18 @@ func (suite *MerkleTestSuite) TestConvertProofs() {
tc.malleate()

proof, err := types.ConvertProofs(proofOps)
if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err, "ConvertProofs unexpectedly returned error for case: %s", tc.name)
if tc.keyExists {
err := proof.VerifyMembership(types.GetSDKSpecs(), &root, existsPath, value)
suite.Require().NoError(err, "converted proof failed to verify membership for case: %s", tc.name)
} else {
err := proof.VerifyNonMembership(types.GetSDKSpecs(), &root, nonexistPath)
suite.Require().NoError(err, "converted proof failed to verify membership for case: %s", tc.name)
suite.Require().NoError(err, "converted proof failed to verify non-membership for case: %s", tc.name)
}
} else {
suite.Require().Error(err, "ConvertProofs passed on invalid case for case: %s", tc.name)
suite.Require().ErrorIs(err, tc.expErr, "unexpected error returned for case: %s", tc.name)
}
}
}

0 comments on commit 5cce994

Please sign in to comment.