-
Notifications
You must be signed in to change notification settings - Fork 36
/
query_test.go
569 lines (485 loc) · 16.5 KB
/
query_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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package gorqlite_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/rqlite/gorqlite"
)
func TestQueryOne(t *testing.T) {
// give an extra second for time diff between local and rqlite
started := time.Now().Add(-time.Second)
// When the Federation met the Cardassians
meeting := time.Date(2424, 1, 2, 17, 0, 0, 0, time.UTC)
met := fmt.Sprint(meeting.Unix())
t.Logf("trying Write INSERT")
s := make([]string, 0)
s = append(s, "CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT, wallet REAL, bankrupt INTEGER, payload BLOB, ts DATETIME)")
s = append(s, "INSERT INTO "+testTableName()+" (id, name, wallet, bankrupt, payload, ts) VALUES ( 1, 'Romulan', 123.456, 0, '{\"met\":\""+met+"\"}', "+fmt.Sprint(time.Now().Unix())+" )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name, wallet, bankrupt, payload, ts) VALUES ( 2, 'Vulcan', 123.456, 0, '{\"met\":\""+met+"\"}', "+fmt.Sprint(time.Now().Unix())+" )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name, wallet, bankrupt, payload, ts) VALUES ( 3, 'Klingon', 123.456, 1, '{\"met\":\""+met+"\"}', "+fmt.Sprint(time.Now().Unix())+" )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name, wallet, bankrupt, payload, ts) VALUES ( 4, 'Ferengi', 123.456, 0, '{\"met\":\""+met+"\"}', "+fmt.Sprint(time.Now().Unix())+" )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name, wallet, bankrupt, payload, ts) VALUES ( 5, 'Cardassian', 123.456, 1, '{\"met\":\""+met+"\"}', "+met+" )")
wResults, err := globalConnection.Write(s)
if err != nil {
t.Errorf("failed during insert: %v", err.Error())
for _, wr := range wResults {
if wr.Err != nil {
t.Errorf("caught error: %v", wr.Err.Error())
}
}
}
t.Cleanup(func() {
results, err := globalConnection.Write([]string{"DROP TABLE " + testTableName()})
if err != nil {
t.Errorf("failed during dropping table: %s", err.Error())
for _, r := range results {
if r.Err != nil {
t.Errorf("caught error: %s", err.Error())
}
}
}
})
t.Run("Normal case", func(t *testing.T) {
qr, err := globalConnection.QueryOne("SELECT name, ts, wallet, bankrupt, payload FROM " + testTableName() + " WHERE id > 3")
if err != nil {
t.Errorf("failed during query: %v", err.Error())
if qr.Err != nil {
t.Errorf("query errors: %s", qr.Err.Error())
}
}
if qr.NumRows() != 2 {
t.Errorf("expected 2 row, got %v", qr.NumRows())
}
if len(qr.Columns()) != 5 {
t.Errorf("expected 5 columns, got %v", len(qr.Columns()))
}
if len(qr.Columns()) == 5 {
expect := []string{"name", "ts", "wallet", "bankrupt", "payload"}
for i, c := range qr.Columns() {
if c != expect[i] {
t.Errorf("expected column %v to be %v, got %v", i, expect[i], c)
}
}
}
if len(qr.Types()) != 5 {
t.Errorf("expected 5 types, got %v", len(qr.Types()))
}
na := qr.Next()
if na != true {
t.Errorf("expected true, got %v", na)
}
if qr.RowNumber() != 0 {
t.Errorf("expected row number to be 0, got %v", qr.RowNumber())
}
t.Logf("trying Map()")
r, err := qr.Map()
if err != nil {
t.Errorf("failed during map: %v", err.Error())
}
if r["name"].(string) != "Ferengi" {
t.Errorf("expected Ferengi, got %v", r["name"])
}
if tsi, ok := r["ts"]; ok {
if ts, ok := tsi.(time.Time); ok {
// time should not be zero because it defaults to current utc time
if ts.IsZero() {
t.Errorf("time should not be zero, got zero")
} else if ts.Before(started) {
t.Errorf("time %q is before start %q", ts, started)
}
} else {
t.Errorf("ts is a real %T", tsi)
}
} else {
t.Errorf("ts not found")
}
t.Logf("trying Slice()")
s, err := qr.Slice()
if err != nil {
t.Errorf("failed during slice: %v", err.Error())
}
if s[0].(string) != "Ferengi" {
t.Errorf("expected Ferengi, got %v", s[0])
}
if tsi := s[1]; tsi != nil {
if ts, ok := tsi.(time.Time); ok {
// time should not be zero because it defaults to current utc time
if ts.IsZero() {
t.Errorf("time should not be zero, got zero")
} else if ts.Before(started) {
t.Errorf("time %q is before start %q", ts, started)
}
} else {
t.Errorf("ts is a real %T", tsi)
}
} else {
t.Errorf("ts not found")
}
t.Logf("trying Scan(), also float64->int64 in Scan()")
var id int64
var name string
var ts time.Time
var wallet float64
var bankrupt bool
var payload []byte
err = qr.Scan(&id, &name)
if err == nil {
t.Errorf("expected an error to be returned, got nil")
}
err = qr.Scan(&name, &ts, &wallet, &bankrupt, &payload)
if err != nil {
t.Errorf("scanning: %v", err.Error())
}
if name != "Ferengi" {
t.Errorf("name should be 'Ferengi' but it's '%s'", name)
}
qr.Next()
if qr.RowNumber() != 1 {
t.Errorf("expected row number to be 1, got %v", qr.RowNumber())
}
err = qr.Scan(&name, &ts, &wallet, &bankrupt, &payload)
if err != nil {
t.Errorf("scanning: %s", err.Error())
}
if name != "Cardassian" {
t.Errorf("expected name to be 'Cardassian', got: %s", name)
}
if ts != meeting {
t.Errorf("expected ts to equal meeting, got ts: %v, meeting: %v", ts, meeting)
}
if qr.Next() == true {
t.Errorf("expected no more rows, got one")
}
})
t.Run("Invalid Query", func(t *testing.T) {
qr, err := globalConnection.QueryOne("INVALID QUERY")
if err == nil {
t.Errorf("expected an error to be returned, got nil")
}
if qr.Err == nil {
t.Errorf("expected an error to be returned, got nil")
}
})
t.Run("Map before next", func(t *testing.T) {
qr, err := globalConnection.QueryOne("SELECT name FROM " + testTableName() + " WHERE id = 3")
if err != nil {
t.Errorf("failed during query: %v - %v", err.Error(), qr.Err.Error())
}
_, err = qr.Map()
if err == nil {
t.Errorf("expected an error to be returned, got nil")
}
})
t.Run("Slice before next", func(t *testing.T) {
qr, err := globalConnection.QueryOne("SELECT name FROM " + testTableName() + " WHERE id = 3")
if err != nil {
t.Errorf("failed during query: %v - %v", err.Error(), qr.Err.Error())
}
_, err = qr.Slice()
if err == nil {
t.Errorf("expected an error to be returned, got nil")
}
})
t.Run("Scan before next", func(t *testing.T) {
qr, err := globalConnection.QueryOne("SELECT name FROM " + testTableName() + " WHERE id = 3")
if err != nil {
t.Errorf("failed during query: %v - %v", err.Error(), qr.Err.Error())
}
var name string
err = qr.Scan(&name)
if err == nil {
t.Errorf("expected an error to be returned, got nil")
}
})
}
func TestQueryOneContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
wResults, err := globalConnection.WriteOneContext(
ctx,
"CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT, wallet REAL, bankrupt INTEGER, payload BLOB, ts DATETIME)",
)
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wResults.Err.Error())
}
t.Cleanup(func() {
result, err := globalConnection.WriteOne("DROP TABLE " + testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), result.Err.Error())
}
})
now := time.Now().Round(time.Second)
wResults, err = globalConnection.WriteOneContext(
ctx,
fmt.Sprintf(
"INSERT INTO "+testTableName()+" (id, name, wallet, bankrupt, payload, ts) VALUES ( %d, %q, %v, %t, null, %q )",
11,
"Vulcan",
889.3332,
false,
now.Format(time.RFC3339),
),
)
if err != nil {
t.Errorf("failed during insert: %v - %v", err.Error(), wResults.Err.Error())
}
t.Logf("trying QueryOne")
qr, err := globalConnection.QueryOneContext(ctx, "SELECT id, name, wallet, bankrupt, ts FROM "+testTableName()+" WHERE id = 11")
if err != nil {
t.Errorf("failed during query: %v - %v", err.Error(), qr.Err.Error())
}
na := qr.Next()
if na != true {
t.Errorf("expected true, got %v", na)
}
var id int64
var name string
var ts time.Time
var wallet float64
var bankrupt bool
err = qr.Scan(&id, &name, &wallet, &bankrupt, &ts)
if err != nil {
t.Errorf("scanning: %v", err.Error())
}
if id != 11 {
t.Errorf("expected id to be 11, got %v", id)
}
if name != "Vulcan" {
t.Errorf("expected name to be 'Vulcan', got: %s", name)
}
if wallet != 889.3332 {
t.Errorf("expected wallet to be 889.3332, got: %v", wallet)
}
if bankrupt != false {
t.Errorf("expected bankrupt to be false, got: %v", bankrupt)
}
if !ts.Equal(now) {
t.Errorf("expected ts to equal now, got ts: %v, now: %v", ts, now)
}
}
func TestQueryOneParameterized(t *testing.T) {
var qr gorqlite.QueryResult
var err error
t.Logf("trying WriteOne DROP")
_, err = globalConnection.WriteOne("DROP TABLE IF EXISTS " + testTableName())
if err != nil {
t.Errorf("dropping table: %s", err.Error())
}
// give an extra second for time diff between local and rqlite
started := time.Now().Add(-time.Second)
t.Logf("trying WriteOne CREATE")
_, err = globalConnection.WriteOne("CREATE TABLE " + testTableName() + " (id integer, name text, ts DATETIME DEFAULT CURRENT_TIMESTAMP)")
if err != nil {
t.Errorf("craeting new table: %s", err.Error())
}
t.Cleanup(func() {
_, err = globalConnection.WriteOne("DROP TABLE " + testTableName())
if err != nil {
t.Errorf("dropping table: %s", err.Error())
}
})
// When the Federation met the Cardassians
meeting := time.Date(2424, 1, 2, 17, 0, 0, 0, time.UTC)
met := fmt.Sprint(meeting.Unix())
s := make([]string, 0)
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 1, 'Romulan' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 2, 'Vulcan' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 3, 'Klingon' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 4, 'Ferengi' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name, ts) VALUES ( 5, 'Cardassian',"+met+" )")
_, err = globalConnection.Write(s)
if err != nil {
t.Errorf("inserting bulk queries: %s", err.Error())
}
t.Run("QueryOneParameterized", func(t *testing.T) {
qr, err = globalConnection.QueryOneParameterized(
gorqlite.ParameterizedStatement{
Query: fmt.Sprintf("SELECT name, ts FROM %s WHERE id > ?", testTableName()),
Arguments: []interface{}{3},
},
)
if err != nil {
t.Errorf("executing query: %s", err.Error())
}
na := qr.Next()
if na != true {
t.Error("next should return true, got false")
}
r, err := qr.Map()
if err != nil {
t.Errorf("map: %s", err.Error())
}
if r["name"].(string) != "Ferengi" {
t.Errorf("expected 'Ferengi', got %s", r["name"].(string))
}
if tsi, ok := r["ts"]; ok {
if ts, ok := tsi.(time.Time); ok {
// time should not be zero because it defaults to current utc time
if ts.IsZero() {
t.Error("time is zero")
} else if ts.Before(started) {
t.Errorf("time %q is before start %q", ts, started)
}
} else {
t.Errorf("ts is a real %T", tsi)
}
} else {
t.Error("ts not found")
}
s, err := qr.Slice()
if err != nil {
t.Errorf("slice: %s", err.Error())
}
if s[0].(string) != "Ferengi" {
t.Errorf("expected 'Ferengi', got %s", s[0].(string))
}
if tsi := s[1]; tsi != nil {
if ts, ok := tsi.(time.Time); ok {
// time should not be zero because it defaults to current utc time
if ts.IsZero() {
t.Error("time is zero")
} else if ts.Before(started) {
t.Errorf("time %q is before start %q", ts, started)
}
} else {
t.Errorf("ts is a real %T", tsi)
}
} else {
t.Error("ts not found")
}
t.Logf("trying Scan(), also float64->int64 in Scan()")
var id int64
var name string
var ts time.Time
err = qr.Scan(&id, &name)
if err == nil {
t.Error("expected an error, got nil")
}
err = qr.Scan(&name, &ts)
if err != nil {
t.Errorf("scanning: %s", err.Error())
}
if name != "Ferengi" {
t.Errorf("name should be 'Ferengi' but it's '%s'", name)
}
qr.Next()
err = qr.Scan(&name, &ts)
if err != nil {
t.Errorf("scanning: %s", err.Error())
}
if name != "Cardassian" {
t.Errorf("name should be 'Cardassian' but it's '%s'", name)
}
if ts != meeting {
t.Errorf("time should be %q but it's %q", meeting, ts)
}
})
}
func TestScanNullableTypes(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "CREATE TABLE "+testTableName()+" (id integer, nullstring text, nullint64 integer, nullint32 integer, nullint16 integer, nullfloat64 real, nullbool integer, nulltime integer) strict")
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wr.Err.Error())
}
t.Cleanup(func() {
wr, err := globalConnection.WriteOne("DROP TABLE " + testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), wr.Err.Error())
}
})
var qr gorqlite.QueryResult
// When the Federation met the Cardassians
meeting := time.Date(2424, 1, 2, 17, 0, 0, 0, time.UTC)
met := fmt.Sprint(meeting.Unix())
t.Logf("trying Write INSERT")
s := make([]string, 0)
s = append(s, "INSERT INTO "+testTableName()+" (id) VALUES (1)") // other values are gonna be null
s = append(s, "INSERT INTO "+testTableName()+" (id, nullstring, nullint64, nullint32, nullint16, nullfloat64, nullbool, nulltime) VALUES (2, 'Romulan', 1, 2, 3, 4.5, 1, "+met+")")
_, err = globalConnection.Write(s)
if err != nil {
t.Errorf("insert queries: %s", err.Error())
}
t.Logf("trying QueryOne")
qr, err = globalConnection.QueryOne("SELECT id, nullstring, nullint64, nullint32, nullint16, nullfloat64, nullbool, nulltime FROM " + testTableName() + " WHERE id IN (1, 2)")
if err != nil {
t.Errorf("query one: %s", err.Error())
}
t.Logf("trying Next()")
na := qr.Next()
if na != true {
t.Error("expected next to be true, got false")
}
t.Logf("trying Scan()")
var id int64
var nullString gorqlite.NullString
var nullInt64 gorqlite.NullInt64
var nullInt32 gorqlite.NullInt32
var nullInt16 gorqlite.NullInt16
var nullFloat64 gorqlite.NullFloat64
var nullBool gorqlite.NullBool
var nullTime gorqlite.NullTime
err = qr.Scan(&id, &nullString, &nullInt64, &nullInt32, &nullInt16, &nullFloat64, &nullBool, &nullTime)
if err != nil {
t.Errorf("scanning: %s", err.Error())
}
if id != 1 {
t.Errorf("id should be 1 but it's %v", id)
}
if nullString.Valid || nullString.String != "" {
t.Errorf("nullString should be invalid and unset but it's '%v' and '%v'", nullString.Valid, nullString.String)
}
if nullInt64.Valid || nullInt64.Int64 != 0 {
t.Errorf("nullInt64 should be invalid and unset but it's '%v' and '%v'", nullInt64.Valid, nullInt64.Int64)
}
if nullInt32.Valid || nullInt32.Int32 != 0 {
t.Errorf("nullInt32 should be invalid and unset but it's '%v' and '%v'", nullInt32.Valid, nullInt32.Int32)
}
if nullInt16.Valid || nullInt16.Int16 != 0 {
t.Errorf("nullInt16 should be invalid and unset but it's '%v' and '%v'", nullInt16.Valid, nullInt16.Int16)
}
if nullFloat64.Valid || nullFloat64.Float64 != 0 {
t.Errorf("nullFloat64 should be invalid and unset but it's '%v' and '%v'", nullFloat64.Valid, nullFloat64.Float64)
}
if nullBool.Valid || nullBool.Bool != false {
t.Errorf("nullBool should be invalid and unset but it's '%v' and '%v'", nullBool.Valid, nullBool.Bool)
}
if nullTime.Valid || !nullTime.Time.IsZero() {
t.Errorf("nullTime should be invalid and unset but it's '%v' and '%v'", nullTime.Valid, nullTime.Time)
}
t.Logf("trying Next()")
qr.Next()
if na != true {
t.Error("expected next to be true, got false")
}
t.Logf("trying Scan()")
err = qr.Scan(&id, &nullString, &nullInt64, &nullInt32, &nullInt16, &nullFloat64, &nullBool, &nullTime)
if err != nil {
t.Errorf("scanning: %s", err.Error())
}
if id != 2 {
t.Errorf("id should be 2 but it's %v", id)
}
if !nullString.Valid || nullString.String != "Romulan" {
t.Errorf("nullString should be valid and set to 'Romulan' but it's '%v'", nullString.String)
}
if !nullInt64.Valid || nullInt64.Int64 != 1 {
t.Errorf("nullInt64 should be valid and set to 1 but it's '%v'", nullInt64.Int64)
}
if !nullInt32.Valid || nullInt32.Int32 != 2 {
t.Errorf("nullInt32 should be valid and set to 2 but it's '%v'", nullInt32.Int32)
}
if !nullInt16.Valid || nullInt16.Int16 != 3 {
t.Errorf("nullInt16 should be valid and set to 3 but it's '%v'", nullInt16.Int16)
}
if !nullFloat64.Valid || nullFloat64.Float64 != 4.5 {
t.Errorf("nullFloat64 should be valid and set to 4.5 but it's '%v'", nullFloat64.Float64)
}
if !nullBool.Valid || nullBool.Bool != true {
t.Errorf("nullBool should be valid and set to true but it's '%v'", nullBool.Bool)
}
if !nullTime.Valid || !nullTime.Time.Equal(meeting) {
t.Errorf("nullTime should be valid and set to '%v' but it's '%v'", meeting, nullTime.Time)
}
}