Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for DecisionToString #1384

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions internal/common/util/stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func valueToString(v reflect.Value) string {

// HistoryEventToString convert HistoryEvent to string
func HistoryEventToString(e *s.HistoryEvent) string {
data := getData(e)
data := getHistoryEventData(e)

return e.GetEventType().String() + ": " + anyToString(data)
}

func getData(e *s.HistoryEvent) interface{} {
func getHistoryEventData(e *s.HistoryEvent) interface{} {
switch e.GetEventType() {
case s.EventTypeWorkflowExecutionStarted:
return e.WorkflowExecutionStartedEventAttributes
Expand Down Expand Up @@ -158,32 +158,35 @@ func getData(e *s.HistoryEvent) interface{} {

// DecisionToString convert Decision to string
func DecisionToString(d *s.Decision) string {
var data interface{}
data := decisionGetData(d)

return d.GetDecisionType().String() + ": " + anyToString(data)
}

func decisionGetData(d *s.Decision) interface{} {
switch d.GetDecisionType() {
case s.DecisionTypeScheduleActivityTask:
data = d.ScheduleActivityTaskDecisionAttributes
return d.ScheduleActivityTaskDecisionAttributes

case s.DecisionTypeRequestCancelActivityTask:
data = d.RequestCancelActivityTaskDecisionAttributes
return d.RequestCancelActivityTaskDecisionAttributes

case s.DecisionTypeStartTimer:
data = d.StartTimerDecisionAttributes
return d.StartTimerDecisionAttributes

case s.DecisionTypeCancelTimer:
data = d.CancelTimerDecisionAttributes
return d.CancelTimerDecisionAttributes

case s.DecisionTypeCompleteWorkflowExecution:
data = d.CompleteWorkflowExecutionDecisionAttributes
return d.CompleteWorkflowExecutionDecisionAttributes

case s.DecisionTypeFailWorkflowExecution:
data = d.FailWorkflowExecutionDecisionAttributes
return d.FailWorkflowExecutionDecisionAttributes

case s.DecisionTypeRecordMarker:
data = d.RecordMarkerDecisionAttributes
return d.RecordMarkerDecisionAttributes

default:
data = d
return d
}

return d.GetDecisionType().String() + ": " + anyToString(data)
}
96 changes: 93 additions & 3 deletions internal/common/util/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func TestHistoryEventToString(t *testing.T) {
assert.Equal(t, expected, strVal)
}

// This just tests that we pick the right attibutes to return
// This just tests that we pick the right attributes to return
// the other attributes will be nil
func Test_getData(t *testing.T) {
func Test_getHistoryEventData(t *testing.T) {
cases := []struct {
event *s.HistoryEvent
expected interface{}
Expand Down Expand Up @@ -316,7 +316,97 @@ func Test_getData(t *testing.T) {
name, err := tc.event.GetEventType().MarshalText()
require.NoError(t, err)
t.Run(string(name), func(t *testing.T) {
require.Equal(t, tc.expected, getData(tc.event))
require.Equal(t, tc.expected, getHistoryEventData(tc.event))
})
}
}

func TestDecisionToString(t *testing.T) {
decision := &s.Decision{
DecisionType: toPtr(s.DecisionTypeScheduleActivityTask),
ScheduleActivityTaskDecisionAttributes: &s.ScheduleActivityTaskDecisionAttributes{
ActivityId: toPtr("activity-id"),
ActivityType: &s.ActivityType{Name: toPtr("activity-type")},
},
}

strVal := DecisionToString(decision)
expected := "ScheduleActivityTask: (ActivityId:activity-id, ActivityType:(Name:activity-type), Input:[])"
require.Equal(t, expected, strVal)
}

// This just tests that we pick the right attributes to return
// the other attributes will be nil
func Test_decisionGetData(t *testing.T) {
cases := []struct {
decision *s.Decision
expected interface{}
}{
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeScheduleActivityTask),
ScheduleActivityTaskDecisionAttributes: &s.ScheduleActivityTaskDecisionAttributes{},
},
expected: &s.ScheduleActivityTaskDecisionAttributes{},
},
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeRequestCancelActivityTask),
RequestCancelActivityTaskDecisionAttributes: &s.RequestCancelActivityTaskDecisionAttributes{},
},
expected: &s.RequestCancelActivityTaskDecisionAttributes{},
},
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeStartTimer),
StartTimerDecisionAttributes: &s.StartTimerDecisionAttributes{},
},
expected: &s.StartTimerDecisionAttributes{},
},
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeCancelTimer),
CancelTimerDecisionAttributes: &s.CancelTimerDecisionAttributes{},
},
expected: &s.CancelTimerDecisionAttributes{},
},
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeCompleteWorkflowExecution),
CompleteWorkflowExecutionDecisionAttributes: &s.CompleteWorkflowExecutionDecisionAttributes{},
},
expected: &s.CompleteWorkflowExecutionDecisionAttributes{},
},
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeFailWorkflowExecution),
FailWorkflowExecutionDecisionAttributes: &s.FailWorkflowExecutionDecisionAttributes{},
},
expected: &s.FailWorkflowExecutionDecisionAttributes{},
},
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionTypeRecordMarker),
RecordMarkerDecisionAttributes: &s.RecordMarkerDecisionAttributes{},
},
expected: &s.RecordMarkerDecisionAttributes{},
},
// In the default case, we should return the decision itself
{
decision: &s.Decision{
DecisionType: toPtr(s.DecisionType(123456789)),
},
expected: &s.Decision{
DecisionType: toPtr(s.DecisionType(123456789)),
},
},
}

for _, tc := range cases {
name, err := tc.decision.GetDecisionType().MarshalText()
require.NoError(t, err)
t.Run(string(name), func(t *testing.T) {
require.Equal(t, tc.expected, decisionGetData(tc.decision))
})
}
}
Expand Down
Loading