-
Notifications
You must be signed in to change notification settings - Fork 2
/
conditional_test.go
311 lines (264 loc) · 8.88 KB
/
conditional_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
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
package pipeline_test
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/saantiaguilera/go-pipeline"
)
// The following example evaluates a dummy condition and depending on the
// result it branches to one step or another.
//
// 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 ExampleConditionalStep() {
type User any
type Data any
stmt := pipeline.NewStatement(
"check_something",
func(ctx context.Context, in User) bool {
// check and return were to branch
return true
},
)
tf := pipeline.NewUnitStep(
"true_case",
func(ctx context.Context, in User) (Data, error) {
// do something with input
return Data(true), nil
},
)
ff := pipeline.NewUnitStep(
"false_step",
func(ctx context.Context, u User) (Data, error) {
// do something with input
return Data(false), nil
},
)
ctx := context.Background()
in := User(nil)
pipe := pipeline.NewConditionalStep[User, Data](stmt, tf, ff)
out, err := pipe.Run(ctx, in)
fmt.Println(out, err)
// output:
// true <nil>
}
// Benchmark for traversing a conditional 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
// BenchmarkConditionalStep-8 9185359 150.2 ns/op 0 B/op 0 allocs/op
func BenchmarkConditionalStep(b *testing.B) {
var err error
s := pipeline.NewConditionalStep[any, any](
pipeline.NewAnonymousStatement(func(ctx context.Context, a any) bool {
return a != nil
}),
noopStep[any]{},
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 TestConditionalStep_GivenNilStatement_WhenRun_FalseIsRun(t *testing.T) {
run := false
falseStep := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
run = true
return nil, nil
})
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement[any](nil), trueStep, falseStep)
_, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.True(t, run)
}
func TestConditionalStep_GivenStatementTrue_WhenRun_TrueIsRun(t *testing.T) {
run := false
falseStep := pipeline.NewUnitStep[any, any]("", nil)
trueStep := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
run = true
return nil, nil
})
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), trueStep, falseStep)
_, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.True(t, run)
}
func TestConditionalStep_GivenStatementFalse_WhenRun_FalseIsRun(t *testing.T) {
run := false
falseStep := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
run = true
return nil, nil
})
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return false
}), trueStep, falseStep)
_, err := step.Run(context.Background(), 1)
assert.Nil(t, err)
assert.True(t, run)
}
func TestConditionalStep_GivenStatementTrueAndNilTrue_WhenRun_ThenErrors(t *testing.T) {
falseStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), nil, falseStep)
_, err := step.Run(context.Background(), 1)
assert.Error(t, err)
}
func TestConditionalStep_GivenStatementFalseNilFalse_WhenRun_ThenErrors(t *testing.T) {
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return false
}), trueStep, nil)
_, err := step.Run(context.Background(), 1)
assert.Error(t, err)
}
func TestConditionalStep_GivenStatementTrueWithTrueError_WhenRun_TrueErrorReturned(t *testing.T) {
trueErr := errors.New("error")
falseStep := pipeline.NewUnitStep[any, any]("", nil)
trueStep := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
return nil, trueErr
})
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), trueStep, falseStep)
_, err := step.Run(context.Background(), 1)
assert.Equal(t, trueErr, err)
}
func TestConditionalStep_GivenStatementFalseWithFalseError_WhenRun_FalseErrorReturned(t *testing.T) {
falseErr := errors.New("error")
trueStep := pipeline.NewUnitStep[any, any]("", nil)
falseStep := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
return nil, falseErr
})
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return false
}), trueStep, falseStep)
_, err := step.Run(context.Background(), 1)
assert.Equal(t, falseErr, err)
}
func TestConditionalStep_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
}),
)
falseStep := pipeline.NewUnitStep[any, any]("", nil)
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewConditionalStep[any, any](statement, trueStep, falseStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}
func TestConditionalStep_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
}),
)
falseStep := pipeline.NewUnitStep[any, any]("", nil)
trueStep := pipeline.NewUnitStep[any, any]("", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewStatement[any]("SomeFuncName", nil), trueStep, falseStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}
func TestConditionalStep_GivenAGraphToDraw_WhenDrawn_ThenConditionIsAppliedWithBothBranches(t *testing.T) {
mockGraph := new(mockGraph)
mockGraph.On("AddActivity", "truestep").Once()
mockGraph.On("AddActivity", "falsestep").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)
})
falseStep := pipeline.NewUnitStep[any, any]("falsestep", nil)
trueStep := pipeline.NewUnitStep[any, any]("truestep", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), trueStep, falseStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}
func TestConditionalStep_GivenAGraphToDraw_WhenDrawnAndTrueExecuted_ThenTrueBranchIsNilValidated(t *testing.T) {
mockGraph := new(mockGraph)
mockGraph.On("AddActivity", "falsestep").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)
})
falseStep := pipeline.NewUnitStep[any, any]("falsestep", nil)
step := pipeline.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), nil, falseStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}
func TestConditionalStep_GivenAGraphToDraw_WhenDrawnAndFalseExecuted_ThenFalseBranchIsNilValidated(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.NewConditionalStep[any, any](pipeline.NewAnonymousStatement(func(ctx context.Context, in any) bool {
return true
}), trueStep, nil)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
}