Skip to content

Commit

Permalink
Fix marshal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Sep 13, 2023
1 parent 3a6d9f1 commit a81f92d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 30 deletions.
67 changes: 67 additions & 0 deletions tests/e2e/action_rapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func MarshalAndUnmarshalAction(action interface{}) error {
// include generators for all actions that are mentioned in main.go/runStep.
func GetActionGen() *rapid.Generator[any] {
return rapid.OneOf(
GetStartSovereignChainActionGen().AsAny(),
GetSubmitLegacyUpgradeProposalActionGen().AsAny(),
GetWaitUntilBlockActionGen().AsAny(),
GetChangeoverChainActionGen().AsAny(),
GetSendTokensActionGen().AsAny(),
GetStartChainActionGen().AsAny(),
GetSubmitTextProposalActionGen().AsAny(),
Expand All @@ -74,9 +78,72 @@ func GetActionGen() *rapid.Generator[any] {
GetDoublesignSlashActionGen().AsAny(),
GetAssignConsumerPubKeyActionGen().AsAny(),
GetSlashThrottleDequeueGen().AsAny(),
GetCreateIbcClientsActionGen().AsAny(),
CreateCancelUnbondTokensActionGen().AsAny(),
)
}

func CreateCancelUnbondTokensActionGen() *rapid.Generator[cancelUnbondTokensAction] {
return rapid.Custom(func(t *rapid.T) cancelUnbondTokensAction {
return cancelUnbondTokensAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
Amount: rapid.Uint().Draw(t, "Amount"),
Delegator: GetValidatorIDGen().Draw(t, "Delegator"),
Validator: GetValidatorIDGen().Draw(t, "Validator"),
}
})
}

func GetCreateIbcClientsActionGen() *rapid.Generator[createIbcClientsAction] {
return rapid.Custom(func(t *rapid.T) createIbcClientsAction {
return createIbcClientsAction{
ChainA: GetChainIDGen().Draw(t, "ChainA"),
ChainB: GetChainIDGen().Draw(t, "ChainB"),
}
})
}

func GetStartSovereignChainActionGen() *rapid.Generator[StartSovereignChainAction] {
return rapid.Custom(func(t *rapid.T) StartSovereignChainAction {
return StartSovereignChainAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
Validators: GetStartChainValidatorsGen().Draw(t, "Validators"),
GenesisChanges: rapid.String().Draw(t, "GenesisChanges"),
}
})
}

func GetSubmitLegacyUpgradeProposalActionGen() *rapid.Generator[LegacyUpgradeProposalAction] {
return rapid.Custom(func(t *rapid.T) LegacyUpgradeProposalAction {
return LegacyUpgradeProposalAction{
ChainID: GetChainIDGen().Draw(t, "ChainID"),
UpgradeTitle: rapid.String().Draw(t, "UpgradeTitle"),
Proposer: GetValidatorIDGen().Draw(t, "Proposer"),
UpgradeHeight: rapid.Uint64().Draw(t, "UpgradeHeight"),
}
})
}

func GetWaitUntilBlockActionGen() *rapid.Generator[waitUntilBlockAction] {
return rapid.Custom(func(t *rapid.T) waitUntilBlockAction {
return waitUntilBlockAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
Block: rapid.Uint().Draw(t, "Block"),
}
})
}

func GetChangeoverChainActionGen() *rapid.Generator[ChangeoverChainAction] {
return rapid.Custom(func(t *rapid.T) ChangeoverChainAction {
return ChangeoverChainAction{
SovereignChain: GetChainIDGen().Draw(t, "SovereignChain"),
ProviderChain: GetChainIDGen().Draw(t, "ProviderChain"),
Validators: GetStartChainValidatorsGen().Draw(t, "Validators"),
GenesisChanges: rapid.String().Draw(t, "GenesisChanges"),
}
})
}

func GetSendTokensActionGen() *rapid.Generator[SendTokensAction] {
return rapid.Custom(func(t *rapid.T) SendTokensAction {
return SendTokensAction{
Expand Down
63 changes: 35 additions & 28 deletions tests/e2e/json_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func TestProposalUnmarshal(t *testing.T) {
}

type ChainStateTestCase struct {
name string
jsonBytes []byte
chainState ChainState
expectedErrorText string
name string
jsonBytes []byte
chainState ChainState
expectedUnmarshalErrorText string
}

var testCases = []ChainStateTestCase{
Expand All @@ -71,8 +71,8 @@ var testCases = []ChainStateTestCase{
},
"Proposals": {
"1": {
"ProposalType": "main.ConsumerAdditionProposal",
"Proposal": {
"Type": "main.ConsumerAdditionProposal",
"RawProposal": {
"Deposit": 10000001,
"Chain": "consu",
"SpawnTime": 0,
Expand Down Expand Up @@ -100,12 +100,12 @@ var testCases = []ChainStateTestCase{
},
},
},
expectedErrorText: "",
expectedUnmarshalErrorText: "",
},
{
name: "invalid JSON",
jsonBytes: []byte(`thisisnotagoodjsonstring`),
expectedErrorText: "invalid json",
name: "invalid JSON",
jsonBytes: []byte(`thisisnotagoodjsonstring`),
expectedUnmarshalErrorText: "invalid json",
},
{
name: "unknown proposal type",
Expand All @@ -117,8 +117,8 @@ var testCases = []ChainStateTestCase{
},
"Proposals": {
"1": {
"ProposalType": "main.NotAProposalTypeProposal",
"Proposal": {
"Type": "main.NotAProposalTypeProposal",
"RawProposal": {
"Deposit": 10000001,
"Chain": "consu",
"SpawnTime": 0,
Expand All @@ -130,7 +130,7 @@ var testCases = []ChainStateTestCase{
}
},
}`),
expectedErrorText: "not a known proposal type",
expectedUnmarshalErrorText: "not a known proposal type",
},
}

Expand All @@ -139,43 +139,50 @@ func TestUnmarshalJSON(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var result ChainState
err := result.UnmarshalJSON(tc.jsonBytes)
if err != nil && tc.expectedErrorText == "" {
t.Errorf("Unexpected error: %v", err)
if err != nil && tc.expectedUnmarshalErrorText == "" {
t.Errorf("Test case %v: Unexpected error: %v", tc.name, err)
}

if err == nil && tc.expectedErrorText != "" {
t.Errorf("Expected error to contain: %v, but got no error", tc.expectedErrorText)
if err == nil && tc.expectedUnmarshalErrorText != "" {
t.Errorf("Test case %v: Expected error to contain: %v, but got no error", tc.name, tc.expectedUnmarshalErrorText)
}

if err != nil && tc.expectedErrorText != "" && strings.Contains(err.Error(), tc.expectedErrorText) {
t.Errorf("Expected error to contain: %v, but got: %v", tc.expectedErrorText, err)
if err != nil && tc.expectedUnmarshalErrorText != "" && strings.Contains(err.Error(), tc.expectedUnmarshalErrorText) {
t.Errorf("Test case %v: Expected error to contain: %v, but got: %v", tc.name, tc.expectedUnmarshalErrorText, err)
}

if !reflect.DeepEqual(result, tc.chainState) {
t.Errorf("Expected ChainState: %v, but got: %v", tc.chainState, result)
t.Errorf("Test case %v: Expected ChainState: %v, but got: %v", tc.name, tc.chainState, result)
}
})
}
}

func TestMarshalJSON(t *testing.T) {
// checks that marshalling and unmarshalling is the identity
// would optimally check that the marshalled JSON is the same as the expected JSON,
// but the marshalled JSON will specifically list null fields
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := tc.chainState.MarshalJSON()
if err != nil && tc.expectedErrorText == "" {
t.Errorf("Unexpected error: %v", err)
if err != nil {
t.Errorf("Test case %v: Unexpected error while marshalling: %v", tc.name, err)
}

if err == nil && tc.expectedErrorText != "" {
t.Errorf("Expected error to contain: %v, but got no error", tc.expectedErrorText)
if tc.expectedUnmarshalErrorText != "" {
// unmarshalling to compare does not make sense, since we expect it to
// fail, so just test that marshalling works and continue
return
}

if err != nil && tc.expectedErrorText != "" && strings.Contains(err.Error(), tc.expectedErrorText) {
t.Errorf("Expected error to contain: %v, but got: %v", tc.expectedErrorText, err)
unmarshalledResult := ChainState{}
err = unmarshalledResult.UnmarshalJSON(result)
if err != nil {
t.Errorf("Test case %v: Unexpected error while unmarshalling: %v", tc.name, err)
}

if !reflect.DeepEqual(result, tc.jsonBytes) {
t.Errorf("Expected JSON: %v, but got: %v", string(tc.jsonBytes), string(result))
if !reflect.DeepEqual(unmarshalledResult, tc.chainState) {
t.Errorf("Test case %v: Expected: %v, but got: %v", tc.name, string(tc.jsonBytes), string(result))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/tracehandler_testdata/happyPath.json

Large diffs are not rendered by default.

Loading

0 comments on commit a81f92d

Please sign in to comment.