|
15 | 15 | package model
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "encoding/json" |
19 | 18 | "testing"
|
20 | 19 |
|
21 | 20 | "github.com/stretchr/testify/assert"
|
22 | 21 |
|
23 | 22 | val "github.com/serverlessworkflow/sdk-go/v2/validator"
|
24 | 23 | )
|
25 | 24 |
|
26 |
| -func TestWorkflowRefUnmarshalJSON(t *testing.T) { |
27 |
| - type testCase struct { |
28 |
| - desp string |
29 |
| - data string |
30 |
| - expect WorkflowRef |
31 |
| - err string |
32 |
| - } |
33 |
| - testCases := []testCase{ |
34 |
| - { |
35 |
| - desp: "normal object test", |
36 |
| - data: `{"workflowId": "1", "version": "2", "invoke": "async", "onParentComplete": "continue"}`, |
37 |
| - expect: WorkflowRef{ |
38 |
| - WorkflowID: "1", |
39 |
| - Version: "2", |
40 |
| - Invoke: InvokeKindAsync, |
41 |
| - OnParentComplete: "continue", |
42 |
| - }, |
43 |
| - err: ``, |
44 |
| - }, |
45 |
| - { |
46 |
| - desp: "normal object test & defaults", |
47 |
| - data: `{"workflowId": "1"}`, |
48 |
| - expect: WorkflowRef{ |
49 |
| - WorkflowID: "1", |
50 |
| - Version: "", |
51 |
| - Invoke: InvokeKindSync, |
52 |
| - OnParentComplete: "terminate", |
53 |
| - }, |
54 |
| - err: ``, |
55 |
| - }, |
56 |
| - { |
57 |
| - desp: "normal string test", |
58 |
| - data: `"1"`, |
59 |
| - expect: WorkflowRef{ |
60 |
| - WorkflowID: "1", |
61 |
| - Version: "", |
62 |
| - Invoke: InvokeKindSync, |
63 |
| - OnParentComplete: "terminate", |
64 |
| - }, |
65 |
| - err: ``, |
66 |
| - }, |
67 |
| - { |
68 |
| - desp: "empty data", |
69 |
| - data: ` `, |
70 |
| - expect: WorkflowRef{}, |
71 |
| - err: `unexpected end of JSON input`, |
72 |
| - }, |
73 |
| - { |
74 |
| - desp: "invalid string format", |
75 |
| - data: `"1`, |
76 |
| - expect: WorkflowRef{}, |
77 |
| - err: `unexpected end of JSON input`, |
78 |
| - }, |
79 |
| - { |
80 |
| - desp: "invalid json format", |
81 |
| - data: `{"workflowId": 1, "version": "2", "invoke": "async", "onParentComplete": "continue"}`, |
82 |
| - expect: WorkflowRef{}, |
83 |
| - err: `json: cannot unmarshal number into Go struct field workflowRefForUnmarshal.workflowId of type string`, |
84 |
| - }, |
85 |
| - { |
86 |
| - desp: "invalid string or object", |
87 |
| - data: `1`, |
88 |
| - expect: WorkflowRef{}, |
89 |
| - err: `subFlowRef value '1' is not supported, it must be an object or string`, |
90 |
| - }, |
91 |
| - } |
92 |
| - for _, tc := range testCases { |
93 |
| - t.Run(tc.desp, func(t *testing.T) { |
94 |
| - var v WorkflowRef |
95 |
| - err := json.Unmarshal([]byte(tc.data), &v) |
96 |
| - |
97 |
| - if tc.err != "" { |
98 |
| - assert.Error(t, err) |
99 |
| - assert.Regexp(t, tc.err, err) |
100 |
| - return |
101 |
| - } |
102 |
| - |
103 |
| - assert.NoError(t, err) |
104 |
| - assert.Equal(t, tc.expect, v) |
105 |
| - }) |
106 |
| - } |
107 |
| -} |
108 |
| - |
109 |
| -func TestWorkflowRefValidate(t *testing.T) { |
110 |
| - type testCase struct { |
111 |
| - desp string |
112 |
| - workflowRef WorkflowRef |
113 |
| - err string |
114 |
| - } |
115 |
| - testCases := []testCase{ |
116 |
| - { |
117 |
| - desp: "all field & defaults", |
118 |
| - workflowRef: WorkflowRef{ |
119 |
| - WorkflowID: "1", |
120 |
| - Version: "2", |
121 |
| - Invoke: InvokeKindSync, |
122 |
| - OnParentComplete: "terminate", |
123 |
| - }, |
124 |
| - err: ``, |
125 |
| - }, |
126 |
| - { |
127 |
| - desp: "all field", |
128 |
| - workflowRef: WorkflowRef{ |
129 |
| - WorkflowID: "1", |
130 |
| - Version: "2", |
131 |
| - Invoke: InvokeKindAsync, |
132 |
| - OnParentComplete: "continue", |
133 |
| - }, |
134 |
| - err: ``, |
135 |
| - }, |
136 |
| - { |
137 |
| - desp: "missing workflowId", |
138 |
| - workflowRef: WorkflowRef{ |
139 |
| - WorkflowID: "", |
140 |
| - Version: "2", |
141 |
| - Invoke: InvokeKindSync, |
142 |
| - OnParentComplete: "terminate", |
143 |
| - }, |
144 |
| - err: `Key: 'WorkflowRef.WorkflowID' Error:Field validation for 'WorkflowID' failed on the 'required' tag`, |
145 |
| - }, |
146 |
| - { |
147 |
| - desp: "invalid invoke", |
148 |
| - workflowRef: WorkflowRef{ |
149 |
| - WorkflowID: "1", |
150 |
| - Version: "2", |
151 |
| - Invoke: "sync1", |
152 |
| - OnParentComplete: "terminate", |
153 |
| - }, |
154 |
| - err: `Key: 'WorkflowRef.Invoke' Error:Field validation for 'Invoke' failed on the 'oneof' tag`, |
155 |
| - }, |
156 |
| - { |
157 |
| - desp: "invalid onParentComplete", |
158 |
| - workflowRef: WorkflowRef{ |
159 |
| - WorkflowID: "1", |
160 |
| - Version: "2", |
161 |
| - Invoke: InvokeKindSync, |
162 |
| - OnParentComplete: "terminate1", |
163 |
| - }, |
164 |
| - err: `Key: 'WorkflowRef.OnParentComplete' Error:Field validation for 'OnParentComplete' failed on the 'oneof' tag`, |
165 |
| - }, |
166 |
| - } |
167 |
| - for _, tc := range testCases { |
168 |
| - t.Run(tc.desp, func(t *testing.T) { |
169 |
| - err := val.GetValidator().Struct(tc.workflowRef) |
170 |
| - |
171 |
| - if tc.err != "" { |
172 |
| - assert.Error(t, err) |
173 |
| - assert.Regexp(t, tc.err, err) |
174 |
| - return |
175 |
| - } |
176 |
| - |
177 |
| - assert.NoError(t, err) |
178 |
| - }) |
179 |
| - } |
180 |
| -} |
181 |
| - |
182 | 25 | func TestSleepValidate(t *testing.T) {
|
183 | 26 | type testCase struct {
|
184 | 27 | desp string
|
|
0 commit comments