-
Notifications
You must be signed in to change notification settings - Fork 2
/
optional_test.go
266 lines (229 loc) · 7.36 KB
/
optional_test.go
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
package pipeline_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/saantiaguilera/go-pipeline"
)
// The following example shows an optional step were it will be run if the evaluated
// statement yields true, otherwise it will return the same input it was provided
// this type of constructed step doesn't allow output mutation as we don't know
// how to default the mutated output if the step is skipped
//
// This example uses dummy data to showcase as simple as possible this scenario.
//
// Note: we use several UnitStep to showcase as it allows us to
// easily run dummy code, but it could use any type of step you want
// as long as it implements pipeline.Step[I, O]
func ExampleOptionalStep() {
type User any
stmt := pipeline.NewStatement(
"check_something",
func(ctx context.Context, in User) bool {
// check and return if we should run optional or not
return in == User(1)
},
)
of := pipeline.NewUnitStep(
"optional_case",
func(ctx context.Context, in User) (User, error) {
// do something with input
return User(true), nil
},
)
ctx := context.Background()
pipe := pipeline.NewOptionalStep[User](stmt, of)
// Skips optional step (returns same input)
out, err := pipe.Run(ctx, User(12))
fmt.Println(out, err)
// Runs optional step (returns step output)
out, err = pipe.Run(ctx, User(1))
fmt.Println(out, err)
// output:
// 12 <nil>
// true <nil>
}
// The following example allows us to define an optional step with different
// output from its input, and in case the optional step is skipped it will
// call a provided default function for it to return the default output you want for that case
//
// This example uses dummy data to showcase as simple as possible this scenario.
//
// Note: we use several UnitStep to showcase as it allows us to
// easily run dummy code, but it could use any type of step you want
// as long as it implements pipeline.Step[I, O]
func ExampleOptionalStep_default() {
type User any
type Data any
stmt := pipeline.NewStatement(
"check_something",
func(ctx context.Context, in User) bool {
// check and return if we should run optional or not
return in == User(1)
},
)
of := pipeline.NewUnitStep(
"optional_case",
func(ctx context.Context, in User) (Data, error) {
// do something with input
return Data(true), nil
},
)
def := func(ctx context.Context, in User) (Data, error) {
// create default value of type Data (gets run if we
// skip the step as we don't know how to default Data type)
return Data(false), nil
}
ctx := context.Background()
pipe := pipeline.NewOptionalStepWithDefault[User, Data](stmt, of, def)
// Skips optional step (returns default)
out, err := pipe.Run(ctx, User(12))
fmt.Println(out, err)
// Runs optional step (returns step output)
out, err = pipe.Run(ctx, User(1))
fmt.Println(out, err)
// output:
// false <nil>
// true <nil>
}
// Benchmark for traversing a optional step. This is simply used so that future changes can
// easily reflect how they affected the performance
//
// goos: darwin
// goarch: amd64
// pkg: github.com/saantiaguilera/go-pipeline
// cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
// BenchmarkOptionalStep-8 7752477 175.9 ns/op 0 B/op 0 allocs/op
func BenchmarkOptionalStep(b *testing.B) {
var err error
s := pipeline.NewOptionalStep[any](
pipeline.NewAnonymousStatement(func(ctx context.Context, a any) bool {
return a != nil
}),
noopStep[any]{},
)
ctx := context.Background()
in := 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
_, err = s.Run(ctx, in)
b.StopTimer()
if err != nil {
b.Fail()
}
}
}
func TestOptionalStep_GivenNilStatement_WhenRun_ThenDefaults(t *testing.T) {
run := false
step := pipeline.NewOptionalStep[any](pipeline.NewAnonymousStatement[any](nil), pipeline.NewUnitStep("", func(_ context.Context, _ any) (any, error) {
return nil, nil
}))
res, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.Equal(t, 1, res)
assert.False(t, run)
}
func TestOptionalStep_GivenStatementTrue_WhenRun_ThenEvaluatesStep(t *testing.T) {
run := false
ev := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
run = true
return 25, nil
})
step := pipeline.NewOptionalStep[any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), ev)
v, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.Equal(t, 25, v)
assert.True(t, run)
}
func TestOptionalStep_GivenNilStatementWithDefault_WhenRun_ThenDefaults(t *testing.T) {
run := false
step := pipeline.NewOptionalStepWithDefault[any, any](pipeline.NewAnonymousStatement[any](nil), pipeline.NewUnitStep("", func(_ context.Context, _ any) (any, error) {
return nil, nil
}), func(_ context.Context, _ any) (any, error) {
return 25, nil
})
res, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.Equal(t, 25, res)
assert.False(t, run)
}
func TestOptionalStep_GivenStatementTrueWithDefault_WhenRun_ThenEvaluatesStep(t *testing.T) {
run := false
ev := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
run = true
return 25, nil
})
step := pipeline.NewOptionalStepWithDefault[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), ev, func(_ context.Context, _ any) (any, error) {
return 50, nil
})
v, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.Equal(t, 25, v)
assert.True(t, run)
}
func TestOptionalStep_GivenAGraphToDrawWithAnonymouseStatement_WhenDrawn_ThenConditionGetsEmptyName(t *testing.T) {
statement := pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
})
mockGraph := new(mockGraph)
mockGraph.On(
"AddDecision",
"",
mock.MatchedBy(func(obj any) bool {
return true
}), mock.MatchedBy(func(obj any) bool {
return true
}),
)
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewOptionalStepWithDefault[any, any](statement, trueStep, func(_ context.Context, _ any) (any, error) {
return nil, nil
})
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}
func TestOptionalStep_GivenAGraphToDraw_WhenDrawn_ThenConditionGetsNameOfStatement(t *testing.T) {
mockGraph := new(mockGraph)
mockGraph.On(
"AddDecision",
"SomeFuncName",
mock.MatchedBy(func(obj any) bool {
return true
}), mock.MatchedBy(func(obj any) bool {
return true
}),
)
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewOptionalStep[any](pipeline.NewStatement[any]("SomeFuncName", nil), trueStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}
func TestOptionalStep_GivenAGraphToDraw_WhenDrawn_ThenConditionIsAppliedWithBothBranches(t *testing.T) {
mockGraph := new(mockGraph)
mockGraph.On("AddActivity", "truestep").Once()
mockGraph.On(
"AddDecision",
mock.Anything,
mock.MatchedBy(func(obj any) bool {
return true
}), mock.MatchedBy(func(obj any) bool {
return true
}),
).Run(func(args mock.Arguments) {
args.Get(1).(pipeline.GraphDrawer)(mockGraph)
args.Get(2).(pipeline.GraphDrawer)(mockGraph)
})
trueStep := pipeline.NewUnitStep[any, any]("truestep", nil)
step := pipeline.NewOptionalStep[any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), trueStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}