forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3_test.go
448 lines (383 loc) · 9.38 KB
/
sqlite3_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
package sqlite
import (
"database/sql"
"os"
"strings"
"testing"
"time"
)
func TestOpen(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to open database:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("drop table foo")
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
if stat, err := os.Stat("./foo.db"); err != nil || stat.IsDir() {
t.Errorf("Failed to create ./foo.db")
}
}
func TestInsert(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to open database:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("drop table foo")
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
res, err := db.Exec("insert into foo(id) values(123)")
if err != nil {
t.Errorf("Failed to insert record:", err)
return
}
affected, _ := res.RowsAffected()
if affected != 1 {
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
return
}
rows, err := db.Query("select id from foo")
if err != nil {
t.Errorf("Failed to select records:", err)
return
}
defer rows.Close()
rows.Next()
var result int
rows.Scan(&result)
if result != 123 {
t.Errorf("Fetched %q; expected %q", 123, result)
}
}
func TestUpdate(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to open database:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("drop table foo")
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
res, err := db.Exec("insert into foo(id) values(123)")
if err != nil {
t.Errorf("Failed to insert record:", err)
return
}
expected, err := res.LastInsertId()
if err != nil {
t.Errorf("Failed to get LastInsertId:", err)
return
}
affected, _ := res.RowsAffected()
if err != nil {
t.Errorf("Failed to get RowsAffected:", err)
return
}
if affected != 1 {
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
return
}
res, err = db.Exec("update foo set id = 234")
if err != nil {
t.Errorf("Failed to update record:", err)
return
}
lastId, err := res.LastInsertId()
if err != nil {
t.Errorf("Failed to get LastInsertId:", err)
return
}
if expected != lastId {
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
}
affected, _ = res.RowsAffected()
if err != nil {
t.Errorf("Failed to get RowsAffected:", err)
return
}
if affected != 1 {
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
return
}
rows, err := db.Query("select id from foo")
if err != nil {
t.Errorf("Failed to select records:", err)
return
}
defer rows.Close()
rows.Next()
var result int
rows.Scan(&result)
if result != 234 {
t.Errorf("Fetched %q; expected %q", 234, result)
}
}
func TestDelete(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to select records:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("drop table foo")
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
res, err := db.Exec("insert into foo(id) values(123)")
if err != nil {
t.Errorf("Failed to insert record:", err)
return
}
expected, err := res.LastInsertId()
if err != nil {
t.Errorf("Failed to get LastInsertId:", err)
return
}
affected, err := res.RowsAffected()
if err != nil {
t.Errorf("Failed to get RowsAffected:", err)
return
}
if affected != 1 {
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
}
res, err = db.Exec("delete from foo where id = 123")
if err != nil {
t.Errorf("Failed to delete record:", err)
return
}
lastId, err := res.LastInsertId()
if err != nil {
t.Errorf("Failed to get LastInsertId:", err)
return
}
if expected != lastId {
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
}
affected, err = res.RowsAffected()
if err != nil {
t.Errorf("Failed to get RowsAffected:", err)
return
}
if affected != 1 {
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
}
rows, err := db.Query("select id from foo")
if err != nil {
t.Errorf("Failed to select records:", err)
return
}
defer rows.Close()
if rows.Next() {
t.Errorf("Fetched row but expected not rows")
}
}
func TestBooleanRoundtrip(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to open database:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("DROP TABLE foo")
_, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true)
if err != nil {
t.Errorf("Failed to insert true value:", err)
return
}
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false)
if err != nil {
t.Errorf("Failed to insert false value:", err)
return
}
rows, err := db.Query("SELECT id, value FROM foo")
if err != nil {
t.Errorf("Unable to query foo table:", err)
return
}
for rows.Next() {
var id int
var value bool
if err := rows.Scan(&id, &value); err != nil {
t.Errorf("Unable to scan results:", err)
continue
}
if id == 1 && !value {
t.Errorf("Value for id 1 should be true, not false")
} else if id == 2 && value {
t.Errorf("Value for id 2 should be false, not true")
}
}
}
func TestTimestamp(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to open database:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("DROP TABLE foo")
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC)
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(1, ?)", timestamp1)
if err != nil {
t.Errorf("Failed to insert timestamp:", err)
return
}
timestamp2 := time.Date(2012, time.April, 6, 23, 22, 0, 0, time.UTC)
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(2, ?)", timestamp2.Unix())
if err != nil {
t.Errorf("Failed to insert timestamp:", err)
return
}
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(3, ?)", "nonsense")
if err != nil {
t.Errorf("Failed to insert nonsense:", err)
return
}
rows, err := db.Query("SELECT id, ts FROM foo ORDER BY id ASC")
if err != nil {
t.Errorf("Unable to query foo table:", err)
return
}
seen := 0
for rows.Next() {
var id int
var ts time.Time
if err := rows.Scan(&id, &ts); err != nil {
t.Errorf("Unable to scan results:", err)
continue
}
if id == 1 {
seen += 1
if !timestamp1.Equal(ts) {
t.Errorf("Value for id 1 should be %v, not %v", timestamp1, ts)
}
}
if id == 2 {
seen += 1
if !timestamp2.Equal(ts) {
t.Errorf("Value for id 2 should be %v, not %v", timestamp2, ts)
}
}
}
if seen != 2 {
t.Errorf("Expected to see two valid timestamps")
}
// make sure "nonsense" triggered an error
err = rows.Err()
if err == nil || !strings.Contains(err.Error(), "cannot parse \"nonsense\"") {
t.Errorf("Expected error from \"nonsense\" timestamp")
}
}
func TestBoolean(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Errorf("Failed to open database:", err)
return
}
defer os.Remove("./foo.db")
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")
if err != nil {
t.Errorf("Failed to create table:", err)
return
}
bool1 := true
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(1, ?)", bool1)
if err != nil {
t.Errorf("Failed to insert boolean:", err)
return
}
bool2 := false
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2)
if err != nil {
t.Errorf("Failed to insert boolean:", err)
return
}
bool3 := "nonsense"
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3)
if err != nil {
t.Errorf("Failed to insert nonsense:", err)
return
}
rows, err := db.Query("SELECT id, fbool FROM foo where fbool is ?", bool1)
if err != nil {
t.Errorf("Unable to query foo table:", err)
return
}
counter := 0
var id int
var fbool bool
for rows.Next(){
if err := rows.Scan(&id, &fbool); err != nil {
t.Errorf("Unable to scan results:", err)
return
}
counter ++
}
if counter != 1{
t.Errorf("Expected 1 row but %v", counter)
return
}
if id!=1 && fbool != true {
t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool)
return
}
rows, err = db.Query("SELECT id, fbool FROM foo where fbool is ?", bool2)
if err != nil {
t.Errorf("Unable to query foo table:", err)
return
}
counter = 0
for rows.Next(){
if err := rows.Scan(&id, &fbool); err != nil {
t.Errorf("Unable to scan results:", err)
return
}
counter ++
}
if counter != 1{
t.Errorf("Expected 1 row but %v", counter)
return
}
if id != 2 && fbool != false {
t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool)
return
}
// make sure "nonsense" triggered an error
rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3)
if err != nil {
t.Errorf("Unable to query foo table:", err)
return
}
rows.Next()
err = rows.Scan(&id, &fbool)
if err == nil {
t.Errorf("Expected error from \"nonsense\" bool")
}
}