Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Sep 17, 2024
1 parent 8b4cc6d commit fc2dd50
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 83 deletions.
2 changes: 1 addition & 1 deletion wormchain/x/ibc-composability-mw/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types"
)

// TestGenesis ensures genesis state can be initialiazed and exported correctly
// TestGenesis ensures genesis state can be initialiazed and exported correctly.
func TestGenesis(t *testing.T) {
for _, tc := range []struct {
dataInFlight map[string][]byte
Expand Down
11 changes: 4 additions & 7 deletions wormchain/x/ibc-composability-mw/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,19 @@ func TestPackets(t *testing.T) {
shouldErr: false,
},
} {

packet, ack := keeper.OnRecvPacket(ctx, tc.packet)

t.Run(tc.testName, func(t *testing.T) {

if tc.shouldErr {
require.NotNil(t, ack)
return
} else {
require.NotNil(t, packet)
require.Nil(t, ack)
}

// Should return nil because the packet is not transposed (it is an ibc hooks packet)
res := keeper.GetAndClearTransposedData(ctx, tc.packet.DestinationChannel, tc.packet.DestinationPort, tc.packet.Sequence)
require.Nil(t, res)
// Should return nil because the packet is not transposed (it is an ibc hooks packet)
res := keeper.GetAndClearTransposedData(ctx, tc.packet.DestinationChannel, tc.packet.DestinationPort, tc.packet.Sequence)
require.Nil(t, res)
}
})

}
Expand Down
60 changes: 32 additions & 28 deletions wormchain/x/ibc-composability-mw/types/gateway_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ import (
"github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types"
)

// TestGatewayPayloads tests the VerifyAndParseGatewayPayload function.
func TestGatewayPayloads(t *testing.T) {

for _, tc := range []struct {
testName string
memo string // use memo if present, otherwise marshal tbPayload with json
tbPayload types.GatewayIbcTokenBridgePayload
shouldErr bool
}{
{
testName: "memo present, payload abscent - should error",
memo: "abc123",
shouldErr: true,
},
{
testName: "memo abscent, invalid payload - should error",
tbPayload: types.GatewayIbcTokenBridgePayload{},
shouldErr: true,
},
{
testName: "valid transfer no payload - should pass",
tbPayload: types.GatewayIbcTokenBridgePayload{
GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{
Transfer: types.GatewayTransfer{
Expand All @@ -37,6 +41,7 @@ func TestGatewayPayloads(t *testing.T) {
shouldErr: false,
},
{
testName: "valid transfer with payload - should pass",
tbPayload: types.GatewayIbcTokenBridgePayload{
GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{
TransferWithPayload: types.GatewayTransferWithPayload{
Expand All @@ -50,36 +55,35 @@ func TestGatewayPayloads(t *testing.T) {
shouldErr: false,
},
} {
t.Run(tc.testName, func(t *testing.T) {
memo := tc.memo

memo := tc.memo

if memo == "" {
bz, err := json.Marshal(tc.tbPayload)
require.NoError(t, err)
memo = string(bz)
}
if memo == "" {
bz, err := json.Marshal(tc.tbPayload)
require.NoError(t, err)
memo = string(bz)
}

payload, err := types.VerifyAndParseGatewayPayload(memo)
payload, err := types.VerifyAndParseGatewayPayload(memo)

if tc.shouldErr {
require.Error(t, err)
// continue to next case if err
continue
} else {
require.NoError(t, err)
}
if tc.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)

// validate payload was parsed correctly
if payload.NoPayload {
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Chain, payload.ChainId)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Recipient, payload.Recipient)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Fee, payload.Fee)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Nonce, payload.Nonce)
} else {
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Chain, payload.ChainId)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Contract, payload.Recipient)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Payload, payload.Payload)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Nonce, payload.Nonce)
}
// validate payload was parsed correctly
if payload.NoPayload {
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Chain, payload.ChainId)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Recipient, payload.Recipient)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Fee, payload.Fee)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Nonce, payload.Nonce)
} else {
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Chain, payload.ChainId)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Contract, payload.Recipient)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Payload, payload.Payload)
require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Nonce, payload.Nonce)
}
}
})
}
}
45 changes: 24 additions & 21 deletions wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types"
)

// TestFormatIbcHooksMemo tests the FormatIbcHooksMemo function.
func TestFormatIbcHooksMemo(t *testing.T) {

ibcTranslatorContract := "wormhole123abc"

for _, tc := range []struct {
testName string
payload types.ParsedPayload
shouldErr bool
}{
// Normal w/ no payload
{
testName: "Normal w/o payload - should pass",
payload: types.ParsedPayload{
NoPayload: true,
ChainId: 1,
Expand All @@ -27,8 +28,8 @@ func TestFormatIbcHooksMemo(t *testing.T) {
},
shouldErr: false,
},
// Provide payload when unnecessary
{
testName: "Provide payload when unnecessary - should pass",
payload: types.ParsedPayload{
NoPayload: true,
ChainId: 1,
Expand All @@ -39,8 +40,8 @@ func TestFormatIbcHooksMemo(t *testing.T) {
},
shouldErr: false,
},
// Normal w/ payload
{
testName: "Normal w/ payload - should pass",
payload: types.ParsedPayload{
NoPayload: false,
ChainId: 1,
Expand All @@ -51,8 +52,8 @@ func TestFormatIbcHooksMemo(t *testing.T) {
},
shouldErr: false,
},
// Nil payload should not err
{
testName: "Nil payload - should pass",
payload: types.ParsedPayload{
NoPayload: true,
ChainId: 1,
Expand All @@ -64,23 +65,25 @@ func TestFormatIbcHooksMemo(t *testing.T) {
shouldErr: false,
},
} {
res, err := types.FormatIbcHooksMemo(tc.payload, ibcTranslatorContract)
t.Run(tc.testName, func(t *testing.T) {
res, err := types.FormatIbcHooksMemo(tc.payload, ibcTranslatorContract)

if tc.shouldErr {
require.Error(t, err)
continue
} else {
require.NoError(t, err)
require.NotNil(t, res)
}
if tc.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.NotNil(t, res)

if tc.payload.NoPayload {
require.NotContains(t, res, "gateway_convert_and_transfer_with_payload")
require.Contains(t, res, "recipient")
} else {
require.Contains(t, res, "gateway_convert_and_transfer_with_payload")
require.NotContains(t, res, "recipient")
require.Contains(t, res, "payload")
}
// validate payload was formatted correctly
if tc.payload.NoPayload {
require.NotContains(t, res, "gateway_convert_and_transfer_with_payload")
require.Contains(t, res, "recipient")
} else {
require.Contains(t, res, "gateway_convert_and_transfer_with_payload")
require.NotContains(t, res, "recipient")
require.Contains(t, res, "payload")
}
}
})
}
}
54 changes: 28 additions & 26 deletions wormchain/x/ibc-composability-mw/types/pfm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (
"github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types"
)

// TestFormatPfmMemo tests the FormatPfmMemo function.
func TestFormatPfmMemo(t *testing.T) {
for _, tc := range []struct {
testName string
payload types.ParsedPayload
queryResp types.IbcTranslatorQueryRsp
timeout time.Duration
retries uint8
shouldErr bool
}{
// Normal w/ no payload
{
testName: "Normal w/o payload - should pass",
payload: types.ParsedPayload{
NoPayload: true,
ChainId: 1,
Expand All @@ -34,8 +36,8 @@ func TestFormatPfmMemo(t *testing.T) {
retries: 3,
shouldErr: false,
},
// Provide payload when unnecessary
{
testName: "Provide payload when unnecessary - should pass",
payload: types.ParsedPayload{
NoPayload: true,
ChainId: 1,
Expand All @@ -51,8 +53,8 @@ func TestFormatPfmMemo(t *testing.T) {
retries: 3,
shouldErr: false,
},
// Normal w/ payload
{
testName: "Normal w/ payload - should pass",
payload: types.ParsedPayload{
NoPayload: false,
ChainId: 1,
Expand All @@ -68,8 +70,8 @@ func TestFormatPfmMemo(t *testing.T) {
retries: 21,
shouldErr: false,
},
// Nil payload should not err
{
testName: "Nil payload - should pass",
payload: types.ParsedPayload{
NoPayload: true,
ChainId: 1,
Expand All @@ -86,31 +88,31 @@ func TestFormatPfmMemo(t *testing.T) {
shouldErr: false,
},
} {
t.Run(tc.testName, func(t *testing.T) {
// turn the query response into bytes
queryRespBz, err := json.Marshal(tc.queryResp)
require.NoError(t, err)

// turn the query response into bytes
queryRespBz, err := json.Marshal(tc.queryResp)
require.NoError(t, err)

res, err := types.FormatPfmMemo(tc.payload, queryRespBz, tc.timeout, tc.retries)
res, err := types.FormatPfmMemo(tc.payload, queryRespBz, tc.timeout, tc.retries)

if tc.shouldErr {
require.Error(t, err)
continue
} else {
require.NoError(t, err)
require.NotNil(t, res)
}
if tc.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.NotNil(t, res)

// convert response back to packet metadata
var packetMetadata types.PacketMetadata
err = json.Unmarshal([]byte(res), &packetMetadata)
require.NoError(t, err)
// convert response back to packet metadata
var packetMetadata types.PacketMetadata
err = json.Unmarshal([]byte(res), &packetMetadata)
require.NoError(t, err)

// validation checks
require.Equal(t, string(tc.payload.Recipient), packetMetadata.Forward.Receiver)
require.Equal(t, "transfer", packetMetadata.Forward.Port)
require.Equal(t, tc.queryResp.Channel, packetMetadata.Forward.Channel)
require.Equal(t, tc.timeout, packetMetadata.Forward.Timeout)
require.Equal(t, &tc.retries, packetMetadata.Forward.Retries)
// validation checks
require.Equal(t, string(tc.payload.Recipient), packetMetadata.Forward.Receiver)
require.Equal(t, "transfer", packetMetadata.Forward.Port)
require.Equal(t, tc.queryResp.Channel, packetMetadata.Forward.Channel)
require.Equal(t, tc.timeout, packetMetadata.Forward.Timeout)
require.Equal(t, &tc.retries, packetMetadata.Forward.Retries)
}
})
}
}

0 comments on commit fc2dd50

Please sign in to comment.