-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_test.go
263 lines (257 loc) · 4.3 KB
/
struct_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
package reflect
import (
"reflect"
"testing"
)
func TestDiffSlice(t *testing.T) {
type testModel struct {
name string
age int
}
var testCase = []struct {
id int
newSlice interface{}
oldSlice interface{}
addSlice []interface{}
deleteSlice []interface{}
}{
{
1,
[]string{"222", "333", "4444"},
[]string{"111", "555", "4444"},
[]interface{}{"222", "333"},
[]interface{}{"111", "555"},
},
{
2,
[]string{"222", "333", "4444"},
[]string{},
[]interface{}{"222", "333", "4444"},
[]interface{}{},
},
{
3,
[]string{},
[]string{"222", "333", "4444"},
[]interface{}{},
[]interface{}{"222", "333", "4444"},
},
{
4,
[]int{3, 4, 5},
[]int{3, 4, 6},
[]interface{}{5},
[]interface{}{6},
},
{
5,
[]testModel{
testModel{
name: "test1",
age: 21,
},
testModel{
name: "test2",
age: 22,
},
},
[]testModel{
testModel{
name: "test3",
age: 23,
},
testModel{
name: "test2",
age: 22,
},
},
[]interface{}{
testModel{
name: "test1",
age: 21,
},
},
[]interface{}{
testModel{
name: "test3",
age: 23,
},
},
},
{
6,
[]int{3, 4, 5, 6},
[]int{3, 4, 5},
[]interface{}{6},
[]interface{}{},
},
}
for _, test := range testCase {
addSlice, deleteSlice, _ := CompareSlice(test.newSlice, test.oldSlice)
switch {
case len(addSlice) == 0 && len(test.addSlice) == 0:
break
case !reflect.DeepEqual(addSlice, test.addSlice):
t.Errorf("%d test failed. Add slice Want => %v , Get => %v", test.id, test.addSlice, addSlice)
}
switch {
case len(deleteSlice) == 0 && len(test.deleteSlice) == 0:
break
case !reflect.DeepEqual(deleteSlice, test.deleteSlice):
t.Errorf("%d test failed. Delete slice Want => %v , Get => %v", test.id, test.deleteSlice, deleteSlice)
}
}
}
func TestTransformStruct(t *testing.T) {
type c struct {
Name string
}
type cwant struct {
Name string
}
type testModel struct {
Name string
ID int
Next c
IntList []int
C []c
}
type testWantModel struct {
Name string
ID int
Next cwant
C []cwant
IntList []int
}
var testCase = []struct {
id int
input testModel
want testWantModel
}{
{
1, testModel{
Name: "test",
ID: 1,
Next: c{
Name: "test",
},
C: []c{
c{
Name: "test",
},
c{
Name: "test",
},
},
IntList: []int{111, 222},
},
testWantModel{
Name: "test",
ID: 1,
Next: cwant{
Name: "test",
},
C: []cwant{
cwant{
Name: "test",
},
cwant{
Name: "test",
},
},
IntList: []int{111, 222},
},
},
}
for _, test := range testCase {
var to testWantModel
TransformStruct(&test.input, &to)
if !reflect.DeepEqual(to, test.want) {
t.Errorf("test failed. Want => %v , Get => %v", test.want, to)
}
}
}
func TestSumSliceParamsValue(t *testing.T) {
type testModel struct {
A string
B int
C string
}
var testCase = []struct {
id int
input interface{}
want int32
}{
{
1,
[]testModel{
{"1", 1, "1"},
{"2", 2, "2"},
},
3,
},
}
for _, test := range testCase {
var sum int32
SumSliceParamsValue(test.input, "B", &sum)
if sum != test.want {
t.Errorf("test failed. Want => %v , Get => %v", test.want, sum)
}
}
}
func TestFilterSlice(t *testing.T) {
type testModel struct {
A string
B int
C string
}
var testCase = []struct {
id int
input []testModel
filterName string
filterValue interface{}
filter bool
want []testModel
}{
{
1,
[]testModel{
{"1", 1, "1"},
{"2", 2, "2"},
{"3", 2, "3"},
},
"B",
2,
true,
[]testModel{
{"1", 1, "1"},
},
},
{
1,
[]testModel{
{"1", 1, "1"},
{"2", 2, "2"},
{"3", 2, "3"},
},
"B",
2,
false,
[]testModel{
{"2", 2, "2"},
{"3", 2, "3"},
},
},
}
for _, test := range testCase {
var sum int32
if test.filter {
FilterSlice(&test.input, test.filterName, test.filterValue)
} else {
FilterSlice(&test.input, test.filterName, test.filterValue, false)
}
if !reflect.DeepEqual(test.input, test.want) {
t.Errorf("test failed. Want => %v , Get => %v", test.want, sum)
}
}
}