Commit ce883c04 authored by gwenn's avatar gwenn

Clean up tests (introduce checkNoError method).

parent 3f258c0f
...@@ -7,11 +7,15 @@ import ( ...@@ -7,11 +7,15 @@ import (
"testing" "testing"
) )
func open(t *testing.T) *Conn { func checkNoError(t *testing.T, err error, format string) {
db, err := Open("", OPEN_READWRITE, OPEN_CREATE, OPEN_FULLMUTEX, OPEN_URI)
if err != nil { if err != nil {
t.Fatalf("couldn't open database file: %s", err) t.Fatalf(format, err)
} }
}
func open(t *testing.T) *Conn {
db, err := Open("", OPEN_READWRITE, OPEN_CREATE, OPEN_FULLMUTEX, OPEN_URI)
checkNoError(t, err, "couldn't open database file: %s")
if db == nil { if db == nil {
t.Fatal("opened database is nil") t.Fatal("opened database is nil")
} }
...@@ -23,9 +27,7 @@ func createTable(db *Conn, t *testing.T) { ...@@ -23,9 +27,7 @@ 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 AUTOINCREMENT," + "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT," +
" float_num REAL, int_num INTEGER, a_string TEXT); -- bim") " float_num REAL, int_num INTEGER, a_string TEXT); -- bim")
if err != nil { checkNoError(t, err, "error creating table: %s")
t.Fatalf("error creating table: %s", err)
}
} }
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
...@@ -37,9 +39,7 @@ func TestVersion(t *testing.T) { ...@@ -37,9 +39,7 @@ func TestVersion(t *testing.T) {
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {
db := open(t) db := open(t)
if err := db.Close(); err != nil { checkNoError(t, db.Close(), "Error closing database: %s")
t.Fatalf("Error closing database: %s", err)
}
} }
func TestEnableFKey(t *testing.T) { func TestEnableFKey(t *testing.T) {
...@@ -57,17 +57,13 @@ func TestEnableFKey(t *testing.T) { ...@@ -57,17 +57,13 @@ func TestEnableFKey(t *testing.T) {
func TestEnableExtendedResultCodes(t *testing.T) { func TestEnableExtendedResultCodes(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer db.Close()
if err := db.EnableExtendedResultCodes(true); err != nil { checkNoError(t, db.EnableExtendedResultCodes(true), "cannot enabled extended result codes: %s")
t.Fatalf("cannot enabled extended result codes: %s", err)
}
} }
func TestIntegrityCheck(t *testing.T) { func TestIntegrityCheck(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer db.Close()
if err := db.IntegrityCheck(1, true); err != nil { checkNoError(t, db.IntegrityCheck(1, true), "Error checking integrity of database: %s")
t.Fatalf("Error checking integrity of database: %s", err)
}
} }
func TestCreateTable(t *testing.T) { func TestCreateTable(t *testing.T) {
...@@ -79,15 +75,11 @@ func TestCreateTable(t *testing.T) { ...@@ -79,15 +75,11 @@ func TestCreateTable(t *testing.T) {
func TestTransaction(t *testing.T) { func TestTransaction(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer db.Close()
if err := db.Begin(); err != nil { checkNoError(t, db.Begin(), "Error while beginning transaction: %s")
t.Fatalf("Error while beginning transaction: %s", err)
}
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)")
} }
if err := db.Commit(); err != nil { checkNoError(t, db.Commit(), "Error while commiting transaction: %s")
t.Fatalf("Error while commiting transaction: %s", err)
}
} }
func TestExists(t *testing.T) { func TestExists(t *testing.T) {
...@@ -110,17 +102,13 @@ func TestInsert(t *testing.T) { ...@@ -110,17 +102,13 @@ func TestInsert(t *testing.T) {
db.Begin() db.Begin()
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
ierr := db.Exec("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)", float64(i)*float64(3.14), i, "hello") ierr := db.Exec("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)", float64(i)*float64(3.14), i, "hello")
if ierr != nil { checkNoError(t, ierr, "insert error: %s")
t.Fatalf("insert error: %s", ierr)
}
c := db.Changes() c := db.Changes()
if c != 1 { if c != 1 {
t.Errorf("insert error: %d but got 1", c) t.Errorf("insert error: %d but got 1", c)
} }
} }
if err := db.Commit(); err != nil { checkNoError(t, db.Commit(), "Error: %s")
t.Fatalf("Error: %s", err)
}
lastId := db.LastInsertRowid() lastId := db.LastInsertRowid()
if lastId != 1000 { if lastId != 1000 {
...@@ -143,10 +131,7 @@ func TestInsert(t *testing.T) { ...@@ -143,10 +131,7 @@ func TestInsert(t *testing.T) {
t.Fatal("no result for count") t.Fatal("no result for count")
} }
var i int var i int
err := cs.Scan(&i) checkNoError(t, cs.Scan(&i), "error scanning count: %s")
if err != nil {
t.Fatalf("error scanning count: %s", err)
}
if i != 1000 { if i != 1000 {
t.Errorf("count should be 1000, but it is %d", i) t.Errorf("count should be 1000, but it is %d", i)
} }
...@@ -157,9 +142,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -157,9 +142,7 @@ func TestInsertWithStatement(t *testing.T) {
defer db.Close() defer db.Close()
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)")
if serr != nil { checkNoError(t, serr, "prepare error: %s")
t.Fatalf("prepare error: %s", serr)
}
if s == nil { if s == nil {
t.Fatal("statement is nil") t.Fatal("statement is nil")
} }
...@@ -185,17 +168,13 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -185,17 +168,13 @@ func TestInsertWithStatement(t *testing.T) {
db.Begin() db.Begin()
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
c, ierr := s.ExecUpdate(float64(i)*float64(3.14), i, "hello") c, ierr := s.ExecUpdate(float64(i)*float64(3.14), i, "hello")
if ierr != nil { checkNoError(t, ierr, "insert error: %s")
t.Fatalf("insert error: %s", ierr)
}
if c != 1 { if c != 1 {
t.Errorf("insert error: %d but got 1", c) t.Errorf("insert error: %d but got 1", c)
} }
} }
if err := db.Commit(); err != nil { checkNoError(t, db.Commit(), "Error: %s")
t.Fatalf("Error: %s", err)
}
cs, _ := db.Prepare("SELECT COUNT(*) FROM test") cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer cs.Finalize() defer cs.Finalize()
...@@ -206,10 +185,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -206,10 +185,7 @@ func TestInsertWithStatement(t *testing.T) {
t.Fatal("no result for count") t.Fatal("no result for count")
} }
var i int var i int
err := cs.Scan(&i) checkNoError(t, cs.Scan(&i), "error scanning count: %s")
if err != nil {
t.Fatalf("error scanning count: %s", err)
}
if i != 1000 { if i != 1000 {
t.Errorf("count should be 1000, but it is %d", i) t.Errorf("count should be 1000, but it is %d", i)
} }
...@@ -271,48 +247,32 @@ func TestBlob(t *testing.T) { ...@@ -271,48 +247,32 @@ func TestBlob(t *testing.T) {
defer db.Close() defer db.Close()
err := db.Exec("CREATE TABLE test (content BLOB);") err := db.Exec("CREATE TABLE test (content BLOB);")
if err != nil { checkNoError(t, err, "error creating table: %s")
t.Fatalf("error creating tables: %s", err)
}
s, err := db.Prepare("INSERT INTO test VALUES (?)") s, err := db.Prepare("INSERT INTO test VALUES (?)")
if err != nil { checkNoError(t, err, "prepare error: %s")
t.Fatalf("prepare error: %s", err)
}
if s == nil { if s == nil {
t.Fatal("statement is nil") t.Fatal("statement is nil")
} }
defer s.Finalize() defer s.Finalize()
err = s.Exec(ZeroBlobLength(10)) err = s.Exec(ZeroBlobLength(10))
if err != nil { checkNoError(t, err, "insert error: %s")
t.Fatalf("insert error: %s", err)
}
rowid := db.LastInsertRowid() rowid := db.LastInsertRowid()
bw, err := db.NewBlobReadWriter("main", "test", "content", rowid) bw, err := db.NewBlobReadWriter("main", "test", "content", rowid)
if err != nil { checkNoError(t, err, "blob open error: %s")
t.Fatalf("blob open error: %s", err)
}
defer bw.Close() defer bw.Close()
content := []byte("Clob") content := []byte("Clob")
n, err := bw.Write(content) n, err := bw.Write(content)
if err != nil { checkNoError(t, err, "blob write error: %s")
t.Fatalf("blob write error: %s", err)
}
br, err := db.NewBlobReader("main", "test", "content", rowid) br, err := db.NewBlobReader("main", "test", "content", rowid)
if err != nil { checkNoError(t, err, "blob open error: %s")
t.Fatalf("blob open error: %s", err)
}
defer br.Close() defer br.Close()
size, err := br.Size() size, err := br.Size()
if err != nil { checkNoError(t, err, "blob size error: %s")
t.Fatalf("blob size error: %s", err)
}
content = make([]byte, size) content = make([]byte, size)
n, err = br.Read(content) n, err = br.Read(content)
if err != nil { checkNoError(t, err, "blob read error: %s")
t.Fatalf("blob read error: %s", err)
}
if n != 10 { if n != 10 {
t.Fatalf("Expected 10 bytes but got %d", n) t.Fatalf("Expected 10 bytes but got %d", n)
} }
...@@ -325,9 +285,7 @@ func TestScanColumn(t *testing.T) { ...@@ -325,9 +285,7 @@ func TestScanColumn(t *testing.T) {
defer db.Close() defer db.Close()
s, err := db.Prepare("select 1, null, 0") s, err := db.Prepare("select 1, null, 0")
if err != nil { checkNoError(t, err, "prepare error: %s")
t.Fatalf("prepare error: %s", err)
}
defer s.Finalize() defer s.Finalize()
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
...@@ -358,9 +316,7 @@ func TestNamedScanColumn(t *testing.T) { ...@@ -358,9 +316,7 @@ func TestNamedScanColumn(t *testing.T) {
defer db.Close() defer db.Close()
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")
if err != nil { checkNoError(t, err, "prepare error: %s")
t.Fatalf("prepare error: %s", err)
}
defer s.Finalize() defer s.Finalize()
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
...@@ -391,9 +347,7 @@ func TestScanCheck(t *testing.T) { ...@@ -391,9 +347,7 @@ func TestScanCheck(t *testing.T) {
defer db.Close() defer db.Close()
s, err := db.Prepare("select 'hello'") s, err := db.Prepare("select 'hello'")
if err != nil { checkNoError(t, err, "prepare error: %s")
t.Fatalf("prepare error: %s", err)
}
defer s.Finalize() defer s.Finalize()
if !Must(s.Next()) { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
...@@ -422,8 +376,6 @@ func TestLoadExtension(t *testing.T) { ...@@ -422,8 +376,6 @@ func TestLoadExtension(t *testing.T) {
db.EnableLoadExtension(true) db.EnableLoadExtension(true)
err := db.LoadExtension("/tmp/myext.so") err := db.LoadExtension("/tmp/myext.so")
if err != nil { checkNoError(t, err, "load extension error: %s")
t.Errorf("load extension error: %s", err)
}
} }
*/ */
...@@ -47,9 +47,7 @@ func log(d interface{}, err error, msg string) { ...@@ -47,9 +47,7 @@ func log(d interface{}, err error, msg string) {
func TestNoTrace(t *testing.T) { func TestNoTrace(t *testing.T) {
db, err := Open("") db, err := Open("")
if err != nil { checkNoError(t, err, "couldn't open database: %s")
t.Fatalf("couldn't open database file: %s", err)
}
defer db.Close() defer db.Close()
db.Trace(nil, nil) db.Trace(nil, nil)
db.SetAuthorizer(nil, nil) db.SetAuthorizer(nil, nil)
...@@ -63,15 +61,11 @@ func TestNoTrace(t *testing.T) { ...@@ -63,15 +61,11 @@ func TestNoTrace(t *testing.T) {
func TestTrace(t *testing.T) { func TestTrace(t *testing.T) {
db, err := Open("") db, err := Open("")
if err != nil { checkNoError(t, err, "couldn't open database: %s")
t.Fatalf("couldn't open database file: %s", err)
}
defer db.Close() defer db.Close()
db.Trace(trace, "TRACE") db.Trace(trace, "TRACE")
err = db.SetAuthorizer(authorizer, "AUTH") err = db.SetAuthorizer(authorizer, "AUTH")
if err != nil { checkNoError(t, err, "couldn't set an authorizer")
t.Fatal("couldn't set an authorizer", err)
}
db.Profile(profile, "PROFILE") db.Profile(profile, "PROFILE")
db.ProgressHandler(progressHandler, 1, /*20*/ nil) db.ProgressHandler(progressHandler, 1, /*20*/ nil)
db.CommitHook(commitHook, "CMT") db.CommitHook(commitHook, "CMT")
......
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