-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan_test.go
More file actions
137 lines (114 loc) · 3.35 KB
/
plan_test.go
File metadata and controls
137 lines (114 loc) · 3.35 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
package databuilder
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)
func TestPlanRun(t *testing.T) {
const VALUE = "9F0D8E07-6C46-48B7-983C-5C309C042CC6"
d := testNew(t)
err := d.AddBuilders(DBTestFunc, DBTestFunc4)
assert.NoError(t, err)
executionPlan, err := d.Compile(TestStruct1{})
assert.NotNil(t, executionPlan)
assert.NoError(t, err)
ctx := context.Background()
_, err = executionPlan.Run(ctx, 1)
assert.Error(t, err, "initial data needs to be struct")
_, err = executionPlan.Run(ctx, TestStruct1{}, TestStruct1{})
assert.Error(t, err, "multiple instance of same data should not be provided")
_, err = executionPlan.Run(ctx)
assert.Error(t, err, "missing starting data should error out")
result, err := executionPlan.Run(ctx,
nil,
TestStruct1{
Value: VALUE,
},
) // nil values should be ignored
assert.NoError(t, err)
var t3 TestStruct3
data := result.Get(t3)
assert.NotNil(t, data)
ts3, ok := data.(TestStruct3)
assert.True(t, ok)
assert.Equal(t, VALUE, ts3.Value)
var t2 TestStruct2
data = result.Get(t2)
assert.NotNil(t, data)
ts2, ok := data.(TestStruct2)
assert.True(t, ok)
assert.Equal(t, strings.ReplaceAll(VALUE, "-", "_"), ts2.Value)
goleak.VerifyNone(t)
}
func TestPlanRunPartialSuccess(t *testing.T) {
const VALUE = "9F0D8E07-6C46-48B7-983C-5C309C042CC6"
d := testNew(t)
err := d.AddBuilders(DBTestFunc6, DBTestFunc7, DBTestFuncErr, DBTestFuncAfterErr)
assert.NoError(t, err)
executionPlan, err := d.Compile(TestStruct1{})
assert.NotNil(t, executionPlan)
assert.NoError(t, err)
ctx := context.Background()
result, err := executionPlan.Run(ctx,
TestStruct1{
Value: VALUE,
},
)
assert.Error(t, err, "DBTestFunc encounterd an error")
var t2 TestStruct2
data := result.Get(t2)
assert.Nil(t, data, "should not return data with error")
var t3 TestStruct3
data = result.Get(t3)
assert.NotNil(t, data, "should return data with no error")
_, ok := data.(TestStruct3)
assert.True(t, ok)
var t4 TestStruct4
data = result.Get(t4)
assert.NotNil(t, data, "should return data downstream of success")
_, ok = data.(TestStruct4)
assert.True(t, ok)
var t5 TestStruct5
data = result.Get(t5)
assert.Nil(t, data, "should not return data downstream of error")
goleak.VerifyNone(t)
}
func TestPlanRun_ErrorFlowWithCommaOk(t *testing.T) {
// Verify that the comma-ok type assertion guards in processWork and
// doWorkAndGetResult correctly propagate errors from builders.
d := testNew(t)
err := d.AddBuilders(DBTestFuncErr)
assert.NoError(t, err)
executionPlan, err := d.Compile(TestStruct1{})
assert.NotNil(t, executionPlan)
assert.NoError(t, err)
_, err = executionPlan.Run(context.Background(), TestStruct1{Value: "test"})
assert.ErrorContains(t, err, "encountered an error")
goleak.VerifyNone(t)
}
func ExamplePlan() {
b := New()
err := b.AddBuilders(DBTestFunc, DBTestFunc4)
fmt.Println(err == nil)
ep, err := b.Compile(TestStruct1{})
fmt.Println(err == nil)
_, err = ep.Run(context.Background(), TestStruct1{})
fmt.Println(err == nil)
err = ep.Replace(context.Background(), DBTestFunc, DBTestFunc5)
fmt.Println(err == nil)
_, err = ep.Run(context.Background(), TestStruct1{})
fmt.Println(err == nil)
// Output:
// true
// true
// CALLED DBTestFunc
// CALLED DBTestFunc4
// true
// true
// CALLED DBTestFunc5
// CALLED DBTestFunc4
// true
}