-
Notifications
You must be signed in to change notification settings - Fork 4
/
ddt_test.go
358 lines (340 loc) · 16.7 KB
/
ddt_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
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
package ddt
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/sgrodriguez/ddt/compare"
"github.com/sgrodriguez/ddt/function"
"github.com/sgrodriguez/ddt/value"
)
func TestResolveTree_SimpleTree(t *testing.T) {
leaf1 := Node{
ID: 1,
ParentID: 0,
ValueToCompare: &value.Value{Value: int64(60), Type: value.Int64},
Comparer: &compare.Greater{},
Result: &value.Value{Value: "prize1", Type: value.String},
}
leaf11 := Node{
ID: 3,
ParentID: 2,
ValueToCompare: &value.Value{Value: int64(30), Type: value.Int64},
Comparer: &compare.Equal{},
Result: &value.Value{Value: "prize2", Type: value.String},
}
leaf12 := Node{
ID: 4,
ParentID: 2,
ValueToCompare: &value.Value{Value: int64(30), Type: value.Int64},
Comparer: &compare.Greater{},
Result: &value.Value{Value: "prize3", Type: value.String},
}
leaf13 := Node{
ID: 5,
ParentID: 2,
ValueToCompare: &value.Value{Value: int64(30), Type: value.Int64},
Comparer: &compare.Lesser{},
Result: &value.Value{Value: "prize4", Type: value.String},
}
node1 := Node{
Children: []*Node{&leaf11, &leaf12, &leaf13},
ID: 2,
ParentID: 0,
ValueToCompare: &value.Value{Value: int64(60), Type: value.Int64},
Comparer: &compare.Lesser{Equal: true},
}
root := Node{
ID: 0,
ParentID: -1,
Children: []*Node{&node1, &leaf1},
}
simpleTree, err := NewTree("simpleTree", &root)
require.NoError(t, err)
testCases := map[string]struct {
input int64
expected string
}{
"given 100 as input expect prize1": {input: 100, expected: "prize1"},
"given 45 as input expect prize3": {input: 45, expected: "prize3"},
"given 30 as input expect prize2": {input: 30, expected: "prize2"},
"given 10 as input expect prize4": {input: 10, expected: "prize4"},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
actual, err := ResolveTree(simpleTree, tc.input)
require.NoError(t, err)
assert.Equal(t, tc.expected, actual.(string))
})
}
_, err = ResolveTree(simpleTree, "invalid input")
require.Error(t, err)
_, err = ResolveTree(simpleTree, nil)
require.Error(t, err)
_, err = ResolveTree(simpleTree, 4.20)
require.Error(t, err)
}
type user struct {
Age int
FirstName string
LastName string
}
func (u *user) UnderAge() bool {
return u.Age < 18
}
func (u *user) FullName() string {
return u.FirstName + " " + u.LastName
}
func (u *user) ReturnErr() error {
return errors.New("always error")
}
func newUser(age int, firstName, lastName string) *user {
return &user{
Age: age,
FirstName: firstName,
LastName: lastName,
}
}
func userTree() *Tree {
node6 := &Node{
ID: 6,
ParentID: 2,
ValueToCompare: &value.Value{Type: value.Int, Value: 30},
Result: &value.Value{Type: value.String, Value: "node6"},
Comparer: &compare.Greater{},
}
node5 := &Node{
ID: 5,
ParentID: 2,
ValueToCompare: &value.Value{Type: value.Int, Value: 30},
Result: &value.Value{Type: value.String, Value: "node5"},
Comparer: &compare.Lesser{Equal: true},
}
node3 := &Node{
ID: 3,
ParentID: 1,
ValueToCompare: &value.Value{Type: value.String, Value: "SANTIAGO LUCIA"},
Result: &value.Value{Type: value.String, Value: "node3"},
Comparer: &compare.Equal{},
}
node4 := &Node{
ID: 4,
ParentID: 1,
ValueToCompare: &value.Value{Type: value.String, Value: "LUCIA SANTIAGO"},
Result: &value.Value{Type: value.String, Value: "node4"},
Comparer: &compare.Equal{},
}
node1 := &Node{
ID: 1,
ParentID: 0,
Children: []*Node{node3, node4},
ValueToCompare: &value.Value{Type: value.Bool, Value: true},
PreProcessArgs: []*value.Value{{Type: value.String, Value: "FullName"}},
PreProcessFn: function.PreProcessFn{Function: function.CallStructMethod, Name: "CallStructMethod"},
Comparer: &compare.Equal{},
}
node2 := &Node{
ID: 2,
ParentID: 0,
Children: []*Node{node5, node6},
ValueToCompare: &value.Value{Type: value.Bool, Value: false},
Comparer: &compare.Equal{},
PreProcessArgs: []*value.Value{{Type: value.String, Value: "Age"}},
PreProcessFn: function.PreProcessFn{Function: function.GetStructAttribute, Name: "GetStructAttribute"},
}
root := &Node{
Children: []*Node{node1, node2},
PreProcessArgs: []*value.Value{{Type: value.String, Value: "UnderAge"}},
PreProcessFn: function.PreProcessFn{Function: function.CallStructMethod, Name: "CallStructMethod"},
ID: 0,
ParentID: -1,
}
tree, _ := NewTree("userTree", root)
return tree
}
func TestResolveTree_UserTree(t *testing.T) {
userTree := userTree()
b, err := json.Marshal(userTree)
require.NoError(t, err)
treeFromJSON, err := NewTree("treeFromJSON", &Node{ID: 0, ParentID: -1})
err = json.Unmarshal(b, treeFromJSON)
require.NoError(t, err)
testCases := map[string]struct {
input *user
expected string
}{
"expected node3": {input: newUser(11, "SANTIAGO", "LUCIA"), expected: "node3"},
"expected node4": {input: newUser(11, "LUCIA", "SANTIAGO"), expected: "node4"},
"expected node5": {input: newUser(25, "LUCIA", "SANTIAGO"), expected: "node5"},
"expected node6": {input: newUser(65, "LUCIA", "SANTIAGO"), expected: "node6"},
}
for testName, tc := range testCases {
t.Run(testName, func(t *testing.T) {
actual, err := ResolveTree(userTree, tc.input)
require.NoError(t, err)
actualFromNewTree, err := ResolveTree(treeFromJSON, tc.input)
require.NoError(t, err)
assert.Equal(t, tc.expected, actual.(string))
assert.Equal(t, tc.expected, actualFromNewTree.(string))
})
}
//
t.Run("handle empty/invalid user", func(t *testing.T) {
_, err = ResolveTree(userTree, &user{})
require.Error(t, err)
assert.EqualError(t, err, "value not found when comparing with all children nodes")
_, err = ResolveTree(userTree, nil)
require.Error(t, err)
assert.EqualError(t, err, "callStructMethod invalid methods args")
_, err = ResolveTree(userTree, 123)
require.Error(t, err)
assert.EqualError(t, err, "callStructMethod invalid methods args")
})
t.Run("handling method error", func(t *testing.T) {
node1 := userTree.Root.Children[0]
node1.PreProcessArgs = []*value.Value{{Type: value.String, Value: "ReturnErr"}}
_, err = ResolveTree(userTree, &user{Age: 11})
require.Error(t, err)
assert.EqualError(t, err, "always error")
})
t.Run("invalid method name", func(t *testing.T) {
node1 := userTree.Root.Children[0]
node1.PreProcessArgs = []*value.Value{{Type: value.String, Value: "ASD"}}
_, err = ResolveTree(userTree, &user{Age: 11})
assert.EqualError(t, err, "callStructMethod invalid methods args")
})
t.Run("invalid struct attribute name", func(t *testing.T) {
node2 := userTree.Root.Children[1]
node2.PreProcessArgs = []*value.Value{{Type: value.String, Value: "ASD"}}
_, err = ResolveTree(userTree, &user{Age: 30})
assert.EqualError(t, err, "getStructAttribute invalid struct attribute")
})
}
// TODO replace json []byte literal with some lib to manipulate better (ie simplejson)
func TestModifyTreeTroughJSON(t *testing.T) {
t.Run("modified node4 result to 100 int", func(t *testing.T) {
ut := userTree()
valueBeforeModified, err := ResolveTree(ut, newUser(11, "LUCIA", "SANTIAGO"))
require.NoError(t, err)
assert.Equal(t, "node4", valueBeforeModified.(string))
newTree := []byte(`{"nodes":[{"preProcessFnName":"CallStructMethod","id":0,"parentId":-1,"preProcessFnArgs":[{"Value":"UnderAge","Type":"string"}]},{"preProcessFnName":"CallStructMethod","id":1,"parentId":0,"preProcessFnArgs":[{"Value":"FullName","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":true,"Type":"bool"}},{"preProcessFnName":"GetStructAttribute","id":2,"parentId":0,"preProcessFnArgs":[{"Value":"Age","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":false,"Type":"bool"}},{"preProcessFnName":"","id":3,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"SANTIAGO LUCIA","Type":"string"},"result":{"Value":"node3","Type":"string"}},{"preProcessFnName":"","id":4,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"LUCIA SANTIAGO","Type":"string"},"result":{"Value":100,"Type":"int"}},{"preProcessFnName":"","id":5,"parentId":2,"comparer":{"type":"lt","equal":true},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node5","Type":"string"}},{"preProcessFnName":"","id":6,"parentId":2,"comparer":{"type":"gt","equal":false},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node6","Type":"string"}}],"name":"userTree"}`)
err = json.Unmarshal(newTree, ut)
require.NoError(t, err)
actualResult, err := ResolveTree(ut, newUser(11, "LUCIA", "SANTIAGO"))
require.NoError(t, err)
assert.Equal(t, 100, actualResult.(int))
})
t.Run("modified under age node value to compare", func(t *testing.T) {
ut := userTree()
resultBeforeModified, err := ResolveTree(ut, newUser(33, "SANTIAGO", "LUCIA"))
require.NoError(t, err)
assert.Equal(t, "node6", resultBeforeModified.(string))
newTree := []byte(`{"nodes":[{"preProcessFnName":"CallStructMethod","id":0,"parentId":-1,"preProcessFnArgs":[{"Value":"UnderAge","Type":"string"}]},{"preProcessFnName":"CallStructMethod","id":1,"parentId":0,"preProcessFnArgs":[{"Value":"FullName","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":false,"Type":"bool"}},{"preProcessFnName":"GetStructAttribute","id":2,"parentId":0,"preProcessFnArgs":[{"Value":"Age","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":true,"Type":"bool"}},{"preProcessFnName":"","id":3,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"SANTIAGO LUCIA","Type":"string"},"result":{"Value":"node3","Type":"string"}},{"preProcessFnName":"","id":4,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"LUCIA SANTIAGO","Type":"string"},"result":{"Value":"node4","Type":"string"}},{"preProcessFnName":"","id":5,"parentId":2,"comparer":{"type":"lt","equal":true},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node5","Type":"string"}},{"preProcessFnName":"","id":6,"parentId":2,"comparer":{"type":"gt","equal":false},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node6","Type":"string"}}],"name":"userTree"}`)
err = json.Unmarshal(newTree, ut)
require.NoError(t, err)
actualResult, err := ResolveTree(ut, newUser(33, "SANTIAGO", "LUCIA"))
require.NoError(t, err)
assert.Equal(t, "node3", actualResult.(string))
})
t.Run("modified age compare from gt to lt", func(t *testing.T) {
ut := userTree()
resultBeforeModified, err := ResolveTree(ut, newUser(55, "SANTIAGO", "LUCIA"))
require.NoError(t, err)
assert.Equal(t, "node6", resultBeforeModified.(string))
newTree := []byte(`{"nodes":[{"preProcessFnName":"CallStructMethod","id":0,"parentId":-1,"preProcessFnArgs":[{"Value":"UnderAge","Type":"string"}]},{"preProcessFnName":"CallStructMethod","id":1,"parentId":0,"preProcessFnArgs":[{"Value":"FullName","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":true,"Type":"bool"}},{"preProcessFnName":"GetStructAttribute","id":2,"parentId":0,"preProcessFnArgs":[{"Value":"Age","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":false,"Type":"bool"}},{"preProcessFnName":"","id":3,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"SANTIAGO LUCIA","Type":"string"},"result":{"Value":"node3","Type":"string"}},{"preProcessFnName":"","id":4,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"LUCIA SANTIAGO","Type":"string"},"result":{"Value":"node4","Type":"string"}},{"preProcessFnName":"","id":5,"parentId":2,"comparer":{"type":"gt","equal":false},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node5","Type":"string"}},{"preProcessFnName":"","id":6,"parentId":2,"comparer":{"type":"lt","equal":true},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node6","Type":"string"}}],"name":"userTree"}`)
err = json.Unmarshal(newTree, ut)
require.NoError(t, err)
actualResult, err := ResolveTree(ut, newUser(55, "SANTIAGO", "LUCIA"))
require.NoError(t, err)
assert.Equal(t, "node5", actualResult.(string))
})
t.Run("modified Fullname pre process CallStructMethod fn to FirstName GetStructAttribute fn", func(t *testing.T) {
ut := userTree()
resultBeforeModified, err := ResolveTree(ut, newUser(11, "SANTIAGO", "LUCIA"))
require.NoError(t, err)
assert.Equal(t, "node3", resultBeforeModified.(string))
resultBeforeModified, err = ResolveTree(ut, newUser(11, "LUCIA", "SANTIAGO"))
require.NoError(t, err)
assert.Equal(t, "node4", resultBeforeModified.(string))
newTree := []byte(`{"nodes":[{"preProcessFnName":"CallStructMethod","id":0,"parentId":-1,"preProcessFnArgs":[{"Value":"UnderAge","Type":"string"}]},{"preProcessFnName":"GetStructAttribute","id":1,"parentId":0,"preProcessFnArgs":[{"Value":"FirstName","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":true,"Type":"bool"}},{"preProcessFnName":"GetStructAttribute","id":2,"parentId":0,"preProcessFnArgs":[{"Value":"Age","Type":"string"}],"comparer":{"type":"eq"},"valueToCompare":{"Value":false,"Type":"bool"}},{"preProcessFnName":"","id":3,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"SANTIAGO","Type":"string"},"result":{"Value":"node3","Type":"string"}},{"preProcessFnName":"","id":4,"parentId":1,"comparer":{"type":"eq"},"valueToCompare":{"Value":"LUCIA","Type":"string"},"result":{"Value":"node4","Type":"string"}},{"preProcessFnName":"","id":5,"parentId":2,"comparer":{"type":"lt","equal":true},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node5","Type":"string"}},{"preProcessFnName":"","id":6,"parentId":2,"comparer":{"type":"gt","equal":false},"valueToCompare":{"Value":30,"Type":"int"},"result":{"Value":"node6","Type":"string"}}],"name":"userTree"}`)
err = json.Unmarshal(newTree, ut)
require.NoError(t, err)
actualResult, err := ResolveTree(ut, newUser(11, "SANTIAGO", "xd"))
require.NoError(t, err)
assert.Equal(t, "node3", actualResult.(string))
actualResult, err = ResolveTree(ut, newUser(11, "LUCIA", "xd"))
require.NoError(t, err)
assert.Equal(t, "node4", actualResult.(string))
})
}
func TestNewTree_InvalidRootNode(t *testing.T) {
roots := []*Node{
{
ParentID: 2,
},
{
ParentID: -1,
ID: 3,
},
{
ParentID: -1,
ID: 0,
Result: &value.Value{Value: 10, Type: value.Int},
},
}
for _, r := range roots {
_, err := NewTree("invalidTree", r)
require.Error(t, err)
}
}
func TestMarshalComparator(t *testing.T) {
tests := map[string]struct {
input Comparer
expected string
}{
"marshal equal": {input: &compare.Equal{}, expected: `{"type":"eq"}`},
"marshal greater": {input: &compare.Greater{}, expected: `{"equal":false, "type":"gt"}`},
"marshal greater or equal": {input: &compare.Greater{Equal: true}, expected: `{"equal":true, "type":"gt"}`},
"marshal lesser": {input: &compare.Lesser{}, expected: `{"equal":false, "type":"lt"}`},
"marshal lesser or equal": {input: &compare.Lesser{Equal: true}, expected: `{"equal":true, "type":"lt"}`},
}
for name, tst := range tests {
t.Run(name, func(t *testing.T) {
got, err := json.Marshal(&tst.input)
assert.NoError(t, err, "expected no error in marshaling")
assert.JSONEq(t, tst.expected, string(got), "expected compare marshaled not differs %s", name)
})
}
}
func TestUnmarshallComparator(t *testing.T) {
tests := map[string]struct {
input string
expected Comparer
}{
"unmarshal equal": {expected: &compare.Equal{}, input: `{"type":"eq"}`},
"unmarshal greater": {expected: &compare.Greater{}, input: `{"equal":false, "type":"gt"}`},
"unmarshal greater or equal": {expected: &compare.Greater{Equal: true}, input: `{"equal":true, "type":"gt"}`},
"unmarshal lesser": {expected: &compare.Lesser{}, input: `{"equal":false, "type":"lt"}`},
"unmarshal lesser or equal": {expected: &compare.Lesser{Equal: true}, input: `{"equal":true, "type":"lt"}`},
}
for name, tst := range tests {
t.Run(name, func(t *testing.T) {
comp, err := createComparatorFromJSON(json.RawMessage(tst.input))
assert.NoError(t, err, "expected no error in unmarshalling")
assert.Equal(t, tst.expected, comp, "expected value unmarshalled differs %s", name)
})
}
testsError := map[string]struct {
input string
}{
"unmarshal unknown type": {input: `{"type":"asd"}`},
"unmarshal invalid json type": {input: `{"equal":"asd"}`},
}
for name, tst := range testsError {
t.Run(name, func(t *testing.T) {
_, err := createComparatorFromJSON(json.RawMessage(tst.input))
assert.Error(t, err, "expected error in unmarshalling")
})
}
}