Skip to content

Commit

Permalink
address the coderait suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Mar 5, 2024
1 parent d56b807 commit 016438e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
52 changes: 38 additions & 14 deletions x/uibc/gmp/gmp_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,44 @@ func TestGmpMemoHandler(t *testing.T) {
logger := log.NewNopLogger()
ctx := sdk.NewContext(nil, tmproto.Header{}, false, logger)

// invalid memo
invalidMemo := "invalid memp"
err := gmpHandler.OnRecvPacket(ctx, sdk.Coin{}, invalidMemo, nil)
assert.ErrorContains(t, err, "invalid character")
tests := []struct {
name string
memo func() string
errMsg string
}{
{
name: "invalid memo",
memo: func() string {
return "invalid memo"
},
errMsg: "invalid character",
},
{
name: "valid memo",
memo: func() string {
// valid memo
validMemo := Message{
SourceChain: "source_chain",
SourceAddress: "source_addr",
Payload: nil,
Type: int64(1),
}
m, err := json.Marshal(validMemo)
assert.NilError(t, err)
return string(m)
},
errMsg: "",
},
}

// valid memo
validMemo := Message{
SourceChain: "source_chain",
SourceAddress: "source_addr",
Payload: nil,
Type: int64(1),
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := gmpHandler.OnRecvPacket(ctx, sdk.Coin{}, tc.memo(), nil)
if len(tc.errMsg) != 0 {
assert.ErrorContains(t, err, tc.errMsg)
} else {
assert.NilError(t, err)
}
})
}
m, err := json.Marshal(validMemo)
assert.NilError(t, err)
err = gmpHandler.OnRecvPacket(ctx, sdk.Coin{}, string(m), nil)
assert.NilError(t, err)
}
2 changes: 2 additions & 0 deletions x/uibc/uics20/ibc_module_mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/umee-network/umee/v6/x/leverage/types"
)

// MOCKIBCModule provides a mock implementation of the IBCModule interface for testing purposes.
type MOCKIBCModule struct {
porttypes.IBCModule
}
Expand All @@ -27,6 +28,7 @@ func (m MOCKIBCModule) OnRecvPacket(
return channeltypes.NewResultAcknowledgement([]byte("true"))
}

// MockLeverageMsgServer provides a mock implementation of the MsgServer interface for leverage module.
type MockLeverageMsgServer struct {
types.MsgServer
}
Expand Down
56 changes: 40 additions & 16 deletions x/uibc/uics20/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,47 @@ func TestIBCOnRecvPacket(t *testing.T) {

func TestDeserializeFTData(t *testing.T) {
cdc := tsdk.NewCodec(uibc.RegisterInterfaces, ltypes.RegisterInterfaces)
invalidPacketData := []byte("asdasd")
packet.Data = invalidPacketData
_, err := deserializeFTData(cdc, packet)
assert.ErrorContains(t, err, "invalid character")

ftData := ics20types.FungibleTokenPacketData{
Denom: atomCoin.Denom,
Amount: atomCoin.Amount.String(),
Sender: senderAddr,
Receiver: recvAddr,
Memo: "",
tests := []struct {
name string
packetData func() []byte
errMsg string
}{
{
name: "invalid packet data",
packetData: func() []byte {
return []byte("invalid packet data")
},
errMsg: "invalid character",
},
{
name: "valid packet data ",
packetData: func() []byte {
ftData := ics20types.FungibleTokenPacketData{
Denom: atomCoin.Denom,
Amount: atomCoin.Amount.String(),
Sender: senderAddr,
Receiver: recvAddr,
Memo: "",
}
mar, err := json.Marshal(ftData)
assert.NilError(t, err)
return mar
},
errMsg: "",
},
}

mar, err := json.Marshal(ftData)
assert.NilError(t, err)
packet.Data = mar
recvFtData, err := deserializeFTData(cdc, packet)
assert.NilError(t, err)
assert.DeepEqual(t, recvFtData, ftData)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
packet.Data = tc.packetData()
recvFtData, err := deserializeFTData(cdc, packet)
if tc.errMsg != "" {
assert.ErrorContains(t, err, tc.errMsg)
} else {
assert.NilError(t, err)
assert.DeepEqual(t, recvFtData, ftData)
}
})
}
}

0 comments on commit 016438e

Please sign in to comment.