-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_bag_inner_test.go
More file actions
188 lines (158 loc) · 3.66 KB
/
data_bag_inner_test.go
File metadata and controls
188 lines (158 loc) · 3.66 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
package choreograph
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
NoErrorWanted = func(assert.TestingT, error, ...interface{}) bool { return true }
ErrorWanted = func(_ assert.TestingT, err error, _ ...interface{}) bool { return err != nil }
)
type (
storeGetArgs struct {
keyName string
storeVal interface{}
}
storeGetTestCase struct {
name string
args storeGetArgs
getKeyName string
expected interface{}
expectExists bool
}
)
func TestDataBag_GetJobData(t *testing.T) {
tests := getStoreGetTestCases()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := new(DataBag)
d.SetJobData(tt.args.keyName, tt.args.storeVal)
got, exists := d.GetJobData(tt.getKeyName)
assert.Equalf(t, tt.expected, got, "GetData(%v)", tt.args.keyName)
assert.Equalf(t, tt.expectExists, exists, "GetData(%v)", tt.args.keyName)
})
}
}
func TestDataBag_GetPreCheckData(t *testing.T) {
tests := getStoreGetTestCases()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := new(DataBag)
d.SetPreCheckData(tt.args.keyName, tt.args.storeVal)
got, exists := d.GetPreCheckData(tt.getKeyName)
assert.Equalf(t, tt.expected, got, "GetData(%v)", tt.args.keyName)
assert.Equalf(t, tt.expectExists, exists, "GetData(%v)", tt.args.keyName)
})
}
}
func TestDataBag_clear(t *testing.T) {
const (
keyName = "test"
value = 1
)
d := new(DataBag)
doStoreCheck := func() {
// Setting data and getting it (check if was stored)
d.setData(keyName, value)
val, ok := d.getData(keyName)
require.Truef(t, ok, "expected that '"+keyName+"' key will exist")
require.Equal(t, value, val)
}
doStoreCheck()
// Clearing the bag
d.clear()
// Again trying to get the data and check if there is none
val2, ok2 := d.getData(keyName)
require.Falsef(t, ok2, "expected that '"+keyName+"' won't exist")
require.Nil(t, val2)
// Let's do store one more time to see if working fine
doStoreCheck()
}
func TestDataBag_getKeyName(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "first",
want: "first",
},
{
name: "Second",
want: "second",
},
{
name: "ThIrd",
want: "third",
},
{
name: "Forth!2313-=+",
want: "forth!2313-=+",
},
}
bag := new(DataBag)
for _, tt := range tests {
t.Run("", func(t *testing.T) {
assert.Equalf(t, tt.want, bag.getKeyName(tt.name), "getKeyName(%v)", tt.name)
})
}
}
func TestGetDataBagFromCtx(t *testing.T) {
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want *DataBag
wantErr assert.ErrorAssertionFunc
}{
{
name: "data bag in context",
args: args{
ctx: context.WithValue(context.Background(), DataBagContextKey, &DataBag{}),
},
want: &DataBag{},
wantErr: NoErrorWanted,
},
{
name: "no data bag in context",
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: ErrorWanted,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetDataBagFromCtx(tt.args.ctx)
if !tt.wantErr(t, err, fmt.Sprintf("GetDataBagFromCtx(%v)", tt.args.ctx)) {
return
}
assert.Equalf(t, tt.want, got, "GetDataBagFromCtx(%v)", tt.args.ctx)
})
}
}
func getStoreGetTestCases() []storeGetTestCase {
return []storeGetTestCase{
{
name: "store simple data",
args: storeGetArgs{
keyName: "one",
storeVal: 1,
},
getKeyName: "one",
expected: 1,
expectExists: true,
},
{
name: "store nothing",
getKeyName: "one",
expected: nil,
expectExists: false,
},
}
}