2011-11-12 22:52:02 +04:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2012-01-25 04:23:44 +04:00
|
|
|
"database/sql"
|
2011-11-12 22:52:02 +04:00
|
|
|
"os"
|
2012-04-07 08:17:54 +04:00
|
|
|
"strings"
|
2011-12-07 15:40:21 +04:00
|
|
|
"testing"
|
2012-04-07 08:17:54 +04:00
|
|
|
"time"
|
2011-11-12 22:52:02 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOpen(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", "./foo.db")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove("./foo.db")
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-07 15:40:21 +04:00
|
|
|
if stat, err := os.Stat("./foo.db"); 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsert(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", "./foo.db")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove("./foo.db")
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to insert record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
affected, _ := res.RowsAffected()
|
|
|
|
if affected != 1 {
|
|
|
|
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
|
|
|
|
return
|
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
|
|
|
rows, err := db.Query("select id from foo")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to select records:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
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 {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove("./foo.db")
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to insert record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
expected, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
affected, _ := res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if affected != 1 {
|
|
|
|
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
|
|
|
|
return
|
|
|
|
}
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to update record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
lastId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if expected != lastId {
|
|
|
|
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
|
|
|
}
|
|
|
|
affected, _ = res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if affected != 1 {
|
|
|
|
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
|
|
|
|
return
|
|
|
|
}
|
2011-11-12 22:52:02 +04:00
|
|
|
|
|
|
|
rows, err := db.Query("select id from foo")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to select records:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
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 {
|
2012-09-11 17:41:45 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove("./foo.db")
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to insert record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
expected, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to delete record:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
2011-11-15 06:03:31 +04:00
|
|
|
lastId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get LastInsertId:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if expected != lastId {
|
|
|
|
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
|
|
|
}
|
|
|
|
affected, err = res.RowsAffected()
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to get RowsAffected:", err)
|
2011-11-15 06:03:31 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to select records:", err)
|
2011-11-12 22:52:02 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
db, err := sql.Open("sqlite3", "./foo.db")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
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 {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true)
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert true value:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false)
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert false value:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT id, value FROM foo")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Unable to query foo table:", err)
|
2012-02-07 02:56:36 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
db, err := sql.Open("sqlite3", "./foo.db")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
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 {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
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 {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert timestamp:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
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 {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert timestamp:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(3, ?)", "nonsense")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert nonsense:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT id, ts FROM foo ORDER BY id ASC")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Unable to query foo table:", err)
|
2012-04-07 08:17:54 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
seen := 0
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
var ts time.Time
|
|
|
|
|
|
|
|
if err := rows.Scan(&id, &ts); 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Expected to see two valid timestamps")
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure "nonsense" triggered an error
|
|
|
|
err = rows.Err()
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "cannot parse \"nonsense\"") {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Expected error from \"nonsense\" timestamp")
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 19:16:03 +04:00
|
|
|
|
|
|
|
func TestBoolean(t *testing.T) {
|
2012-05-26 20:09:12 +04:00
|
|
|
db, err := sql.Open("sqlite3", "./foo.db")
|
2012-05-25 19:16:03 +04:00
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to open database:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
2012-05-26 20:09:12 +04:00
|
|
|
defer os.Remove("./foo.db")
|
2012-05-25 19:16:03 +04:00
|
|
|
|
|
|
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to create table:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
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 17:17:09 +04:00
|
|
|
t.Error("Failed to insert boolean:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bool2 := false
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2)
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert boolean:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bool3 := "nonsense"
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3)
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Failed to insert nonsense:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-05-26 20:09:12 +04:00
|
|
|
rows, err := db.Query("SELECT id, fbool FROM foo where fbool is ?", bool1)
|
2012-05-25 19:16:03 +04:00
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Unable to query foo table:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
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 17:17:09 +04:00
|
|
|
t.Error("Unable to scan results:", err)
|
|
|
|
return
|
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-05-26 20:09:12 +04:00
|
|
|
t.Errorf("Expected 1 row but %v", counter)
|
2012-09-11 17:17:09 +04:00
|
|
|
return
|
2012-05-26 20:09:12 +04:00
|
|
|
}
|
2012-09-11 17:17:09 +04:00
|
|
|
|
|
|
|
if id != 1 && fbool != true {
|
2012-05-26 20:09:12 +04:00
|
|
|
t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool)
|
|
|
|
return
|
2012-09-11 17:17:09 +04:00
|
|
|
}
|
|
|
|
|
2012-05-26 20:09:12 +04:00
|
|
|
rows, err = db.Query("SELECT id, fbool FROM foo where fbool is ?", bool2)
|
|
|
|
if err != nil {
|
2012-09-11 17:17:09 +04:00
|
|
|
t.Error("Unable to query foo table:", err)
|
2012-05-26 20:09:12 +04:00
|
|
|
return
|
|
|
|
}
|
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 17:17:09 +04:00
|
|
|
t.Error("Unable to scan results:", err)
|
|
|
|
return
|
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-05-26 20:09:12 +04:00
|
|
|
t.Errorf("Expected 1 row but %v", counter)
|
2012-09-11 17:17:09 +04:00
|
|
|
return
|
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 {
|
|
|
|
t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool)
|
|
|
|
return
|
|
|
|
}
|
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 17:17:09 +04:00
|
|
|
t.Error("Unable to query foo table:", err)
|
2012-05-25 19:16:03 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|