Skip to content

Commit

Permalink
Add rapid tests for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Sep 12, 2023
1 parent d88deec commit 81a504e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
53 changes: 46 additions & 7 deletions tests/e2e/action_rapid_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
package main

import (
"encoding/json"
"fmt"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"
)

func TestActionMarshalling(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
action := GetActionGen().Draw(t, "Action")
err := MarshalAndUnmarshalAction(action)
if err != nil {
t.Fatalf("error marshalling and unmarshalling action: %v", err)
}
})
}

func MarshalAndUnmarshalAction(action interface{}) error {
step := Step{
Action: action,
}
jsonobj, err := json.Marshal(step)
if err != nil {
return fmt.Errorf("error marshalling action inside step: %v", err)
}

var got Step
err = json.Unmarshal(jsonobj, &got)
if err != nil {
return fmt.Errorf("error unmarshalling action inside step: %v", err)
}

diff := cmp.Diff(step, got)
if diff != "" {
return fmt.Errorf("got (-), want (+): %v", diff)
}

return nil
}

// This needs to be adjusted manually when new actions are added and should
// include generators for all actions that are mentioned in main.go/runStep.
func GetActionGen() *rapid.Generator[any] {
return rapid.OneOf(
GetSendTokensActionGen().AsAny(),
Expand All @@ -22,7 +60,7 @@ func GetActionGen() *rapid.Generator[any] {
GetAddChainToRelayerActionGen().AsAny(),
GetAddIbcConnectionActionGen().AsAny(),
GetAddIbcChannelActionGen().AsAny(),
GetStartHermesActionGen().AsAny(),
GetStartRelayerActionGen().AsAny(),
GetTransferChannelCompleteActionGen().AsAny(),
GetRelayPacketsActionGen().AsAny(),
GetRelayRewardPacketsToProviderActionGen().AsAny(),
Expand Down Expand Up @@ -114,9 +152,9 @@ func GetSubmitConsumerRemovalProposalActionGen() *rapid.Generator[submitConsumer
})
}

func GetSubmitParamChangeProposalActionGen() *rapid.Generator[submitParamChangeProposalAction] {
return rapid.Custom(func(t *rapid.T) submitParamChangeProposalAction {
return submitParamChangeProposalAction{
func GetSubmitParamChangeProposalActionGen() *rapid.Generator[submitParamChangeLegacyProposalAction] {
return rapid.Custom(func(t *rapid.T) submitParamChangeLegacyProposalAction {
return submitParamChangeLegacyProposalAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
From: GetValidatorIDGen().Draw(t, "From"),
Deposit: rapid.Uint().Draw(t, "Deposit"),
Expand Down Expand Up @@ -213,8 +251,8 @@ func GetAddIbcChannelActionGen() *rapid.Generator[addIbcChannelAction] {
})
}

func GetStartHermesActionGen() *rapid.Generator[startHermesAction] {
return rapid.Just(startHermesAction{})
func GetStartRelayerActionGen() *rapid.Generator[startRelayerAction] {
return rapid.Just(startRelayerAction{})
}

func GetTransferChannelCompleteActionGen() *rapid.Generator[transferChannelCompleteAction] {
Expand All @@ -235,7 +273,8 @@ func GetTransferChannelCompleteActionGen() *rapid.Generator[transferChannelCompl
func GetRelayPacketsActionGen() *rapid.Generator[relayPacketsAction] {
return rapid.Custom(func(t *rapid.T) relayPacketsAction {
return relayPacketsAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
ChainA: GetChainIDGen().Draw(t, "Chain"),
ChainB: GetChainIDGen().Draw(t, "Chain"),
Port: rapid.String().Draw(t, "Port"),
Channel: rapid.Uint().Draw(t, "Channel"),
}
Expand Down
4 changes: 1 addition & 3 deletions tests/e2e/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func (step *Step) UnmarshalJSON(data []byte) error {

// UnmarshalMapToActionType takes a JSON object and an action type and marshals into an object of the corresponding action.
func UnmarshalMapToActionType(rawAction json.RawMessage, actionTypeString string) (interface{}, error) {
// TODO: ERRORS PROBABLY COME FROM HERE

switch actionTypeString {
case "main.submitConsumerAdditionProposalAction":
var a submitConsumerAdditionProposalAction
Expand Down Expand Up @@ -112,7 +110,7 @@ func UnmarshalMapToActionType(rawAction json.RawMessage, actionTypeString string
return nil, err
}
return a, nil
case "main.AddChainToRelayerAction":
case "main.addChainToRelayerAction":
var a addChainToRelayerAction
err := json.Unmarshal(rawAction, &a)
if err != nil {
Expand Down

0 comments on commit 81a504e

Please sign in to comment.