2014-08-18 11:56:31 +04:00
|
|
|
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-08-18 12:00:59 +04:00
|
|
|
|
2013-06-21 00:52:38 +04:00
|
|
|
package sqlite3
|
2011-11-12 22:52:02 +04:00
|
|
|
|
|
|
|
import (
|
2013-05-09 21:37:39 +04:00
|
|
|
"crypto/rand"
|
2012-01-25 04:23:44 +04:00
|
|
|
"database/sql"
|
2013-05-09 21:37:39 +04:00
|
|
|
"encoding/hex"
|
2011-11-12 22:52:02 +04:00
|
|
|
"os"
|
2013-05-09 21:37:39 +04:00
|
|
|
"path/filepath"
|
2011-12-07 15:40:21 +04:00
|
|
|
"testing"
|
2012-04-07 08:17:54 +04:00
|
|
|
"time"
|
2014-06-25 22:54:09 +04:00
|
|
|
|
2014-08-20 18:13:15 +04:00
|
|
|
"github.com/mattn/go-sqlite3/sqlite3_test"
|
2011-11-12 22:52:02 +04:00
|
|
|
)
|
|
|
|
|
2013-05-09 21:37:39 +04:00
|
|
|
func TempFilename() string {
|
|
|
|
randBytes := make([]byte, 16)
|
|
|
|
rand.Read(randBytes)
|
|
|
|
return filepath.Join(os.TempDir(), "foo"+hex.EncodeToString(randBytes)+".db")
|
|
|
|
}
|
|
|
|
|
2011-11-12 22:52:02 +04:00
|
|
|
func TestOpen(t *testing.T) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2011-11-12 22:52:02 +04:00
|
|
|
|
2011-11-15 05:41:43 +04:00
|
|
|
_, err = db.Exec("drop table foo")
|
2011-11-12 22:52:02 +04:00
|
|
|
_, err = db.Exec("create table foo (id integer)")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 21:37:39 +04:00
|
|
|
if stat, err := os.Stat(tempFilename); err != nil || stat.IsDir() {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to create ./foo.db")
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 14:36:33 +04:00
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
|
|
|
|
_, err = db.Exec("drop table foo")
|
|
|
|
_, err = db.Exec("create table foo (id integer)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.Prepare("select id from foo where id = ?")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to select records:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Close()
|
|
|
|
_, err = stmt.Exec(1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Failed to operate closed statement")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-12 22:52:02 +04:00
|
|
|
func TestInsert(t *testing.T) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2011-11-12 22:52:02 +04:00
|
|
|
|
2011-11-15 05:41:43 +04:00
|
|
|
_, err = db.Exec("drop table foo")
|
2011-11-12 22:52:02 +04:00
|
|
|
_, err = db.Exec("create table foo (id integer)")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:03:31 +04:00
|
|
|
res, err := db.Exec("insert into foo(id) values(123)")
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
affected, _ := res.RowsAffected()
|
|
|
|
if affected != 1 {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
|
|
|
rows, err := db.Query("select id from foo")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to select records:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
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) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2011-11-12 22:52:02 +04:00
|
|
|
|
2011-11-15 05:41:43 +04:00
|
|
|
_, err = db.Exec("drop table foo")
|
2011-11-12 22:52:02 +04:00
|
|
|
_, err = db.Exec("create table foo (id integer)")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:03:31 +04:00
|
|
|
res, err := db.Exec("insert into foo(id) values(123)")
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
expected, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
affected, _ := res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
if affected != 1 {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
2011-11-15 06:03:31 +04:00
|
|
|
res, err = db.Exec("update foo set id = 234")
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to update record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
lastId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
if expected != lastId {
|
|
|
|
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
|
|
|
}
|
|
|
|
affected, _ = res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
if affected != 1 {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
|
|
|
rows, err := db.Query("select id from foo")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to select records:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
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) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2011-11-12 22:52:02 +04:00
|
|
|
|
2011-11-15 05:41:43 +04:00
|
|
|
_, err = db.Exec("drop table foo")
|
2011-11-12 22:52:02 +04:00
|
|
|
_, err = db.Exec("create table foo (id integer)")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:03:31 +04:00
|
|
|
res, err := db.Exec("insert into foo(id) values(123)")
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
expected, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
if affected != 1 {
|
|
|
|
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
|
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
2011-11-15 06:03:31 +04:00
|
|
|
res, err = db.Exec("delete from foo where id = 123")
|
2011-11-12 22:52:02 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to delete record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
lastId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
if expected != lastId {
|
|
|
|
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
|
|
|
}
|
|
|
|
affected, err = res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
}
|
|
|
|
if affected != 1 {
|
|
|
|
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
|
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
|
|
|
rows, err := db.Query("select id from foo")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to select records:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Fetched row but expected not rows")
|
2011-11-12 22:52:02 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-07 02:56:36 +04:00
|
|
|
|
|
|
|
func TestBooleanRoundtrip(t *testing.T) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2012-02-07 02:56:36 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
}
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2012-02-07 02:56:36 +04:00
|
|
|
|
|
|
|
_, err = db.Exec("DROP TABLE foo")
|
|
|
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true)
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert true value:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false)
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert false value:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT id, value FROM foo")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to query foo table:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
}
|
2012-09-11 18:23:21 +04:00
|
|
|
defer rows.Close()
|
2012-02-07 02:56:36 +04:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
var value bool
|
|
|
|
|
2012-02-20 11:14:49 +04:00
|
|
|
if err := rows.Scan(&id, &value); err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Unable to scan results:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == 1 && !value {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Value for id 1 should be true, not false")
|
2012-02-07 02:56:36 +04:00
|
|
|
|
|
|
|
} else if id == 2 && value {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Value for id 2 should be false, not true")
|
2012-02-07 02:56:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-07 08:17:54 +04:00
|
|
|
|
|
|
|
func TestTimestamp(t *testing.T) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2012-04-07 08:17:54 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2012-04-07 08:17:54 +04:00
|
|
|
|
|
|
|
_, err = db.Exec("DROP TABLE foo")
|
2012-12-30 04:24:53 +04:00
|
|
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP, dt DATETIME)")
|
2012-04-07 08:17:54 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC)
|
2012-12-30 04:36:29 +04:00
|
|
|
timestamp2 := time.Date(2006, time.January, 2, 15, 4, 5, 123456789, time.UTC)
|
2012-12-30 04:51:15 +04:00
|
|
|
timestamp3 := time.Date(2012, time.November, 4, 0, 0, 0, 0, time.UTC)
|
2012-12-30 04:24:53 +04:00
|
|
|
tests := []struct {
|
|
|
|
value interface{}
|
|
|
|
expected time.Time
|
|
|
|
}{
|
|
|
|
{"nonsense", time.Time{}},
|
|
|
|
{"0000-00-00 00:00:00", time.Time{}},
|
2012-12-30 04:51:15 +04:00
|
|
|
{timestamp1, timestamp1},
|
|
|
|
{timestamp1.Unix(), timestamp1},
|
2015-01-02 09:31:46 +03:00
|
|
|
{timestamp1.UnixNano() / int64(time.Millisecond), timestamp1},
|
2012-12-30 04:24:53 +04:00
|
|
|
{timestamp1.In(time.FixedZone("TEST", -7*3600)), timestamp1},
|
2012-12-30 04:51:15 +04:00
|
|
|
{timestamp1.Format("2006-01-02 15:04:05.000"), timestamp1},
|
|
|
|
{timestamp1.Format("2006-01-02T15:04:05.000"), timestamp1},
|
|
|
|
{timestamp1.Format("2006-01-02 15:04:05"), timestamp1},
|
|
|
|
{timestamp1.Format("2006-01-02T15:04:05"), timestamp1},
|
2012-12-30 04:24:53 +04:00
|
|
|
{timestamp2, timestamp2},
|
2012-12-30 04:36:29 +04:00
|
|
|
{"2006-01-02 15:04:05.123456789", timestamp2},
|
2012-12-30 04:51:15 +04:00
|
|
|
{"2006-01-02T15:04:05.123456789", timestamp2},
|
|
|
|
{"2012-11-04", timestamp3},
|
|
|
|
{"2012-11-04 00:00", timestamp3},
|
|
|
|
{"2012-11-04 00:00:00", timestamp3},
|
|
|
|
{"2012-11-04 00:00:00.000", timestamp3},
|
|
|
|
{"2012-11-04T00:00", timestamp3},
|
|
|
|
{"2012-11-04T00:00:00", timestamp3},
|
|
|
|
{"2012-11-04T00:00:00.000", timestamp3},
|
2012-12-30 04:24:53 +04:00
|
|
|
}
|
|
|
|
for i := range tests {
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, ts, dt) VALUES(?, ?, ?)", i, tests[i].value, tests[i].value)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert timestamp:", err)
|
|
|
|
}
|
2012-12-30 02:47:17 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 04:24:53 +04:00
|
|
|
rows, err := db.Query("SELECT id, ts, dt FROM foo ORDER BY id ASC")
|
2012-04-07 08:17:54 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to query foo table:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
2012-09-11 18:23:21 +04:00
|
|
|
defer rows.Close()
|
2012-04-07 08:17:54 +04:00
|
|
|
|
|
|
|
seen := 0
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
2012-12-30 04:24:53 +04:00
|
|
|
var ts, dt time.Time
|
2012-04-07 08:17:54 +04:00
|
|
|
|
2012-12-30 04:24:53 +04:00
|
|
|
if err := rows.Scan(&id, &ts, &dt); err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Unable to scan results:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
continue
|
|
|
|
}
|
2012-12-30 04:24:53 +04:00
|
|
|
if id < 0 || id >= len(tests) {
|
|
|
|
t.Error("Bad row id: ", id)
|
|
|
|
continue
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
2012-12-30 04:24:53 +04:00
|
|
|
seen++
|
|
|
|
if !tests[id].expected.Equal(ts) {
|
2012-12-30 04:51:15 +04:00
|
|
|
t.Errorf("Timestamp value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt)
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
2012-12-30 04:24:53 +04:00
|
|
|
if !tests[id].expected.Equal(dt) {
|
2012-12-30 04:51:15 +04:00
|
|
|
t.Errorf("Datetime value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt)
|
2012-12-30 02:47:17 +04:00
|
|
|
}
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 04:24:53 +04:00
|
|
|
if seen != len(tests) {
|
|
|
|
t.Errorf("Expected to see %d rows", len(tests))
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 19:16:03 +04:00
|
|
|
|
|
|
|
func TestBoolean(t *testing.T) {
|
2013-05-09 21:37:39 +04:00
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
2012-05-25 19:16:03 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to open database:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
2013-05-09 21:37:39 +04:00
|
|
|
defer os.Remove(tempFilename)
|
2012-09-11 18:23:21 +04:00
|
|
|
defer db.Close()
|
2012-05-25 19:16:03 +04:00
|
|
|
|
|
|
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to create table:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
2012-05-25 19:16:03 +04:00
|
|
|
bool1 := true
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(1, ?)", bool1)
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert boolean:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool2 := false
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2)
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert boolean:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool3 := "nonsense"
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3)
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Failed to insert nonsense:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
|
|
|
|
2012-09-12 06:43:54 +04:00
|
|
|
rows, err := db.Query("SELECT id, fbool FROM foo where fbool = ?", bool1)
|
2012-05-25 19:16:03 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to query foo table:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-05-26 20:09:12 +04:00
|
|
|
counter := 0
|
2012-09-11 17:17:09 +04:00
|
|
|
|
2012-05-25 19:16:03 +04:00
|
|
|
var id int
|
|
|
|
var fbool bool
|
2012-09-11 17:17:09 +04:00
|
|
|
|
|
|
|
for rows.Next() {
|
2012-05-25 19:16:03 +04:00
|
|
|
if err := rows.Scan(&id, &fbool); err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to scan results:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
counter++
|
2012-05-26 20:09:12 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
|
|
|
if counter != 1 {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Expected 1 row but %v", counter)
|
2012-05-26 20:09:12 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
|
|
|
if id != 1 && fbool != true {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Value for id 1 should be %v, not %v", bool1, fbool)
|
2012-09-11 17:17:09 +04:00
|
|
|
}
|
|
|
|
|
2012-09-12 06:43:54 +04:00
|
|
|
rows, err = db.Query("SELECT id, fbool FROM foo where fbool = ?", bool2)
|
2012-05-26 20:09:12 +04:00
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to query foo table:", err)
|
2012-05-26 20:09:12 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
2012-05-26 20:09:12 +04:00
|
|
|
counter = 0
|
2012-05-25 19:16:03 +04:00
|
|
|
|
2012-09-11 17:17:09 +04:00
|
|
|
for rows.Next() {
|
2012-05-26 20:09:12 +04:00
|
|
|
if err := rows.Scan(&id, &fbool); err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to scan results:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
counter++
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
|
|
|
if counter != 1 {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Expected 1 row but %v", counter)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
2012-05-26 20:09:12 +04:00
|
|
|
if id != 2 && fbool != false {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatalf("Value for id 2 should be %v, not %v", bool2, fbool)
|
2012-05-26 20:09:12 +04:00
|
|
|
}
|
2012-05-25 19:16:03 +04:00
|
|
|
|
|
|
|
// make sure "nonsense" triggered an error
|
|
|
|
rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3)
|
|
|
|
if err != nil {
|
2012-09-11 19:13:11 +04:00
|
|
|
t.Fatal("Unable to query foo table:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rows.Next()
|
|
|
|
err = rows.Scan(&id, &fbool)
|
|
|
|
if err == nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Expected error from \"nonsense\" bool")
|
2012-05-25 19:16:03 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
}
|
2013-09-03 14:36:33 +04:00
|
|
|
|
|
|
|
func TestFloat32(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id) VALUES(null)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert null:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT id FROM foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to query foo table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rows.Next() {
|
|
|
|
t.Fatal("Unable to query results:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var id interface{}
|
|
|
|
if err := rows.Scan(&id); err != nil {
|
|
|
|
t.Fatal("Unable to scan results:", err)
|
|
|
|
}
|
|
|
|
if id != nil {
|
|
|
|
t.Error("Expected nil but not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNull(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT 3.141592")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to query foo table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rows.Next() {
|
|
|
|
t.Fatal("Unable to query results:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var v interface{}
|
|
|
|
if err := rows.Scan(&v); err != nil {
|
|
|
|
t.Fatal("Unable to scan results:", err)
|
|
|
|
}
|
|
|
|
f, ok := v.(float64)
|
|
|
|
if !ok {
|
|
|
|
t.Error("Expected float but not")
|
|
|
|
}
|
|
|
|
if f != 3.141592 {
|
|
|
|
t.Error("Expected 3.141592 but not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransaction(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to begin transaction:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec("INSERT INTO foo(id) VALUES(1)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert null:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Query("SELECT id from foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to query foo table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to rollback transaction:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
t.Fatal("Unable to query results:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err = db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to begin transaction:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec("INSERT INTO foo(id) VALUES(1)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert null:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to commit transaction:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err = tx.Query("SELECT id from foo")
|
2013-09-03 14:40:18 +04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure to query")
|
2013-09-03 14:36:33 +04:00
|
|
|
}
|
|
|
|
}
|
2013-09-09 05:44:44 +04:00
|
|
|
|
2013-09-12 19:38:11 +04:00
|
|
|
func TestWAL(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
defer db.Close()
|
|
|
|
if _, err = db.Exec("PRAGMA journal_mode=WAL;"); err != nil {
|
|
|
|
t.Fatal("Failed to Exec PRAGMA journal_mode:", err)
|
|
|
|
}
|
|
|
|
if _, err = db.Exec("PRAGMA locking_mode=EXCLUSIVE;"); err != nil {
|
|
|
|
t.Fatal("Failed to Exec PRAGMA locking_mode:", err)
|
|
|
|
}
|
|
|
|
if _, err = db.Exec("CREATE TABLE test (id SERIAL, user TEXT NOT NULL, name TEXT NOT NULL);"); err != nil {
|
|
|
|
t.Fatal("Failed to Exec CREATE TABLE:", err)
|
|
|
|
}
|
|
|
|
if _, err = db.Exec("INSERT INTO test (user, name) VALUES ('user','name');"); err != nil {
|
|
|
|
t.Fatal("Failed to Exec INSERT:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
trans, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to Begin:", err)
|
|
|
|
}
|
|
|
|
s, err := trans.Prepare("INSERT INTO test (user, name) VALUES (?, ?);")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to Prepare:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int
|
|
|
|
if err = trans.QueryRow("SELECT count(user) FROM test;").Scan(&count); err != nil {
|
|
|
|
t.Fatal("Failed to QueryRow:", err)
|
|
|
|
}
|
|
|
|
if _, err = s.Exec("bbbb", "aaaa"); err != nil {
|
|
|
|
t.Fatal("Failed to Exec prepared statement:", err)
|
|
|
|
}
|
|
|
|
if err = s.Close(); err != nil {
|
|
|
|
t.Fatal("Failed to Close prepared statement:", err)
|
|
|
|
}
|
|
|
|
if err = trans.Commit(); err != nil {
|
|
|
|
t.Fatal("Failed to Commit:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 11:56:03 +04:00
|
|
|
func TestSuite(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2014-07-04 05:25:27 +04:00
|
|
|
sqlite3_test.RunTests(t, db, sqlite3_test.SQLITE)
|
2013-09-18 11:56:03 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 05:46:35 +04:00
|
|
|
// TODO: Execer & Queryer currently disabled
|
|
|
|
// https://github.com/mattn/go-sqlite3/issues/82
|
2014-06-25 22:54:09 +04:00
|
|
|
func TestExecer(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(`
|
|
|
|
create table foo (id integer); -- one comment
|
|
|
|
insert into foo(id) values(?);
|
|
|
|
insert into foo(id) values(?);
|
|
|
|
insert into foo(id) values(?); -- another comment
|
|
|
|
`, 1, 2, 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to call db.Exec:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 23:06:39 +04:00
|
|
|
func TestQueryer(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(`
|
|
|
|
create table foo (id integer);
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to call db.Query:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query(`
|
|
|
|
insert into foo(id) values(?);
|
|
|
|
insert into foo(id) values(?);
|
|
|
|
insert into foo(id) values(?);
|
|
|
|
select id from foo order by id;
|
|
|
|
`, 3, 2, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to call db.Query:", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
n := 1
|
|
|
|
if rows != nil {
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
err = rows.Scan(&id)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to db.Query:", err)
|
|
|
|
}
|
|
|
|
if id != n {
|
|
|
|
t.Error("Failed to db.Query: not matched results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 12:44:23 +04:00
|
|
|
|
|
|
|
func TestStress(t *testing.T) {
|
|
|
|
tempFilename := TempFilename()
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
db.Exec("CREATE TABLE foo (id int);")
|
|
|
|
db.Exec("INSERT INTO foo VALUES(1);")
|
|
|
|
db.Exec("INSERT INTO foo VALUES(2);")
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
rows, err := db.Query("select * from foo where id=1;")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to call db.Query:", err)
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var i int
|
|
|
|
if err := rows.Scan(&i); err != nil {
|
|
|
|
t.Errorf("Scan failed: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
t.Errorf("Post-scan failed: %v\n", err)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
}
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
}
|