From 4ee530d173bfba68d74958b2a325057bfc4d4dcc Mon Sep 17 00:00:00 2001 From: Jakob Haahr Taankvist Date: Tue, 5 Nov 2024 14:29:22 +0100 Subject: [PATCH 1/3] Added tests for DecisionToString --- internal/common/util/stringer.go | 25 ++++---- internal/common/util/stringer_test.go | 88 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 11 deletions(-) diff --git a/internal/common/util/stringer.go b/internal/common/util/stringer.go index ddd5885ed..47d356986 100644 --- a/internal/common/util/stringer.go +++ b/internal/common/util/stringer.go @@ -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) } diff --git a/internal/common/util/stringer_test.go b/internal/common/util/stringer_test.go index 2f485d014..7ca5c4cbb 100644 --- a/internal/common/util/stringer_test.go +++ b/internal/common/util/stringer_test.go @@ -321,6 +321,94 @@ func Test_getData(t *testing.T) { } } +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) +} + +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)) + }) + } +} + func toPtr[v any](x v) *v { return &x } From 4af76fd07654a26a7a787c6828eee48c2f6c7511 Mon Sep 17 00:00:00 2001 From: Jakob Haahr Taankvist Date: Tue, 5 Nov 2024 14:49:16 +0100 Subject: [PATCH 2/3] rename getData to getHistoryEventData --- internal/common/util/stringer.go | 4 ++-- internal/common/util/stringer_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/common/util/stringer.go b/internal/common/util/stringer.go index 47d356986..717c8a108 100644 --- a/internal/common/util/stringer.go +++ b/internal/common/util/stringer.go @@ -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 diff --git a/internal/common/util/stringer_test.go b/internal/common/util/stringer_test.go index 7ca5c4cbb..b46b3973a 100644 --- a/internal/common/util/stringer_test.go +++ b/internal/common/util/stringer_test.go @@ -142,7 +142,7 @@ func TestHistoryEventToString(t *testing.T) { // This just tests that we pick the right attibutes 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{} @@ -316,7 +316,7 @@ 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)) }) } } From 91921c0b032c4b055af263dca7f20362bbfefb89 Mon Sep 17 00:00:00 2001 From: Jakob Haahr Taankvist Date: Tue, 5 Nov 2024 14:50:16 +0100 Subject: [PATCH 3/3] Comments --- internal/common/util/stringer_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/common/util/stringer_test.go b/internal/common/util/stringer_test.go index b46b3973a..19fc40a1e 100644 --- a/internal/common/util/stringer_test.go +++ b/internal/common/util/stringer_test.go @@ -140,7 +140,7 @@ 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_getHistoryEventData(t *testing.T) { cases := []struct { @@ -335,6 +335,8 @@ func TestDecisionToString(t *testing.T) { 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