-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
GoMybatisSqlResultDecoder_test.go
382 lines (340 loc) · 12.2 KB
/
GoMybatisSqlResultDecoder_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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package GoMybatis
import (
"fmt"
"github.com/zhuxiujia/GoMybatis/utils"
"testing"
"time"
)
type TestResult struct {
Name string `json:"name"`
Amount1 float32 `json:"amount_1"`
Amount2 float64 `json:"amount_2"`
Age1 int `json:"age_1"`
Age2 int32 `json:"age_2"`
Age3 int64 `json:"age_3"`
Age4 uint `json:"age_4"`
Age5 uint8 `json:"age_5"`
Age6 uint16 `json:"age_6"`
Age7 uint32 `json:"age_7"`
Age8 uint64 `json:"age_8"`
Bool bool `json:"bool"`
F bool `json:"f"`
}
//解码基本数据-int,string,time.Time...
func Test_Convert_Basic_Type(t *testing.T) {
var resMap = make(map[string][]byte)
resMap["Amount1"] = []byte("1908")
var resMapArray = make([]map[string][]byte, 0)
resMapArray = append(resMapArray, resMap)
var intResult int
var error = GoMybatisSqlResultDecoder{}.Decode(nil, resMapArray, &intResult)
if error != nil {
t.Fatal(error)
}
fmt.Println("Test_Convert_Basic_Type,int=", intResult)
var stringResult string
error = GoMybatisSqlResultDecoder{}.Decode(nil, resMapArray, &stringResult)
if error != nil {
t.Fatal(error)
}
fmt.Println("Test_Convert_Basic_Type,string=", stringResult)
var floatResult float64
error = GoMybatisSqlResultDecoder{}.Decode(nil, resMapArray, &floatResult)
if error != nil {
t.Fatal(error)
}
fmt.Println("Test_Convert_Basic_Type,float=", floatResult)
resMap = make(map[string][]byte)
resMap["Date"] = []byte(time.Now().Format(time.RFC3339))
resMapArray = make([]map[string][]byte, 0)
resMapArray = append(resMapArray, resMap)
var timeResult time.Time
error = GoMybatisSqlResultDecoder{}.Decode(nil, resMapArray, &timeResult)
if error != nil {
t.Fatal(error)
}
fmt.Println("Test_Convert_Basic_Type,time=", timeResult)
}
//解码map
func Test_Convert_Map(t *testing.T) {
var resMap = make(map[string][]byte)
resMap["Amount1"] = []byte("1908")
resMap["Amount2"] = []byte("1901")
var resMapArray = make([]map[string][]byte, 0)
resMapArray = append(resMapArray, resMap)
var result map[string]interface{}
var error = GoMybatisSqlResultDecoder{}.Decode(nil, resMapArray, &result)
if error != nil {
t.Fatal(error)
}
fmt.Println("Test_Convert_Map", result)
resMapArray = append(resMapArray, resMap)
var resultMapArray []map[string]interface{}
error = GoMybatisSqlResultDecoder{}.Decode(nil, resMapArray, &resultMapArray)
if error != nil {
t.Fatal(error)
}
fmt.Println("Test_Convert_Map", resultMapArray)
}
func Test_convert_struct(t *testing.T) {
var GoMybatisSqlResultDecoder = GoMybatisSqlResultDecoder{}
var res = make([]map[string][]byte, 0)
var resMap = make(map[string][]byte)
resMap["Name"] = []byte("xiao ming")
resMap["Amount_1"] = []byte("1908.1")
resMap["Amount_2"] = []byte("1908.444")
resMap["Age_1"] = []byte("1908")
resMap["Age_2"] = []byte("1908")
resMap["Age_3"] = []byte("1908")
resMap["Age_4"] = []byte("1908")
resMap["Age_5"] = []byte("1")
resMap["Age_6"] = []byte("1908")
resMap["Age_7"] = []byte("1908")
resMap["Age_8"] = []byte("1908")
resMap["Bool"] = []byte("true")
res = append(res, resMap)
var result TestResult
GoMybatisSqlResultDecoder.Decode(nil, res, &result)
if result.Name != string(resMap["Name"]) {
panic("convert_struct Name fail")
}
if result.Amount1 != 1908.1 {
panic("convert_struct Amount1 fail")
}
if result.Amount2 != 1908.444 {
panic("convert_struct Amount2 fail")
}
if result.Age1 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age2 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age3 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age4 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age5 != 1 {
panic("convert_struct Age1 fail")
}
if result.Age6 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age7 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age8 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Bool != true {
panic("convert_struct Bool fail")
}
fmt.Println("Test_convert_struct", result)
}
func Test_convert_html_struct(t *testing.T) {
var GoMybatisSqlResultDecoder = GoMybatisSqlResultDecoder{}
var res = make([]map[string][]byte, 0)
var resMap = make(map[string][]byte)
resMap["Name"] = []byte("<p>adfd</p><section class=\"\n\twwei - editor \"><fieldset style=\"\n\tclear: both;padding: 0 px;border: 0 px none;margin: 1e m 0 px 0.5e m;\n\t\"><section style=\"\n\tborder - top: 2 px solid rgb(134, 110, 187);font - size: 1e m;font - weight: inherit;text - decoration: inherit;color: rgb(255, 255, 255);box - sizing: border - box;border - bottom - color: rgb(134, 110, 187);border - left - color: rgb(134, 110, 187);border - right - color: rgb(134, 110, 187);\n\t\"><section class=\"\n\twweibrush \" data-brushtype=\"\n\ttext \" style=\"\n\tpadding: 0 px 0.5e m;display: inline - block;font - size: 16 px;background - color: rgb(134, 110, 187);color: rgb(255, 255, 255);\n\t\">微信编辑器</section></section></fieldset></section><p><br/></p><section class=\"\n\twwei - editor \"><section style=\"\n\tfont - size: 1e m;white - space: normal;margin: 1e m 0 px 0.8e m;text - align: center;vertical - align: middle;\n\t\"><section style=\"\n\theight: 0 px;margin: 0 px 1e m;border - width: 1.5e m;border - style: solid;border - image: initial;border - left - color: transparent!important;border - right - color: transparent!important;border - bottom - color: rgb(134, 110, 187);border - top - color: rgb(134, 110, 187);\n\t\"></section><section style=\"\n\theight: 0 px;margin: -2.75e m 1.65e m;border - width: 1.3e m;border - style: solid;border - color: rgb(255, 255, 255) transparent;\n\t\"></section><section style=\"\n\theight: 0 px;margin: 0.45e m 2.1e m;vertical - align: middle;border - width: 1.1e m;border - style: solid;border - image: initial;border - left - color: transparent!important;border - right - color: transparent!important;border - bottom - color: rgb(134, 110, 187);border - top - color: rgb(134, 110, 187);\n\t\"><section class=\"\n\twweibrush \" data-brushtype=\"\n\ttext \" placeholder=\"\n\t一行短标题 \" style=\"\n\tmax - height: 1e m;padding: 0 px;margin - top: -0.5e m;color: rgb(255, 255, 255);font - size: 1.2e m;line - height: 1e m;overflow: hidden;\n\t\">一行短标题</section></section></section></section><p><br/></p><p><br/></p>")
resMap["Amount_1"] = []byte("1908.1")
resMap["Amount_2"] = []byte("1908.444")
resMap["Age_1"] = []byte("1908")
resMap["Age_2"] = []byte("1908")
resMap["Age_3"] = []byte("1908")
resMap["Age_4"] = []byte("1908")
resMap["Age_5"] = []byte("1")
resMap["Age_6"] = []byte("1908")
resMap["Age_7"] = []byte("1908")
resMap["Age_8"] = []byte("1908")
resMap["Bool"] = []byte("true")
res = append(res, resMap)
var result TestResult
GoMybatisSqlResultDecoder.Decode(nil, res, &result)
//if result.Name != string(resMap["Name"]) {
// panic("convert_struct Name fail")
//}
if result.Amount1 != 1908.1 {
panic("convert_struct Amount1 fail")
}
if result.Amount2 != 1908.444 {
panic("convert_struct Amount2 fail")
}
if result.Age1 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age2 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age3 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age4 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age5 != 1 {
panic("convert_struct Age1 fail")
}
if result.Age6 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age7 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Age8 != 1908 {
panic("convert_struct Age1 fail")
}
if result.Bool != true {
panic("convert_struct Bool fail")
}
fmt.Println("Test_convert_struct", result)
}
func Test_Ignore_Case_Underscores(t *testing.T) {
var GoMybatisSqlResultDecoder = GoMybatisSqlResultDecoder{}
var res = make([]map[string][]byte, 0)
var resMap = make(map[string][]byte)
resMap["name"] = []byte("xiao ming")
resMap["Amount_1"] = []byte("1908.1")
resMap["amount_2"] = []byte("1908.444")
resMap["age_1"] = []byte("1908")
resMap["age_2"] = []byte("1908")
resMap["age_3"] = []byte("1908")
resMap["age_4"] = []byte("1908")
resMap["age_5"] = []byte("1908")
resMap["age_6"] = []byte("1908")
resMap["age_7"] = []byte("1908")
resMap["age_8"] = []byte("1908")
resMap["Bool"] = []byte("1")
res = append(res, resMap)
var result TestResult
GoMybatisSqlResultDecoder.Decode(nil, res, &result)
fmt.Println("Test_Ignore_Case_Underscores", result)
}
func Test_Ignore_Case_Underscores_Tps(t *testing.T) {
var GoMybatisSqlResultDecoder = GoMybatisSqlResultDecoder{}
var res = make([]map[string][]byte, 0)
var resMap = make(map[string][]byte)
resMap["name"] = []byte("xiao ming")
resMap["Amount_1"] = []byte("1908.1")
resMap["amount_2"] = []byte("1908.444")
resMap["age_1"] = []byte("1908")
resMap["age_2"] = []byte("1908")
resMap["age_3"] = []byte("1908")
resMap["age_4"] = []byte("1908")
resMap["age_5"] = []byte("1908")
resMap["age_6"] = []byte("1908")
resMap["age_7"] = []byte("1908")
resMap["age_8"] = []byte("1908")
resMap["Bool"] = []byte("1")
res = append(res, resMap)
var result TestResult
defer utils.CountMethodTps(10000, time.Now(), "Test_Ignore_Case_Underscores_Tps")
for i := 0; i < 10000; i++ {
GoMybatisSqlResultDecoder.Decode(nil, res, &result)
}
}
func Test_Decode_Interface(t *testing.T) {
var res = make([]map[string][]byte, 0)
var resMap = make(map[string][]byte)
resMap["name"] = []byte("xiao ming")
resMap["Amount_1"] = []byte("1908.1")
resMap["amount_2"] = []byte("1908.444")
resMap["age_1"] = []byte("1908")
resMap["age_2"] = []byte("1908")
resMap["age_3"] = []byte("1908")
resMap["age_4"] = []byte("1908")
resMap["age_5"] = []byte("1908")
resMap["age_6"] = []byte("1908")
resMap["age_7"] = []byte("1908")
resMap["age_8"] = []byte("1908")
resMap["Bool"] = []byte("1")
res = append(res, resMap)
var resultMap = make(map[string]*ResultProperty)
resultMap["id"] = &ResultProperty{
XMLName: "id",
Column: "id",
LangType: "string",
}
resultMap["name"] = &ResultProperty{
XMLName: "result",
Column: "name",
LangType: "string",
}
resultMap["Amount_1"] = &ResultProperty{
XMLName: "result",
Column: "Amount_1",
LangType: "string",
}
resultMap["amount_2"] = &ResultProperty{
XMLName: "result",
Column: "Amount_2",
LangType: "string",
}
var result map[string]string
GoMybatisSqlResultDecoder{}.Decode(resultMap, res, &result)
fmt.Println("Test_Decode_Interface", result)
}
func Benchmark_Ignore_Case_Underscores(b *testing.B) {
b.StopTimer()
var GoMybatisSqlResultDecoder = GoMybatisSqlResultDecoder{}
var res = make([]map[string][]byte, 0)
var resMap = make(map[string][]byte)
resMap["name"] = []byte("xiao ming")
resMap["Amount_1"] = []byte("1908.1")
resMap["amount_2"] = []byte("1908.444")
resMap["age_1"] = []byte("1908")
resMap["age_2"] = []byte("1908")
resMap["age_3"] = []byte("1908")
resMap["age_4"] = []byte("1908")
resMap["age_5"] = []byte("1908")
resMap["age_6"] = []byte("1908")
resMap["age_7"] = []byte("1908")
resMap["age_8"] = []byte("1908")
resMap["Bool"] = []byte("1")
res = append(res, resMap)
var result TestResult
b.StartTimer()
for i := 0; i < b.N; i++ {
GoMybatisSqlResultDecoder.Decode(nil, res, &result)
}
}
//
//func TestGoMybatisSqlResultDecoder_Decode(t *testing.T) {
// var GoMybatisSqlResultDecoder = GoMybatisSqlResultDecoder{}
// var res = make([]map[string][]byte, 0)
// var resMap = make(map[string][]byte)
// resMap["name"] = []byte("xiao ming")
// resMap["Amount_1"] = []byte("1908.1")
// resMap["amount_2"] = []byte("1908.444")
// resMap["age_1"] = []byte("1908")
// resMap["age_2"] = []byte("1908")
// resMap["age_3"] = []byte("1908")
// resMap["age_4"] = []byte("1908")
// resMap["age_5"] = []byte("1908")
// resMap["age_6"] = []byte("1908")
// resMap["age_7"] = []byte("1908")
// resMap["age_8"] = []byte("1908")
// resMap["Bool"] = []byte("1")
// res = append(res, resMap)
// var result TestResult
// var err = GoMybatisSqlResultDecoder.Decode(nil, res, &result)
// if err != nil {
// t.Fatal(err)
// }
// if result.Name == "" ||
// result.Amount1 == 0 ||
// result.Amount2 == 0 ||
// result.Age1 == 0 ||
// result.Age2 == 0 ||
// result.Age3 == 0 ||
// result.Age4 == 0 ||
// result.Age5 == 0 ||
// result.Age6 == 0 ||
// result.Age7 == 0 ||
// result.Age8 == 0 ||
// result.Bool == false {
// t.Fatal("TestGoMybatisSqlResultDecoder_Decode fail,result not decoded!")
// }
// fmt.Println(result)
//}