forked from coinbase/step
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmachine_test.go
More file actions
421 lines (367 loc) · 12.5 KB
/
machine_test.go
File metadata and controls
421 lines (367 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package machine
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"strings"
"testing"
"github.com/coinbase/step/utils/to"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
)
func loadFixture(file string, t *testing.T) *StateMachine {
example_machine, err := ParseFile(file)
assert.NoError(t, err)
return example_machine
}
func execute(json []byte, input interface{}, t *testing.T) (map[string]interface{}, error) {
example_machine, err := FromJSON(json)
assert.NoError(t, err)
example_machine.SetDefaultHandler()
exec, err := example_machine.Execute(input)
return exec.Output, err
}
func executeFixture(file string, input map[string]interface{}, t *testing.T) map[string]interface{} {
example_machine := loadFixture(file, t)
exec, err := example_machine.Execute(input)
assert.NoError(t, err)
return exec.Output
}
//////
// TESTS
//////
func Test_Machine_EmptyStateMachinePassExample(t *testing.T) {
_, err := execute([]byte(EmptyStateMachine), make(map[string]interface{}), t)
assert.NoError(t, err)
}
func Test_Machine_SimplePassExample_With_Execute(t *testing.T) {
json := []byte(`
{
"StartAt": "start",
"States": {
"start": {
"Type": "Pass",
"Result": "b",
"ResultPath": "$.a",
"End": true
}
}
}`)
output, err := execute(json, make(map[string]interface{}), t)
assert.NoError(t, err)
assert.Equal(t, output["a"], "b")
output, err = execute(json, "{}", t)
assert.NoError(t, err)
assert.Equal(t, output["a"], "b")
output, err = execute(json, to.Strp("{}"), t)
assert.NoError(t, err)
assert.Equal(t, output["a"], "b")
}
func Test_Machine_ErrorUnknownState(t *testing.T) {
example_machine := loadFixture("../examples/bad_unknown_state.json", t)
_, err := example_machine.Execute(make(map[string]interface{}))
assert.Error(t, err)
assert.Regexp(t, "Unknown State", err.Error())
}
func Test_Machine_MarshallAllTypes(t *testing.T) {
file := "../examples/all_types.json"
sm, err := ParseFile(file)
assert.NoError(t, err)
sm.SetDefaultHandler()
assert.NoError(t, sm.Validate())
marshalledJson, err := json.Marshal(sm)
assert.NoError(t, err)
rawJson, err := ioutil.ReadFile(file)
assert.NoError(t, err)
assert.JSONEq(t, string(rawJson), string(marshalledJson))
}
func Test_Machine_Execute_Simple_Chain(t *testing.T) {
stateMachine, err := FromJSON([]byte(`{
"Comment": "Calculate Vector Multiplication on Const",
"StartAt": "AssignCoords",
"States": {
"AssignCoords": {
"Type": "TaskFn",
"Next": "Multiply"
},
"Multiply": {
"Type": "TaskFn",
"End": true
}
}
}`))
assert.Nil(t, err)
type Vector struct {
Coords []float64
}
stateMachine.SetTaskHandler("AssignCoords", func (context context.Context, input interface{})(Vector, error){
vector := Vector{}
spaceSize := input.(map[string]interface{})["Input"].(map[string]interface{})["data"].(map[string]interface{})["spaceSize"].(float64)
vector.Coords = make([]float64, int(spaceSize))
for i:=0; i < int(spaceSize); i++ {
vector.Coords[i] = float64(i + 1)
}
return vector, nil
})
stateMachine.SetTaskHandler("Multiply", func (context context.Context, input interface{})(Vector, error){
vector := Vector{}
const multiplyFactor = 4
mapstructure.Decode(input.(map[string]interface{})["Input"], &vector)
for i := 0; i < len(vector.Coords); i++ {
vector.Coords[i] *= multiplyFactor
}
return vector, nil
})
testLambda := "test_execute_vector_const_multiply"
stateMachine.SetResource(&testLambda)
input := "{\"data\": {\"spaceSize\": 5 } }"
executionRes, executionErr := stateMachine.Execute(input)
assert.NotNil(t, executionRes)
assert.Equal(t,"{\n \"Coords\": [\n 4,\n 8,\n 12,\n 16,\n 20\n ]\n}", executionRes.OutputJSON)
assert.Nil(t, executionErr)
}
func Test_Machine_Execute_With_Parallel_State(t *testing.T) {
stateMachine, err := FromJSON([]byte(`{
"Comment": "Triangle Calculation",
"StartAt": "StartCalculation",
"States": {
"StartCalculation": {
"Type": "Pass",
"Next": "CalculateTriangleAngles"
},
"CalculateTriangleAngles": {
"Type": "Parallel",
"Next": "Summarize",
"Branches": [
{
"StartAt": "CalculateAlpha",
"States": {
"CalculateAlpha": {
"Type": "TaskFn",
"End": true
}
}
},
{
"StartAt": "CalculateBeta",
"States": {
"CalculateBeta": {
"Type": "TaskFn",
"End": true
}
}
},
{
"StartAt": "CalculateGamma",
"States": {
"CalculateGamma": {
"Type": "TaskFn",
"End": true
}
}
}
]
},
"Summarize": {
"Type": "TaskFn",
"End": true
}
}
}`)) //
type Angle struct {
Name string
Value float64
}
type TriangleSides struct {
A float64
B float64
C float64
}
type TriangleAngles struct {
Alpha Angle
Beta Angle
Gamma Angle
}
assert.Nil(t, err)
calculateAngle := func(context context.Context, input interface{})(Angle, error){
// TODO: UMV: Effective Input from Parameters does not work
// TODO: UMV: Therefore I have to understand what type of task here by TaskName (CalculateAlpha, ..Beta, ..Gamma)
taskName := strings.ToLower(input.(map[string]interface{})["Task"].(string))
sides := TriangleSides{}
triangleSidesRaw := input.(map[string]interface{})["Input"].(map[string]interface{})["triangle"]
mapstructure.Decode(triangleSidesRaw, &sides)
angle := Angle{}
aSquare := sides.A * sides.A
bSquare := sides.B * sides.B
cSquare := sides.C * sides.C
if strings.Contains(taskName, "alpha"){
angle.Name = "alpha"
angle.Value = math.Acos((bSquare + cSquare - aSquare)/(2 * sides.B * sides.C)) * (180 / math.Pi)
} else if strings.Contains(taskName, "beta") {
angle.Name = "beta"
angle.Value = math.Acos((aSquare + cSquare - bSquare)/(2 * sides.A * sides.C)) * (180 / math.Pi)
} else if strings.Contains(taskName, "gamma") {
angle.Name = "gamma"
angle.Value = math.Acos((aSquare + bSquare - cSquare)/(2 * sides.A * sides.B)) * (180 / math.Pi)
} else {
return angle, fmt.Errorf("unable to understand what angle to calculate")
}
return angle, nil
}
testAlphaAngleCalcLambda := "test_execute_lambda_alpha_calc"
testBetaAngleCalcLambda := "test_execute_lambda_beta_calc"
testGammaAngleCalcLambda := "test_execute_lambda_gamma_calc"
branches := stateMachine.States["CalculateTriangleAngles"].(*ParallelState)
branches.Branches[0].SetTaskHandler("CalculateAlpha", calculateAngle)
branches.Branches[0].SetResource(&testAlphaAngleCalcLambda)
branches.Branches[1].SetTaskHandler("CalculateBeta", calculateAngle)
branches.Branches[1].SetResource(&testBetaAngleCalcLambda)
branches.Branches[2].SetTaskHandler("CalculateGamma", calculateAngle)
branches.Branches[2].SetResource(&testGammaAngleCalcLambda)
stateMachine.SetTaskHandler("Summarize", func(context context.Context, input interface{})(TriangleAngles, error){
triangle := TriangleAngles{}
// todo: umv: check that orders the same as expected
mapstructure.Decode(input.(map[string]interface{})["Input"].([]interface{})[0], &triangle.Alpha)
mapstructure.Decode(input.(map[string]interface{})["Input"].([]interface{})[1], &triangle.Beta)
mapstructure.Decode(input.(map[string]interface{})["Input"].([]interface{})[2], &triangle.Gamma)
return triangle, nil
})
testLambda := "test_execute_triangle_angles_calculation"
stateMachine.SetResource(&testLambda)
input := "{\"triangle\": {\"a\": 3, \"b\": 4, \"c\": 5} }"
executionRes, executionErr := stateMachine.Execute(input)
assert.Nil(t, executionErr)
assert.NotNil(t, executionRes)
assert.Equal(t, "{\n \"Alpha\": {\n \"Name\": \"alpha\",\n \"Value\": 36.86989764584401\n },\n " +
"\"Beta\": {\n \"Name\": \"beta\",\n \"Value\": 53.13010235415599\n },\n " +
"\"Gamma\": {\n \"Name\": \"gamma\",\n \"Value\": 90\n }\n}",
executionRes.OutputJSON)
}
func Test_Machine_Execute_With_Map_State(t *testing.T){
stateMachine, err := FromJSON([]byte(`{
"Comment": "Calculate sum with subsequent category selection",
"StartAt": "ProcessGrades",
"States": {
"ProcessGrades": {
"Type": "Map",
"Next": "Avg",
"ItemsPath": "$.marks",
"Iterator": {
"StartAt": "Scale",
"States": {
"Scale": {
"Type": "TaskFn",
"End": true
}
}
},
"ResultPath": "$.allMarks"
},
"Avg": {
"Type": "TaskFn",
"Next": "SelectGrade"
},
"SelectGrade": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.Avg",
"NumericLessThan" : 60,
"Next": "CGrade"
},
{
"And": [
{
"Variable": "$.Avg",
"NumericGreaterThanEquals" : 60
},
{
"Variable": "$.Avg",
"NumericLessThan" : 80
}
],
"Next": "BGrade"
},
{
"Variable": "$.Avg",
"NumericGreaterThanEquals" : 80,
"Next": "AGrade"
}
]
},
"AGrade": {
"Type": "TaskFn",
"End": true
},
"BGrade": {
"Type": "TaskFn",
"End": true
},
"CGrade": {
"Type": "TaskFn",
"End": true
}
}
}`))
type StudentMark struct {
Subject string
Mark float64
}
type AvgMark struct {
Avg float64
}
type StudentGrade struct {
Grade string
}
gradeStateMachineResource := "text_execute_machine_student_grade_define"
stateMachine.SetResource(&gradeStateMachineResource)
scaleStateMachine := stateMachine.States["ProcessGrades"].(*MapState).Iterator
scaleResource := "test_state_machine_scale_grades"
scaleStateMachine.SetResource(&scaleResource)
scaleStateMachine.SetTaskHandler("Scale", func(context context.Context, input interface{})(StudentMark, error){
mark :=StudentMark{}
mapstructure.Decode(input.(map[string]interface{})["Input"], &mark)
mark.Mark *= 100 / 5
return mark, nil
})
stateMachine.SetTaskHandler("Avg", func(context context.Context, input interface{})(AvgMark, error){
var marks []StudentMark
rawMarks := input.(map[string]interface{})["Input"].(map[string]interface{})["allMarks"].([]interface{})
mapstructure.Decode(rawMarks, &marks)
avg := AvgMark{}
var total float64
total = 0
for _, m := range marks{
total += m.Mark
}
avg.Avg = total / float64(len(marks))
return avg, nil
})
stateMachine.SetTaskHandler("AGrade", func(context context.Context, input interface{})(StudentGrade, error){
grade := StudentGrade{}
grade.Grade = "Very good student"
return grade, nil
})
stateMachine.SetTaskHandler("BGrade", func(context context.Context, input interface{})(StudentGrade, error){
grade := StudentGrade{}
grade.Grade = "Good student"
return grade, nil
})
stateMachine.SetTaskHandler("CGrade", func(context context.Context, input interface{})(StudentGrade, error){
grade := StudentGrade{}
grade.Grade = "So-so student"
return grade, nil
})
assert.Nil(t, err)
err = stateMachine.Validate()
assert.Nil(t, err)
input := "{\"marks\": [ {\"subject\": \"math\", \"mark\": 4}, " +
"{\"subject\": \"physics\", \"mark\": 5}, " +
"{\"subject\": \"chemistry\", \"mark\": 5} ] }"
executionRes, executionErr := stateMachine.Execute(input)
assert.Nil(t, executionErr)
assert.NotNil(t, executionRes)
assert.Equal(t, "{\n \"Grade\": \"Very good student\"\n}", executionRes.OutputJSON)
}