Commit b0f97339 authored by gwenn's avatar gwenn

Checks no error is returned by deferred Close().

parent eb98c92c
...@@ -11,9 +11,9 @@ import ( ...@@ -11,9 +11,9 @@ import (
func TestBackup(t *testing.T) { func TestBackup(t *testing.T) {
dst := open(t) dst := open(t)
defer dst.Close() defer checkClose(dst, t)
src := open(t) src := open(t)
defer src.Close() defer checkClose(src, t)
fill(src, 1000) fill(src, 1000)
bck, err := NewBackup(dst, "main", src, "main") bck, err := NewBackup(dst, "main", src, "main")
...@@ -35,7 +35,7 @@ func TestBackup(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestBackup(t *testing.T) {
func TestBackupMisuse(t *testing.T) { func TestBackupMisuse(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
bck, err := NewBackup(db, "", db, "") bck, err := NewBackup(db, "", db, "")
assert(t, "source and destination must be distinct", bck == nil && err != nil) assert(t, "source and destination must be distinct", bck == nil && err != nil)
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
func TestBlob(t *testing.T) { func TestBlob(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (content BLOB);") err := db.Exec("CREATE TABLE test (content BLOB);")
checkNoError(t, err, "error creating table: %s") checkNoError(t, err, "error creating table: %s")
...@@ -21,7 +21,7 @@ func TestBlob(t *testing.T) { ...@@ -21,7 +21,7 @@ func TestBlob(t *testing.T) {
if s == nil { if s == nil {
t.Fatal("statement is nil") t.Fatal("statement is nil")
} }
defer s.Finalize() defer checkFinalize(s, t)
err = s.Exec(ZeroBlobLength(10)) err = s.Exec(ZeroBlobLength(10))
checkNoError(t, err, "insert error: %s") checkNoError(t, err, "insert error: %s")
rowid := db.LastInsertRowid() rowid := db.LastInsertRowid()
...@@ -57,7 +57,7 @@ func TestBlob(t *testing.T) { ...@@ -57,7 +57,7 @@ func TestBlob(t *testing.T) {
func TestBlobMisuse(t *testing.T) { func TestBlobMisuse(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
bw, err := db.NewBlobReadWriter("main", "test", "content", 0) bw, err := db.NewBlobReadWriter("main", "test", "content", 0)
assert(t, "error expected", bw == nil && err != nil) assert(t, "error expected", bw == nil && err != nil)
......
...@@ -14,14 +14,14 @@ import ( ...@@ -14,14 +14,14 @@ import (
func TestInterrupt(t *testing.T) { func TestInterrupt(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
db.CreateScalarFunction("interrupt", 0, nil, func(ctx *ScalarContext, nArg int) { db.CreateScalarFunction("interrupt", 0, nil, func(ctx *ScalarContext, nArg int) {
db.Interrupt() db.Interrupt()
ctx.ResultText("ok") ctx.ResultText("ok")
}, nil) }, nil)
s, err := db.Prepare("SELECT interrupt() FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3)") s, err := db.Prepare("SELECT interrupt() FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3)")
checkNoError(t, err, "couldn't prepare stmt: %#v") checkNoError(t, err, "couldn't prepare stmt: %#v")
defer s.Finalize() defer checkFinalize(s, t)
err = s.Select(func(s *Stmt) (err error) { err = s.Select(func(s *Stmt) (err error) {
return return
}) })
...@@ -46,8 +46,8 @@ func openTwoConnSameDb(t *testing.T) (*os.File, *Conn, *Conn) { ...@@ -46,8 +46,8 @@ func openTwoConnSameDb(t *testing.T) (*os.File, *Conn, *Conn) {
func TestDefaultBusy(t *testing.T) { func TestDefaultBusy(t *testing.T) {
f, db1, db2 := openTwoConnSameDb(t) f, db1, db2 := openTwoConnSameDb(t)
defer os.Remove(f.Name()) defer os.Remove(f.Name())
defer db1.Close() defer checkClose(db1, t)
defer db2.Close() defer checkClose(db2, t)
checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s") checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s")
defer db1.Rollback() defer db1.Rollback()
...@@ -63,8 +63,8 @@ func TestDefaultBusy(t *testing.T) { ...@@ -63,8 +63,8 @@ func TestDefaultBusy(t *testing.T) {
func TestBusyTimeout(t *testing.T) { func TestBusyTimeout(t *testing.T) {
f, db1, db2 := openTwoConnSameDb(t) f, db1, db2 := openTwoConnSameDb(t)
defer os.Remove(f.Name()) defer os.Remove(f.Name())
defer db1.Close() defer checkClose(db1, t)
defer db2.Close() defer checkClose(db2, t)
checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s") checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s")
//join := make(chan bool) //join := make(chan bool)
...@@ -83,8 +83,8 @@ func TestBusyTimeout(t *testing.T) { ...@@ -83,8 +83,8 @@ func TestBusyTimeout(t *testing.T) {
func TestBusyHandler(t *testing.T) { func TestBusyHandler(t *testing.T) {
f, db1, db2 := openTwoConnSameDb(t) f, db1, db2 := openTwoConnSameDb(t)
defer os.Remove(f.Name()) defer os.Remove(f.Name())
defer db1.Close() defer checkClose(db1, t)
defer db2.Close() defer checkClose(db2, t)
//c := make(chan bool) //c := make(chan bool)
var called bool var called bool
......
...@@ -17,7 +17,7 @@ func checkCacheSize(t *testing.T, db *Conn, expectedSize, expectedMaxSize int) { ...@@ -17,7 +17,7 @@ func checkCacheSize(t *testing.T, db *Conn, expectedSize, expectedMaxSize int) {
func TestDisabledCache(t *testing.T) { func TestDisabledCache(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
db.SetCacheSize(0) db.SetCacheSize(0)
checkCacheSize(t, db, 0, 0) checkCacheSize(t, db, 0, 0)
...@@ -35,7 +35,7 @@ func TestDisabledCache(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestDisabledCache(t *testing.T) {
func TestEnabledCache(t *testing.T) { func TestEnabledCache(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
db.SetCacheSize(10) db.SetCacheSize(10)
checkCacheSize(t, db, 0, 10) checkCacheSize(t, db, 0, 10)
......
...@@ -24,7 +24,7 @@ func TestJulianDay(t *testing.T) { ...@@ -24,7 +24,7 @@ func TestJulianDay(t *testing.T) {
func TestBind(t *testing.T) { func TestBind(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
var delta int var delta int
err := db.OneValue("SELECT CAST(strftime('%s', 'now') AS NUMERIC) - ?", &delta, time.Now()) err := db.OneValue("SELECT CAST(strftime('%s', 'now') AS NUMERIC) - ?", &delta, time.Now())
checkNoError(t, err, "Error reading date: %#v") checkNoError(t, err, "Error reading date: %#v")
...@@ -35,7 +35,7 @@ func TestBind(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestBind(t *testing.T) {
func TestScan(t *testing.T) { func TestScan(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
var dt time.Time var dt time.Time
err := db.OneValue("SELECT date('now')", &dt) err := db.OneValue("SELECT date('now')", &dt)
checkNoError(t, err, "Error reading date: %#v") checkNoError(t, err, "Error reading date: %#v")
......
...@@ -27,6 +27,18 @@ func sqlOpen(t *testing.T) *sql.DB { ...@@ -27,6 +27,18 @@ func sqlOpen(t *testing.T) *sql.DB {
return db return db
} }
func checkSqlDbClose(db *sql.DB, t *testing.T) {
checkNoError(t, db.Close(), "Error closing connection: %s")
}
func checkSqlStmtClose(stmt *sql.Stmt, t *testing.T) {
checkNoError(t, stmt.Close(), "Error closing statement: %s")
}
func checkSqlRowsClose(rows *sql.Rows, t *testing.T) {
checkNoError(t, rows.Close(), "Error closing rows: %s")
}
func sqlCreate(ddl string, t *testing.T) *sql.DB { func sqlCreate(ddl string, t *testing.T) *sql.DB {
db := sqlOpen(t) db := sqlOpen(t)
_, err := db.Exec(ddl) _, err := db.Exec(ddl)
...@@ -41,7 +53,7 @@ func TestSqlOpen(t *testing.T) { ...@@ -41,7 +53,7 @@ func TestSqlOpen(t *testing.T) {
func TestSqlDdl(t *testing.T) { func TestSqlDdl(t *testing.T) {
db := sqlOpen(t) db := sqlOpen(t)
defer db.Close() defer checkSqlDbClose(db, t)
result, err := db.Exec(ddl) result, err := db.Exec(ddl)
checkNoError(t, err, "Error creating table: %s") checkNoError(t, err, "Error creating table: %s")
_, err = result.LastInsertId() // FIXME Error expected _, err = result.LastInsertId() // FIXME Error expected
...@@ -56,7 +68,7 @@ func TestSqlDdl(t *testing.T) { ...@@ -56,7 +68,7 @@ func TestSqlDdl(t *testing.T) {
func TestSqlDml(t *testing.T) { func TestSqlDml(t *testing.T) {
db := sqlCreate(ddl, t) db := sqlCreate(ddl, t)
defer db.Close() defer checkSqlDbClose(db, t)
result, err := db.Exec(dml) result, err := db.Exec(dml)
checkNoError(t, err, "Error updating data: %s") checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId() id, err := result.LastInsertId()
...@@ -69,7 +81,7 @@ func TestSqlDml(t *testing.T) { ...@@ -69,7 +81,7 @@ func TestSqlDml(t *testing.T) {
func TestSqlInsert(t *testing.T) { func TestSqlInsert(t *testing.T) {
db := sqlCreate(ddl, t) db := sqlCreate(ddl, t)
defer db.Close() defer checkSqlDbClose(db, t)
result, err := db.Exec(insert, "Bart") result, err := db.Exec(insert, "Bart")
checkNoError(t, err, "Error updating data: %s") checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId() id, err := result.LastInsertId()
...@@ -82,7 +94,7 @@ func TestSqlInsert(t *testing.T) { ...@@ -82,7 +94,7 @@ func TestSqlInsert(t *testing.T) {
func TestSqlExecWithIllegalCmd(t *testing.T) { func TestSqlExecWithIllegalCmd(t *testing.T) {
db := sqlCreate(ddl+dml, t) db := sqlCreate(ddl+dml, t)
defer db.Close() defer checkSqlDbClose(db, t)
_, err := db.Exec(query, "%") _, err := db.Exec(query, "%")
if err == nil { if err == nil {
...@@ -92,10 +104,10 @@ func TestSqlExecWithIllegalCmd(t *testing.T) { ...@@ -92,10 +104,10 @@ func TestSqlExecWithIllegalCmd(t *testing.T) {
func TestSqlQuery(t *testing.T) { func TestSqlQuery(t *testing.T) {
db := sqlCreate(ddl+dml, t) db := sqlCreate(ddl+dml, t)
defer db.Close() defer checkSqlDbClose(db, t)
rows, err := db.Query(query, "%") rows, err := db.Query(query, "%")
defer rows.Close() defer checkSqlRowsClose(rows, t)
var id int var id int
var name string var name string
for rows.Next() { for rows.Next() {
...@@ -106,7 +118,7 @@ func TestSqlQuery(t *testing.T) { ...@@ -106,7 +118,7 @@ func TestSqlQuery(t *testing.T) {
func TestSqlTx(t *testing.T) { func TestSqlTx(t *testing.T) {
db := sqlCreate(ddl, t) db := sqlCreate(ddl, t)
defer db.Close() defer checkSqlDbClose(db, t)
tx, err := db.Begin() tx, err := db.Begin()
checkNoError(t, err, "Error while begining tx: %s") checkNoError(t, err, "Error while begining tx: %s")
...@@ -116,26 +128,26 @@ func TestSqlTx(t *testing.T) { ...@@ -116,26 +128,26 @@ func TestSqlTx(t *testing.T) {
func TestSqlPrepare(t *testing.T) { func TestSqlPrepare(t *testing.T) {
db := sqlCreate(ddl+dml, t) db := sqlCreate(ddl+dml, t)
defer db.Close() defer checkSqlDbClose(db, t)
stmt, err := db.Prepare(insert) stmt, err := db.Prepare(insert)
checkNoError(t, err, "Error while preparing stmt: %s") checkNoError(t, err, "Error while preparing stmt: %s")
defer stmt.Close() defer checkSqlStmtClose(stmt, t)
_, err = stmt.Exec("Bart") _, err = stmt.Exec("Bart")
checkNoError(t, err, "Error while executing stmt: %s") checkNoError(t, err, "Error while executing stmt: %s")
} }
func TestRowsWithStmtClosed(t *testing.T) { func TestRowsWithStmtClosed(t *testing.T) {
db := sqlCreate(ddl+dml, t) db := sqlCreate(ddl+dml, t)
defer db.Close() defer checkSqlDbClose(db, t)
stmt, err := db.Prepare(query) stmt, err := db.Prepare(query)
checkNoError(t, err, "Error while preparing stmt: %s") checkNoError(t, err, "Error while preparing stmt: %s")
//defer stmt.Close() //defer stmt.Close()
rows, err := stmt.Query("%") rows, err := stmt.Query("%")
stmt.Close() checkSqlStmtClose(stmt, t)
defer rows.Close() defer checkSqlRowsClose(rows, t)
var id int var id int
var name string var name string
for rows.Next() { for rows.Next() {
......
...@@ -23,7 +23,7 @@ func half(ctx *ScalarContext, nArg int) { ...@@ -23,7 +23,7 @@ func half(ctx *ScalarContext, nArg int) {
func TestScalarFunction(t *testing.T) { func TestScalarFunction(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.CreateScalarFunction("half", 1, nil, half, nil) err := db.CreateScalarFunction("half", 1, nil, half, nil)
checkNoError(t, err, "couldn't create function: %s") checkNoError(t, err, "couldn't create function: %s")
var d float64 var d float64
...@@ -70,12 +70,12 @@ func reDestroy(ad interface{}) { ...@@ -70,12 +70,12 @@ func reDestroy(ad interface{}) {
func TestRegexpFunction(t *testing.T) { func TestRegexpFunction(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.CreateScalarFunction("regexp", 2, nil, re, reDestroy) err := db.CreateScalarFunction("regexp", 2, nil, re, reDestroy)
checkNoError(t, err, "couldn't create function: %s") checkNoError(t, err, "couldn't create function: %s")
s, err := db.Prepare("select regexp('l.s[aeiouy]', name) from (select 'lisa' as name union all select 'bart')") s, err := db.Prepare("select regexp('l.s[aeiouy]', name) from (select 'lisa' as name union all select 'bart')")
checkNoError(t, err, "couldn't prepare statement: %s") checkNoError(t, err, "couldn't prepare statement: %s")
defer s.Finalize() defer checkFinalize(s, t)
if b := Must(s.Next()); !b { if b := Must(s.Next()); !b {
t.Fatalf("No result") t.Fatalf("No result")
...@@ -104,7 +104,7 @@ func user(ctx *ScalarContext, nArg int) { ...@@ -104,7 +104,7 @@ func user(ctx *ScalarContext, nArg int) {
func TestUserFunction(t *testing.T) { func TestUserFunction(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.CreateScalarFunction("user", 0, nil, user, nil) err := db.CreateScalarFunction("user", 0, nil, user, nil)
checkNoError(t, err, "couldn't create function: %s") checkNoError(t, err, "couldn't create function: %s")
var name string var name string
...@@ -138,7 +138,7 @@ func sumFinal(ctx *AggregateContext) { ...@@ -138,7 +138,7 @@ func sumFinal(ctx *AggregateContext) {
func TestSumFunction(t *testing.T) { func TestSumFunction(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.CreateAggregateFunction("mysum", 1, nil, sumStep, sumFinal, nil) err := db.CreateAggregateFunction("mysum", 1, nil, sumStep, sumFinal, nil)
checkNoError(t, err, "couldn't create function: %s") checkNoError(t, err, "couldn't create function: %s")
var i int var i int
......
...@@ -17,7 +17,7 @@ func createIndex(db *Conn, t *testing.T) { ...@@ -17,7 +17,7 @@ func createIndex(db *Conn, t *testing.T) {
func TestDatabases(t *testing.T) { func TestDatabases(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
databases, err := db.Databases() databases, err := db.Databases()
checkNoError(t, err, "error looking for databases: %s") checkNoError(t, err, "error looking for databases: %s")
...@@ -31,7 +31,7 @@ func TestDatabases(t *testing.T) { ...@@ -31,7 +31,7 @@ func TestDatabases(t *testing.T) {
func TestTables(t *testing.T) { func TestTables(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
tables, err := db.Tables("") tables, err := db.Tables("")
checkNoError(t, err, "error looking for tables: %s") checkNoError(t, err, "error looking for tables: %s")
...@@ -45,7 +45,7 @@ func TestTables(t *testing.T) { ...@@ -45,7 +45,7 @@ func TestTables(t *testing.T) {
func TestColumns(t *testing.T) { func TestColumns(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
createTable(db, t) createTable(db, t)
columns, err := db.Columns("", "test") columns, err := db.Columns("", "test")
...@@ -59,7 +59,7 @@ func TestColumns(t *testing.T) { ...@@ -59,7 +59,7 @@ func TestColumns(t *testing.T) {
func TestColumn(t *testing.T) { func TestColumn(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
createTable(db, t) createTable(db, t)
column, err := db.Column("", "test", "id") column, err := db.Column("", "test", "id")
...@@ -71,7 +71,7 @@ func TestColumn(t *testing.T) { ...@@ -71,7 +71,7 @@ func TestColumn(t *testing.T) {
func TestForeignKeys(t *testing.T) { func TestForeignKeys(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.Exec("CREATE TABLE parent (id INTEGER PRIMARY KEY NOT NULL);" + err := db.Exec("CREATE TABLE parent (id INTEGER PRIMARY KEY NOT NULL);" +
"CREATE TABLE child (id INTEGER PRIMARY KEY NOT NULL, parentId INTEGER, " + "CREATE TABLE child (id INTEGER PRIMARY KEY NOT NULL, parentId INTEGER, " +
...@@ -90,7 +90,7 @@ func TestForeignKeys(t *testing.T) { ...@@ -90,7 +90,7 @@ func TestForeignKeys(t *testing.T) {
func TestIndexes(t *testing.T) { func TestIndexes(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
createTable(db, t) createTable(db, t)
createIndex(db, t) createIndex(db, t)
...@@ -114,10 +114,10 @@ func TestIndexes(t *testing.T) { ...@@ -114,10 +114,10 @@ func TestIndexes(t *testing.T) {
func TestColumnMetadata(t *testing.T) { func TestColumnMetadata(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("SELECT name AS table_name FROM sqlite_master") s, err := db.Prepare("SELECT name AS table_name FROM sqlite_master")
check(err) check(err)
defer s.Finalize() defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0) databaseName := s.ColumnDatabaseName(0)
assertEquals(t, "wrong database name: %q <> %q", "main", databaseName) assertEquals(t, "wrong database name: %q <> %q", "main", databaseName)
......
...@@ -10,13 +10,13 @@ import ( ...@@ -10,13 +10,13 @@ import (
func TestIntegrityCheck(t *testing.T) { func TestIntegrityCheck(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
checkNoError(t, db.IntegrityCheck("", 1, true), "Error checking integrity of database: %s") checkNoError(t, db.IntegrityCheck("", 1, true), "Error checking integrity of database: %s")
} }
func TestEncoding(t *testing.T) { func TestEncoding(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
encoding, err := db.Encoding("") encoding, err := db.Encoding("")
checkNoError(t, err, "Error reading encoding of database: %s") checkNoError(t, err, "Error reading encoding of database: %s")
assertEquals(t, "Expecting %s but got %s", "UTF-8", encoding) assertEquals(t, "Expecting %s but got %s", "UTF-8", encoding)
...@@ -24,7 +24,7 @@ func TestEncoding(t *testing.T) { ...@@ -24,7 +24,7 @@ func TestEncoding(t *testing.T) {
func TestSchemaVersion(t *testing.T) { func TestSchemaVersion(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
version, err := db.SchemaVersion("") version, err := db.SchemaVersion("")
checkNoError(t, err, "Error reading schema version of database: %s") checkNoError(t, err, "Error reading schema version of database: %s")
assertEquals(t, "expecting %d but got %d", 0, version) assertEquals(t, "expecting %d but got %d", 0, version)
...@@ -32,7 +32,7 @@ func TestSchemaVersion(t *testing.T) { ...@@ -32,7 +32,7 @@ func TestSchemaVersion(t *testing.T) {
func TestJournalMode(t *testing.T) { func TestJournalMode(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
mode, err := db.JournalMode("") mode, err := db.JournalMode("")
checkNoError(t, err, "Error reading journaling mode of database: %s") checkNoError(t, err, "Error reading journaling mode of database: %s")
assertEquals(t, "expecting %s but got %s", "memory", mode) assertEquals(t, "expecting %s but got %s", "memory", mode)
...@@ -40,7 +40,7 @@ func TestJournalMode(t *testing.T) { ...@@ -40,7 +40,7 @@ func TestJournalMode(t *testing.T) {
func TestSetJournalMode(t *testing.T) { func TestSetJournalMode(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
mode, err := db.SetJournalMode("", "OFF") mode, err := db.SetJournalMode("", "OFF")
checkNoError(t, err, "Error setting journaling mode of database: %s") checkNoError(t, err, "Error setting journaling mode of database: %s")
assertEquals(t, "expecting %s but got %s", "off", mode) assertEquals(t, "expecting %s but got %s", "off", mode)
...@@ -48,7 +48,7 @@ func TestSetJournalMode(t *testing.T) { ...@@ -48,7 +48,7 @@ func TestSetJournalMode(t *testing.T) {
func TestLockingMode(t *testing.T) { func TestLockingMode(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
mode, err := db.LockingMode("") mode, err := db.LockingMode("")
checkNoError(t, err, "Error reading locking-mode of database: %s") checkNoError(t, err, "Error reading locking-mode of database: %s")
assertEquals(t, "expecting %s but got %s", "normal", mode) assertEquals(t, "expecting %s but got %s", "normal", mode)
...@@ -56,7 +56,7 @@ func TestLockingMode(t *testing.T) { ...@@ -56,7 +56,7 @@ func TestLockingMode(t *testing.T) {
func TestSetLockingMode(t *testing.T) { func TestSetLockingMode(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
mode, err := db.SetLockingMode("", "exclusive") mode, err := db.SetLockingMode("", "exclusive")
checkNoError(t, err, "Error setting locking-mode of database: %s") checkNoError(t, err, "Error setting locking-mode of database: %s")
assertEquals(t, "expecting %s but got %s", "exclusive", mode) assertEquals(t, "expecting %s but got %s", "exclusive", mode)
...@@ -64,7 +64,7 @@ func TestSetLockingMode(t *testing.T) { ...@@ -64,7 +64,7 @@ func TestSetLockingMode(t *testing.T) {
func TestSynchronous(t *testing.T) { func TestSynchronous(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
mode, err := db.Synchronous("") mode, err := db.Synchronous("")
checkNoError(t, err, "Error reading synchronous flag of database: %s") checkNoError(t, err, "Error reading synchronous flag of database: %s")
assertEquals(t, "expecting %d but got %d", 2, mode) assertEquals(t, "expecting %d but got %d", 2, mode)
...@@ -72,7 +72,7 @@ func TestSynchronous(t *testing.T) { ...@@ -72,7 +72,7 @@ func TestSynchronous(t *testing.T) {
func TestSetSynchronous(t *testing.T) { func TestSetSynchronous(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
err := db.SetSynchronous("", 0) err := db.SetSynchronous("", 0)
checkNoError(t, err, "Error setting synchronous flag of database: %s") checkNoError(t, err, "Error setting synchronous flag of database: %s")
mode, err := db.Synchronous("") mode, err := db.Synchronous("")
......
...@@ -31,6 +31,14 @@ func open(t *testing.T) *Conn { ...@@ -31,6 +31,14 @@ func open(t *testing.T) *Conn {
return db return db
} }
func checkClose(db *Conn, t *testing.T) {
checkNoError(t, db.Close(), "Error closing database: %s")
}
func checkFinalize(s *Stmt, t *testing.T) {
checkNoError(t, s.Finalize(), "Error finalizing statement: %s")
}
func createTable(db *Conn, t *testing.T) { func createTable(db *Conn, t *testing.T) {
err := db.Exec("DROP TABLE IF EXISTS test;" + err := db.Exec("DROP TABLE IF EXISTS test;" +
"CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL," + "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL," +
...@@ -52,7 +60,7 @@ func TestOpen(t *testing.T) { ...@@ -52,7 +60,7 @@ func TestOpen(t *testing.T) {
func TestEnableFKey(t *testing.T) { func TestEnableFKey(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
b := Must(db.IsFKeyEnabled()) b := Must(db.IsFKeyEnabled())
if !b { if !b {
b = Must(db.EnableFKey(true)) b = Must(db.EnableFKey(true))
...@@ -62,7 +70,7 @@ func TestEnableFKey(t *testing.T) { ...@@ -62,7 +70,7 @@ func TestEnableFKey(t *testing.T) {
func TestEnableTriggers(t *testing.T) { func TestEnableTriggers(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
b := Must(db.AreTriggersEnabled()) b := Must(db.AreTriggersEnabled())
if !b { if !b {
b = Must(db.EnableTriggers(true)) b = Must(db.EnableTriggers(true))
...@@ -72,19 +80,19 @@ func TestEnableTriggers(t *testing.T) { ...@@ -72,19 +80,19 @@ func TestEnableTriggers(t *testing.T) {
func TestEnableExtendedResultCodes(t *testing.T) { func TestEnableExtendedResultCodes(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
checkNoError(t, db.EnableExtendedResultCodes(true), "cannot enabled extended result codes: %s") checkNoError(t, db.EnableExtendedResultCodes(true), "cannot enabled extended result codes: %s")
} }
func TestCreateTable(t *testing.T) { func TestCreateTable(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
createTable(db, t) createTable(db, t)
} }
func TestTransaction(t *testing.T) { func TestTransaction(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
checkNoError(t, db.Begin(), "Error while beginning transaction: %s") checkNoError(t, db.Begin(), "Error while beginning transaction: %s")
if err := db.Begin(); err == nil { if err := db.Begin(); err == nil {
t.Fatalf("Error expected (transaction cannot be nested)") t.Fatalf("Error expected (transaction cannot be nested)")
...@@ -94,7 +102,7 @@ func TestTransaction(t *testing.T) { ...@@ -94,7 +102,7 @@ func TestTransaction(t *testing.T) {
func TestSavepoint(t *testing.T) { func TestSavepoint(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
checkNoError(t, db.Savepoint("1"), "Error while creating savepoint: %s") checkNoError(t, db.Savepoint("1"), "Error while creating savepoint: %s")
checkNoError(t, db.Savepoint("2"), "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.RollbackSavepoint("2"), "Error while creating savepoint: %s")
...@@ -103,7 +111,7 @@ func TestSavepoint(t *testing.T) { ...@@ -103,7 +111,7 @@ func TestSavepoint(t *testing.T) {
func TestExists(t *testing.T) { func TestExists(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
b := Must(db.Exists("SELECT 1 where 1 = 0")) b := Must(db.Exists("SELECT 1 where 1 = 0"))
assert(t, "No row expected", !b) assert(t, "No row expected", !b)
b = Must(db.Exists("SELECT 1 where 1 = 1")) b = Must(db.Exists("SELECT 1 where 1 = 1"))
...@@ -112,7 +120,7 @@ func TestExists(t *testing.T) { ...@@ -112,7 +120,7 @@ func TestExists(t *testing.T) {
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
createTable(db, t) createTable(db, t)
db.Begin() db.Begin()
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
...@@ -127,7 +135,7 @@ func TestInsert(t *testing.T) { ...@@ -127,7 +135,7 @@ func TestInsert(t *testing.T) {
assertEquals(t, "last insert row id error: expected %d but got %d", int64(1000), lastId) assertEquals(t, "last insert row id error: expected %d but got %d", int64(1000), lastId)
cs, _ := db.Prepare("SELECT COUNT(*) FROM test") cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer cs.Finalize() defer checkFinalize(cs, t)
paramCount := cs.BindParameterCount() paramCount := cs.BindParameterCount()
assertEquals(t, "bind parameter count error: expected %d but got %d", 0, paramCount) assertEquals(t, "bind parameter count error: expected %d but got %d", 0, paramCount)
...@@ -148,14 +156,14 @@ func TestInsert(t *testing.T) { ...@@ -148,14 +156,14 @@ func TestInsert(t *testing.T) {
func TestInsertWithStatement(t *testing.T) { func TestInsertWithStatement(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
createTable(db, t) createTable(db, t)
s, serr := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (:f, :i, :s)") s, serr := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (:f, :i, :s)")
checkNoError(t, serr, "prepare error: %s") checkNoError(t, serr, "prepare error: %s")
if s == nil { if s == nil {
t.Fatal("statement is nil") t.Fatal("statement is nil")
} }
defer s.Finalize() defer checkFinalize(s, t)
assert(t, "update statement should not be readonly", !s.ReadOnly()) assert(t, "update statement should not be readonly", !s.ReadOnly())
...@@ -181,7 +189,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -181,7 +189,7 @@ func TestInsertWithStatement(t *testing.T) {
checkNoError(t, db.Commit(), "Error: %s") checkNoError(t, db.Commit(), "Error: %s")
cs, _ := db.Prepare("SELECT COUNT(*) FROM test") cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer cs.Finalize() defer checkFinalize(cs, t)
assert(t, "select statement should be readonly", cs.ReadOnly()) assert(t, "select statement should be readonly", cs.ReadOnly())
if !Must(cs.Next()) { if !Must(cs.Next()) {
t.Fatal("no result for count") t.Fatal("no result for count")
...@@ -191,7 +199,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -191,7 +199,7 @@ func TestInsertWithStatement(t *testing.T) {
assertEquals(t, "count should be %d, but it is %d", 1000, i) assertEquals(t, "count should be %d, but it is %d", 1000, i)
rs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test where a_string like ? ORDER BY int_num LIMIT 2", "hel%") rs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test where a_string like ? ORDER BY int_num LIMIT 2", "hel%")
defer rs.Finalize() defer checkFinalize(rs, t)
columnCount = rs.ColumnCount() columnCount = rs.ColumnCount()
assertEquals(t, "column count error: expected %d but got %d", 3, columnCount) assertEquals(t, "column count error: expected %d but got %d", 3, columnCount)
secondColumnName := rs.ColumnName(1) secondColumnName := rs.ColumnName(1)
...@@ -222,11 +230,11 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -222,11 +230,11 @@ func TestInsertWithStatement(t *testing.T) {
func TestScanColumn(t *testing.T) { func TestScanColumn(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("select 1, null, 0") s, err := db.Prepare("select 1, null, 0")
checkNoError(t, err, "prepare error: %s") checkNoError(t, err, "prepare error: %s")
defer s.Finalize() defer checkFinalize(s, t)
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
...@@ -244,11 +252,11 @@ func TestScanColumn(t *testing.T) { ...@@ -244,11 +252,11 @@ func TestScanColumn(t *testing.T) {
func TestNamedScanColumn(t *testing.T) { func TestNamedScanColumn(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("select 1 as i1, null as i2, 0 as i3") s, err := db.Prepare("select 1 as i1, null as i2, 0 as i3")
checkNoError(t, err, "prepare error: %s") checkNoError(t, err, "prepare error: %s")
defer s.Finalize() defer checkFinalize(s, t)
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
...@@ -266,11 +274,11 @@ func TestNamedScanColumn(t *testing.T) { ...@@ -266,11 +274,11 @@ func TestNamedScanColumn(t *testing.T) {
func TestScanCheck(t *testing.T) { func TestScanCheck(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("select 'hello'") s, err := db.Prepare("select 'hello'")
checkNoError(t, err, "prepare error: %s") checkNoError(t, err, "prepare error: %s")
defer s.Finalize() defer checkFinalize(s, t)
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
...@@ -298,11 +306,11 @@ func TestLoadExtension(t *testing.T) { ...@@ -298,11 +306,11 @@ func TestLoadExtension(t *testing.T) {
func TestScanNull(t *testing.T) { func TestScanNull(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("select null") s, err := db.Prepare("select null")
checkNoError(t, err, "prepare error: %s") checkNoError(t, err, "prepare error: %s")
defer s.Finalize() defer checkFinalize(s, t)
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
...@@ -318,11 +326,11 @@ func TestScanNull(t *testing.T) { ...@@ -318,11 +326,11 @@ func TestScanNull(t *testing.T) {
func TestScanNotNull(t *testing.T) { func TestScanNotNull(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("select 1") s, err := db.Prepare("select 1")
checkNoError(t, err, "prepare error: %s") checkNoError(t, err, "prepare error: %s")
defer s.Finalize() defer checkFinalize(s, t)
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
...@@ -352,7 +360,7 @@ func TestCloseTwice(t *testing.T) { ...@@ -352,7 +360,7 @@ func TestCloseTwice(t *testing.T) {
func TestStmtMisuse(t *testing.T) { func TestStmtMisuse(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
s, err := db.Prepare("MISUSE") s, err := db.Prepare("MISUSE")
assert(t, "error expected", s == nil && err != nil) assert(t, "error expected", s == nil && err != nil)
...@@ -363,20 +371,20 @@ func TestStmtMisuse(t *testing.T) { ...@@ -363,20 +371,20 @@ func TestStmtMisuse(t *testing.T) {
func TestOpenSameMemoryDb(t *testing.T) { func TestOpenSameMemoryDb(t *testing.T) {
db1, err := Open("file:dummy.db?mode=memory&cache=shared", OpenUri, OpenReadWrite, OpenCreate, OpenFullMutex) db1, err := Open("file:dummy.db?mode=memory&cache=shared", OpenUri, OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "open error: %s") checkNoError(t, err, "open error: %s")
defer db1.Close() defer checkClose(db1, t)
err = db1.Exec("CREATE TABLE test (data TEXT)") err = db1.Exec("CREATE TABLE test (data TEXT)")
checkNoError(t, err, "exec error: %s") checkNoError(t, err, "exec error: %s")
db2, err := Open("file:dummy.db?mode=memory&cache=shared", OpenUri, OpenReadWrite, OpenCreate, OpenFullMutex) db2, err := Open("file:dummy.db?mode=memory&cache=shared", OpenUri, OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "open error: %s") checkNoError(t, err, "open error: %s")
defer db2.Close() defer checkClose(db2, t)
_, err = db2.Exists("SELECT 1 from test") _, err = db2.Exists("SELECT 1 from test")
checkNoError(t, err, "exists error: %s") checkNoError(t, err, "exists error: %s")
} }
func TestStmtWithClosedDb(t *testing.T) { func TestStmtWithClosedDb(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
db.SetCacheSize(0) db.SetCacheSize(0)
......
...@@ -82,7 +82,7 @@ func updateHook(d interface{}, a Action, dbName, tableName string, rowId int64) ...@@ -82,7 +82,7 @@ func updateHook(d interface{}, a Action, dbName, tableName string, rowId int64)
func TestNoTrace(t *testing.T) { func TestNoTrace(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
db.Trace(nil, nil) db.Trace(nil, nil)
db.SetAuthorizer(nil, nil) db.SetAuthorizer(nil, nil)
db.Profile(nil, nil) db.Profile(nil, nil)
...@@ -95,7 +95,7 @@ func TestNoTrace(t *testing.T) { ...@@ -95,7 +95,7 @@ func TestNoTrace(t *testing.T) {
func TestTrace(t *testing.T) { func TestTrace(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
db.Trace(trace, t) db.Trace(trace, t)
err := db.SetAuthorizer(authorizer, t) err := db.SetAuthorizer(authorizer, t)
checkNoError(t, err, "couldn't set an authorizer") checkNoError(t, err, "couldn't set an authorizer")
......
...@@ -98,7 +98,7 @@ func (vc *testVTabCursor) Rowid() (int64, error) { ...@@ -98,7 +98,7 @@ func (vc *testVTabCursor) Rowid() (int64, error) {
func TestCreateModule(t *testing.T) { func TestCreateModule(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer checkClose(db, t)
intarray := []int{1, 2, 3} intarray := []int{1, 2, 3}
err := db.CreateModule("test", testModule{t, intarray}) err := db.CreateModule("test", testModule{t, intarray})
checkNoError(t, err, "couldn't create module: %s") checkNoError(t, err, "couldn't create module: %s")
...@@ -107,7 +107,7 @@ func TestCreateModule(t *testing.T) { ...@@ -107,7 +107,7 @@ func TestCreateModule(t *testing.T) {
s, err := db.Prepare("SELECT * from vtab") s, err := db.Prepare("SELECT * from vtab")
checkNoError(t, err, "couldn't select from virtual table: %s") checkNoError(t, err, "couldn't select from virtual table: %s")
defer s.Finalize() defer checkFinalize(s, t)
var i, value int var i, value int
err = s.Select(func(s *Stmt) (err error) { err = s.Select(func(s *Stmt) (err error) {
if err = s.Scan(&value); err != nil { if err = s.Scan(&value); err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment