-
Notifications
You must be signed in to change notification settings - Fork 48
/
result_set_test.go
482 lines (449 loc) · 9.32 KB
/
result_set_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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package myreplication
import (
// "reflect"
"bytes"
"reflect"
"testing"
)
func TestReceiveQueryDataResultSetOneRowOneColumn(t *testing.T) {
mockBuff := []byte{
//length
0x01, 0x00, 0x00,
//sequence
0x01,
//columns count
0x01,
//length
0x27, 0x00, 0x00,
//sequence
0x02,
//catalog
0x03, 0x64, 0x65, 0x66,
//schema
0x00,
//table
0x00,
//orig_table
0x00,
//name
0x11, 0x40, 0x40, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74,
//orig name
0x00,
//filler
0x0c,
//char set
0x08, 0x00,
//column length
0x1c, 0x00, 0x00, 0x00,
//column type
0xfd,
//flags
0x00, 0x00,
//decimals
0x1f,
//filler
0x00, 0x00,
//length
0x05, 0x00, 0x00,
//sequence id
0x03,
//EOF
0xfe,
//warnings
0x00, 0x00,
//status flags
0x02, 0x00,
//length
0x1d, 0x00, 0x00,
//sequence id
0x04,
//data
0x1c, 0x4d, 0x79, 0x53, 0x51, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x74, 0x79, 0x20, 0x53,
0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x28, 0x47, 0x50, 0x4c, 0x29,
//length
0x05, 0x00, 0x00,
//sequence id
0x05,
//EOF
0xfe,
//warnings
0x00, 0x00,
//status flag
0x02, 0x00,
}
rs := resultSet{}
rs.reader = newPackReader(bytes.NewBuffer(mockBuff))
err := rs.init()
if err != nil {
t.Error("ResultSet init error:", err)
}
if len(rs.columns) != 1 {
t.Fatal(
"Incorrect columns count",
"expected", 1,
"got", len(rs.columns),
)
}
type (
checkPair struct {
columnName string
expectedData string
columnPointer *[]byte
}
)
checkDataMap := []*checkPair{
&checkPair{"catalog", "def", &(rs.columns[0].catalog)},
&checkPair{"schema", "", &(rs.columns[0].schema)},
&checkPair{"table", "", &(rs.columns[0].table)},
&checkPair{"org_table", "", &(rs.columns[0].org_table)},
&checkPair{"name", "@@version_comment", &(rs.columns[0].name)},
&checkPair{"org_name", "", &(rs.columns[0].org_name)},
}
for _, pair := range checkDataMap {
if string(*(pair.columnPointer)) != pair.expectedData {
t.Fatal(
"Incorrect column info", pair.columnName,
"expected", pair.expectedData,
"got", string(*pair.columnPointer),
)
}
}
pack, err := rs.nextRow()
if err != nil {
t.Fatal(
"Got incorrect error", err,
)
}
expectedString := "MySQL Community Server (GPL)"
str, err := pack.readStringLength()
if err != nil {
t.Fatal(
"Got incorrect error read string", err,
)
}
if string(str) != expectedString {
t.Fatal(
"Got incorrect data",
"expected", expectedString,
"got", string(str),
)
}
pack, err = rs.nextRow()
if err != EOF_ERR {
t.Fatal(
"Got incorrect next row", err,
)
}
}
func TestReceiveQueryDataResultSetOneRowManyColumns(t *testing.T) {
mockBuff := []byte{
0x01, 0x00, 0x00,
0x01,
0x02,
//column 1
0x27, 0x00, 0x00,
0x02,
0x03, 0x64, 0x65, 0x66,
0x00,
0x00,
0x00,
0x11, 0x40, 0x40, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74,
0x00,
0x0c,
0x08, 0x00,
0x08, 0x00, 0x00, 0x00,
0xfd,
0x00, 0x00,
0x1f,
0x00, 0x00,
//column 2
0x1f, 0x00, 0x00,
0x03,
0x03, 0x64, 0x65, 0x66,
0x00,
0x00,
0x00,
0x09, 0x40, 0x40, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
0x00,
0x0c,
0x08, 0x00,
0x1b, 0x00, 0x00, 0x00,
0xfd,
0x00, 0x00,
0x1f,
0x00, 0x00,
0x05, 0x00, 0x00,
0x04,
0xfe,
0x00, 0x00,
0x02, 0x00,
0x25, 0x00, 0x00,
0x05,
0x08, 0x28, 0x55, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x29,
0x1b, 0x35, 0x2e, 0x35, 0x2e, 0x33, 0x38, 0x2d, 0x30, 0x75, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x30, 0x2e, 0x31,
0x34, 0x2e, 0x30, 0x34, 0x2e, 0x31, 0x2d, 0x6c, 0x6f, 0x67,
0x05, 0x00, 0x00,
0x06,
0xfe,
0x00, 0x00,
0x02, 0x00,
}
rs := resultSet{}
rs.reader = newPackReader(bytes.NewBuffer(mockBuff))
err := rs.init()
if err != nil {
t.Fatal(
"Query got error",
err,
)
}
if len(rs.columns) != 2 {
t.Fatal(
"Incorrect columns count",
"expected", 1,
"got", len(rs.columns),
)
}
type (
checkPair struct {
columnName string
expectedData string
columnPointer *[]byte
}
)
checkDataMap := []*checkPair{
&checkPair{"catalog", "def", &(rs.columns[0].catalog)},
&checkPair{"schema", "", &(rs.columns[0].schema)},
&checkPair{"table", "", &(rs.columns[0].table)},
&checkPair{"org_table", "", &(rs.columns[0].org_table)},
&checkPair{"name", "@@version_comment", &(rs.columns[0].name)},
&checkPair{"org_name", "", &(rs.columns[0].org_name)},
&checkPair{"catalog", "def", &(rs.columns[1].catalog)},
&checkPair{"schema", "", &(rs.columns[1].schema)},
&checkPair{"table", "", &(rs.columns[1].table)},
&checkPair{"org_table", "", &(rs.columns[1].org_table)},
&checkPair{"name", "@@version", &(rs.columns[1].name)},
&checkPair{"org_name", "", &(rs.columns[1].org_name)},
}
for _, pair := range checkDataMap {
if string(*(pair.columnPointer)) != pair.expectedData {
t.Fatal(
"Incorrect column info", pair.columnName,
"expected", pair.expectedData,
"got", string(*pair.columnPointer),
)
}
}
pack, err := rs.nextRow()
if err != nil {
t.Fatal(
"Got incorrect error", err,
)
}
expectedString := "(Ubuntu)"
str, err := pack.readStringLength()
if err != nil {
t.Fatal(
"Got incorrect error read string", err,
)
}
if string(str) != expectedString {
t.Fatal(
"Got incorrect data",
"expected", expectedString,
"got", string(str),
)
}
expectedString = "5.5.38-0ubuntu0.14.04.1-log"
str, err = pack.readStringLength()
if err != nil {
t.Fatal(
"Got incorrect error read string", err,
)
}
if string(str) != expectedString {
t.Fatal(
"Got incorrect data",
"expected", expectedString,
"got", string(str),
)
}
pack, err = rs.nextRow()
if err != EOF_ERR {
t.Fatal(
"Got incorrect next row", err,
)
}
}
func TestFieldList(t *testing.T) {
mockBuff := []byte{
//column 1
0x2e, 0x00, 0x00,
0x01,
//catalog "def"
0x03,
0x64, 0x65, 0x66,
//schema "test"
0x04,
0x74, 0x65, 0x73, 0x74,
//table "table01"
0x07,
0x74, 0x61, 0x62, 0x6c, 0x65, 0x30, 0x31,
//orig_table "table01"
0x07,
0x74, 0x61, 0x62, 0x6c, 0x65, 0x30, 0x31,
//name "id"
0x02,
0x69, 0x64,
//orig_name "id"
0x02,
0x69, 0x64,
//filler
0x0c,
//char set
0x3f, 0x00,
//column length
0x0b, 0x00, 0x00, 0x00,
//column_type LONG
0x03,
//flags
0x03, 0x42,
//decimals
0x00,
//filler
0x00, 0x00,
//wtf???
0x01, 0x30,
//column 2
0x31, 0x00, 0x00,
0x02,
//catalog "def"
0x03,
0x64, 0x65, 0x66,
//schema "test"
0x04,
0x74, 0x65, 0x73, 0x74,
//table "table01"
0x07,
0x74, 0x61, 0x62, 0x6c, 0x65, 0x30, 0x31,
//orig_table "table01"
0x07,
0x74, 0x61, 0x62, 0x6c, 0x65, 0x30, 0x31,
//name "data"
0x04,
0x64, 0x61, 0x74, 0x61,
//orig_name "data"
0x04,
0x64, 0x61, 0x74, 0x61,
//filler
0x0c,
//char set
0x08, 0x00,
//column length
0x2d, 0x00, 0x00, 0x00,
//column type "VAR_STRING"
0xfd,
//flags
0x01, 0x10,
//decimals
0x00,
//filler
0x00, 0x00,
//wtf?
0x00,
//column 3
0x2d, 0x00, 0x00,
0x03,
//catalog "def"
0x03,
0x64, 0x65, 0x66,
//schema "test"
0x04,
0x74, 0x65, 0x73, 0x74,
//table "table01"
0x07,
0x74, 0x61, 0x62, 0x6c, 0x65, 0x30, 0x31,
//orig_table "table01"
0x07,
0x74, 0x61, 0x62, 0x6c, 0x65, 0x30, 0x31,
//name "dl"
0x02,
0x64, 0x6c,
//orig_name "dl"
0x02,
0x64, 0x6c,
//filler
0x0c,
//char set
0x3f, 0x00,
//column length
0x16, 0x00, 0x00, 0x00,
//column type DOUBLE
0x05,
//flags
0x00, 0x00,
//decimals
0x1f,
//filler
0x00, 0x00,
//wtf???
0xfb,
//EOF packet
0x05, 0x00, 0x00,
0x04,
0xfe, 0x00, 0x00,
0x02, 0x00,
}
rs := resultSet{}
rs.reader = newPackReader(bytes.NewBuffer(mockBuff))
err := rs.initFieldList()
if err != nil {
t.Fatal(
"Query got error",
err,
)
}
if len(rs.columns) != 3 {
t.Fatal(
"Incorrect columns count",
"expected", 3,
"got", len(rs.columns),
)
}
type (
checkPair struct {
columnName string
expectedData interface{}
columnPointer interface{}
}
)
checkDataMap := []*checkPair{
&checkPair{"schema", "test", rs.columns[0].schema},
&checkPair{"table", "table01", rs.columns[0].table},
&checkPair{"org_table", "table01", rs.columns[0].org_table},
&checkPair{"name", "id", rs.columns[0].name},
&checkPair{"org_name", "id", rs.columns[0].org_name},
&checkPair{"column_type", MYSQL_TYPE_LONG, rs.columns[0].column_type},
&checkPair{"schema", "test", rs.columns[1].schema},
&checkPair{"table", "table01", rs.columns[1].table},
&checkPair{"org_table", "table01", rs.columns[1].org_table},
&checkPair{"name", "data", rs.columns[1].name},
&checkPair{"org_name", "data", rs.columns[1].org_name},
&checkPair{"column_type", MYSQL_TYPE_VAR_STRING, rs.columns[1].column_type},
&checkPair{"schema", "test", rs.columns[2].schema},
&checkPair{"table", "table01", rs.columns[2].table},
&checkPair{"org_table", "table01", rs.columns[2].org_table},
&checkPair{"name", "dl", rs.columns[2].name},
&checkPair{"org_name", "dl", rs.columns[2].org_name},
&checkPair{"column_type", MYSQL_TYPE_DOUBLE, rs.columns[2].column_type},
}
for _, pair := range checkDataMap {
if reflect.DeepEqual(pair.columnPointer, pair.expectedData) {
t.Fatal(
"Incorrect column info", pair.columnName,
"expected", pair.expectedData,
"got", pair.columnPointer,
)
}
}
}