forked from patrickxb/fgosqlite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsqlite_test.go
338 lines (286 loc) · 9 KB
/
sqlite_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
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite_test
import (
"fmt"
"os"
"path"
"reflect"
"runtime"
"strings"
"testing"
"github.com/bmizerany/assert"
. "github.com/gwenn/gosqlite"
)
func checkNoError(t *testing.T, err error, format string) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
t.Fatalf("\n%s:%d: %s", path.Base(file), line, fmt.Sprintf(format, err))
}
}
func skipIfCgoCheckActive(t *testing.T) {
if /*runtime.Version() >= "go1.6" &&*/ !strings.Contains(os.Getenv("GODEBUG"), "cgocheck=0") {
t.Skip("cgocheck")
}
}
func Must(b bool, err error) bool {
if err != nil {
panic(err)
}
return b
}
func open(t *testing.T) *Conn {
db, err := Open(":memory:", OpenReadWrite, OpenCreate, OpenFullMutex /*OpenNoMutex*/)
checkNoError(t, err, "couldn't open database file: %s")
if db == nil {
t.Fatal("opened database is nil")
}
//db.SetLockingMode("", "exclusive")
//db.SetSynchronous("", 0)
//db.Profile(profile, t)
//db.Trace(trace, t)
if false /*testing.Verbose()*/ { // Go 1.1
db.SetAuthorizer(authorizer, t)
}
return db
}
func checkClose(db *Conn, t *testing.T) {
checkNoError(t, db.Close(), "Error closing database: %s")
}
func createTable(db *Conn, t *testing.T) {
err := db.Exec("DROP TABLE IF EXISTS test;" +
"CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL," +
" float_num REAL, int_num INTEGER, a_string TEXT); -- bim")
checkNoError(t, err, "error creating table: %s")
}
func TestVersion(t *testing.T) {
v := Version()
if !strings.HasPrefix(v, "3") {
t.Fatalf("unexpected library version: %s", v)
}
}
func TestOpen(t *testing.T) {
db := open(t)
checkNoError(t, db.Close(), "Error closing database: %s")
}
func TestOpenFailure(t *testing.T) {
db, err := Open("doesnotexist.sqlite", OpenReadOnly)
assert.T(t, db == nil && err != nil, "open failure expected")
//println(err.Error())
}
func TestCreateTable(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
}
func TestManualTransaction(t *testing.T) {
db := open(t)
defer checkClose(db, t)
checkNoError(t, db.Begin(), "Error while beginning transaction: %s")
if err := db.Begin(); err == nil {
t.Fatalf("Error expected (transaction cannot be nested)")
}
checkNoError(t, db.Commit(), "Error while committing transaction: %s")
checkNoError(t, db.BeginTransaction(Immediate), "Error while beginning immediate transaction: %s")
checkNoError(t, db.Commit(), "Error while committing transaction: %s")
checkNoError(t, db.BeginTransaction(Exclusive), "Error while beginning immediate transaction: %s")
checkNoError(t, db.Commit(), "Error while committing transaction: %s")
}
func TestSavepoint(t *testing.T) {
db := open(t)
defer checkClose(db, t)
checkNoError(t, db.Savepoint("1"), "Error while creating savepoint: %s")
checkNoError(t, db.Savepoint("2"), "Error while creating savepoint: %s")
checkNoError(t, db.RollbackSavepoint("2"), "Error while creating savepoint: %s")
checkNoError(t, db.ReleaseSavepoint("1"), "Error while creating savepoint: %s")
}
func TestExists(t *testing.T) {
db := open(t)
defer checkClose(db, t)
b, err := db.Exists("SELECT 1 WHERE 1 = 0")
checkNoError(t, err, "%s")
assert.T(t, !b, "no row expected")
b, err = db.Exists("SELECT 1 WHERE 1 = 1")
checkNoError(t, err, "%s")
assert.T(t, b, "one row expected")
_, err = db.Exists("SELECT 1", 1)
assert.T(t, err != nil)
//println(err.Error())
_, err = db.Exists("SELECT 1 FROM test")
assert.T(t, err != nil)
//println(err.Error())
_, err = db.Exists("PRAGMA shrink_memory")
assert.T(t, err != nil)
//println(err.Error())
}
func TestInsert(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
db.Begin()
for i := 0; i < 1000; i++ {
ierr := db.Exec("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)", float64(i)*3.14, i, "hello")
checkNoError(t, ierr, "insert error: %s")
c := db.Changes()
assert.Equal(t, 1, c, "changes")
}
checkNoError(t, db.Commit(), "Error: %s")
lastID := db.LastInsertRowid()
assert.Equal(t, int64(1000), lastID, "last insert row id")
cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer checkFinalize(cs, t)
paramCount := cs.BindParameterCount()
assert.Equal(t, 0, paramCount, "bind parameter count")
columnCount := cs.ColumnCount()
assert.Equal(t, 1, columnCount, "column count")
assert.T(t, checkStep(t, cs))
assert.Equal(t, columnCount, cs.DataCount(), "column & data count expected to be equal")
var i int
checkNoError(t, cs.Scan(&i), "error scanning count: %s")
assert.Equal(t, 1000, i, "count")
if checkStep(t, cs) {
t.Fatal("Only one row expected")
}
assert.T(t, !cs.Busy(), "expected statement to be reset")
}
/*
func TestLoadExtension(t *testing.T) { // OMIT_LOAD_EXTENSION
db := open(t)
db.EnableLoadExtension(true)
err := db.LoadExtension("/tmp/myext.so")
checkNoError(t, err, "load extension error: %s")
}
*/
func TestOpenSameMemoryDb(t *testing.T) {
db1, err := Open("file:dummy.db?mode=memory&cache=shared", OpenURI, OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "open error: %s")
defer checkClose(db1, t)
err = db1.Exec("CREATE TABLE test (data TEXT)")
checkNoError(t, err, "exec error: %s")
db2, err := Open("file:dummy.db?mode=memory&cache=shared", OpenURI, OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "open error: %s")
defer checkClose(db2, t)
_, err = db2.Exists("SELECT 1 FROM test")
checkNoError(t, err, "exists error: %s")
}
func TestConnExecWithSelect(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("SELECT 1")
assert.T(t, err != nil, "error expected")
if serr, ok := err.(StmtError); ok {
assert.Equal(t, ErrSpecific, serr.Code())
} else {
t.Errorf("got %s; want StmtError", reflect.TypeOf(err))
}
}
func TestConnInitialState(t *testing.T) {
db := open(t)
defer checkClose(db, t)
autoCommit := db.GetAutocommit()
assert.T(t, autoCommit, "autocommit expected to be active by default")
totalChanges := db.TotalChanges()
assert.Equal(t, 0, totalChanges, "total changes")
err := db.LastError()
assert.Equal(t, nil, err, "expected last error to be nil")
readonly, err := db.Readonly("main")
checkNoError(t, err, "Readonly status error: %s")
assert.T(t, !readonly, "readonly expected to be unset by default")
}
func TestReadonlyMisuse(t *testing.T) {
db := open(t)
defer checkClose(db, t)
_, err := db.Readonly("doesnotexist")
assert.T(t, err != nil, "error expected")
_ = err.Error()
//println(err.Error())
}
func TestComplete(t *testing.T) {
c, err := Complete("SELECT 1;")
checkNoError(t, err, "error: %s")
assert.T(t, c, "expected complete statement")
}
func TestExecMisuse(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.Exec("INSERT INTO test VALUES (?, ?, ?, ?); INSERT INTO test VALUES (?, ?, ?, ?)", 0, 273.1, 1, "test")
assert.T(t, err != nil, "exec misuse expected")
//println(err.Error())
err = db.Exec("CREATE FUNCTION incr(i INT) RETURN i+1;")
assert.T(t, err != nil, "syntax error expected")
//println(err.Error())
}
func TestExecTwice(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.Exec("INSERT INTO test VALUES (?, ?, ?, ?); INSERT INTO test VALUES (?, ?, ?, ?)",
0, 273.1, 1, "test",
1, 2257, 2, nil)
checkNoError(t, err, "Exec error: %s")
}
func TestTransaction(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.Transaction(Immediate, func(_ *Conn) error {
err := db.Transaction(Immediate, func(__ *Conn) error {
return db.Exec("INSERT INTO test VALUES (?, ?, ?, ?)", 0, 273.1, 1, "test")
})
checkNoError(t, err, "error: %s")
return err
})
checkNoError(t, err, "error: %s")
}
func TestCommitMisuse(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Commit()
assert.T(t, err != nil, "error expected")
if cerr, ok := err.(ConnError); ok {
assert.Equal(t, ErrError, cerr.Code())
assert.Equal(t, 1, cerr.ExtendedCode())
} else {
t.Errorf("got %s; want ConnError", reflect.TypeOf(err))
}
assert.Equal(t, err, db.LastError())
}
func TestNilDb(t *testing.T) {
var db *Conn
err := db.Exec("DROP TABLE IF EXISTS test")
assert.T(t, err != nil)
//println(err.Error())
err = db.Close()
assert.T(t, err != nil)
//println(err.Error())
err = db.LastError()
assert.T(t, err != nil)
//println(err.Error())
_, err = db.ForeignKeyCheck("", "")
assert.T(t, err != nil)
_, err = db.Databases()
assert.T(t, err != nil)
}
func TestError(t *testing.T) {
err := ErrMisuse
assert.T(t, err.Error() != "")
}
func TestOneValueMisuse(t *testing.T) {
db := open(t)
defer checkClose(db, t)
var value interface{}
err := db.OneValue("SELECT 1", &value, 1)
assert.T(t, err != nil)
//println(err.Error())
err = db.OneValue("SELECT 1 FROM test", &value)
assert.T(t, err != nil)
//println(err.Error())
err = db.OneValue("PRAGMA shrink_memory", &value)
assert.T(t, err != nil)
//println(err.Error())
err = db.OneValue("SELECT 1 LIMIT 0", &value)
assert.T(t, err != nil)
//println(err.Error())
}