forked from mirror/go-sqlite3
Merge pull request #22 from cdsks/master
Fix sql.DB leaks in unit tests so that tests pass on vmware hgfs
This commit is contained in:
commit
8d4548d72c
|
@ -284,11 +284,11 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
||||||
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
|
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
|
||||||
case C.SQLITE_INTEGER:
|
case C.SQLITE_INTEGER:
|
||||||
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
||||||
switch rc.decltype[i]{
|
switch rc.decltype[i] {
|
||||||
case "timestamp":
|
case "timestamp":
|
||||||
dest[i] = time.Unix(val, 0)
|
dest[i] = time.Unix(val, 0)
|
||||||
case "boolean":
|
case "boolean":
|
||||||
dest[i] = val>0
|
dest[i] = val > 0
|
||||||
default:
|
default:
|
||||||
dest[i] = val
|
dest[i] = val
|
||||||
}
|
}
|
||||||
|
|
224
sqlite3_test.go
224
sqlite3_test.go
|
@ -11,53 +11,48 @@ import (
|
||||||
func TestOpen(t *testing.T) {
|
func TestOpen(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to open database:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("drop table foo")
|
_, err = db.Exec("drop table foo")
|
||||||
_, err = db.Exec("create table foo (id integer)")
|
_, err = db.Exec("create table foo (id integer)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if stat, err := os.Stat("./foo.db"); err != nil || stat.IsDir() {
|
if stat, err := os.Stat("./foo.db"); err != nil || stat.IsDir() {
|
||||||
t.Errorf("Failed to create ./foo.db")
|
t.Error("Failed to create ./foo.db")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsert(t *testing.T) {
|
func TestInsert(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to open database:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("drop table foo")
|
_, err = db.Exec("drop table foo")
|
||||||
_, err = db.Exec("create table foo (id integer)")
|
_, err = db.Exec("create table foo (id integer)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := db.Exec("insert into foo(id) values(123)")
|
res, err := db.Exec("insert into foo(id) values(123)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert record:", err)
|
t.Fatal("Failed to insert record:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
affected, _ := res.RowsAffected()
|
affected, _ := res.RowsAffected()
|
||||||
if affected != 1 {
|
if affected != 1 {
|
||||||
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
|
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query("select id from foo")
|
rows, err := db.Query("select id from foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to select records:", err)
|
t.Fatal("Failed to select records:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
@ -73,65 +68,55 @@ func TestInsert(t *testing.T) {
|
||||||
func TestUpdate(t *testing.T) {
|
func TestUpdate(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to open database:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("drop table foo")
|
_, err = db.Exec("drop table foo")
|
||||||
_, err = db.Exec("create table foo (id integer)")
|
_, err = db.Exec("create table foo (id integer)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := db.Exec("insert into foo(id) values(123)")
|
res, err := db.Exec("insert into foo(id) values(123)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert record:", err)
|
t.Fatal("Failed to insert record:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
expected, err := res.LastInsertId()
|
expected, err := res.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get LastInsertId:", err)
|
t.Fatal("Failed to get LastInsertId:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
affected, _ := res.RowsAffected()
|
affected, _ := res.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get RowsAffected:", err)
|
t.Fatal("Failed to get RowsAffected:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if affected != 1 {
|
if affected != 1 {
|
||||||
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
|
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err = db.Exec("update foo set id = 234")
|
res, err = db.Exec("update foo set id = 234")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to update record:", err)
|
t.Fatal("Failed to update record:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
lastId, err := res.LastInsertId()
|
lastId, err := res.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get LastInsertId:", err)
|
t.Fatal("Failed to get LastInsertId:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if expected != lastId {
|
if expected != lastId {
|
||||||
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
||||||
}
|
}
|
||||||
affected, _ = res.RowsAffected()
|
affected, _ = res.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get RowsAffected:", err)
|
t.Fatal("Failed to get RowsAffected:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if affected != 1 {
|
if affected != 1 {
|
||||||
t.Errorf("Expected %d for affected rows, but %d:", 1, affected)
|
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query("select id from foo")
|
rows, err := db.Query("select id from foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to select records:", err)
|
t.Fatal("Failed to select records:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
@ -147,32 +132,28 @@ func TestUpdate(t *testing.T) {
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to select records:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("drop table foo")
|
_, err = db.Exec("drop table foo")
|
||||||
_, err = db.Exec("create table foo (id integer)")
|
_, err = db.Exec("create table foo (id integer)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := db.Exec("insert into foo(id) values(123)")
|
res, err := db.Exec("insert into foo(id) values(123)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert record:", err)
|
t.Fatal("Failed to insert record:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
expected, err := res.LastInsertId()
|
expected, err := res.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get LastInsertId:", err)
|
t.Fatal("Failed to get LastInsertId:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
affected, err := res.RowsAffected()
|
affected, err := res.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get RowsAffected:", err)
|
t.Fatal("Failed to get RowsAffected:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if affected != 1 {
|
if affected != 1 {
|
||||||
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
|
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
|
||||||
|
@ -180,21 +161,18 @@ func TestDelete(t *testing.T) {
|
||||||
|
|
||||||
res, err = db.Exec("delete from foo where id = 123")
|
res, err = db.Exec("delete from foo where id = 123")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to delete record:", err)
|
t.Fatal("Failed to delete record:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
lastId, err := res.LastInsertId()
|
lastId, err := res.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get LastInsertId:", err)
|
t.Fatal("Failed to get LastInsertId:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if expected != lastId {
|
if expected != lastId {
|
||||||
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
|
||||||
}
|
}
|
||||||
affected, err = res.RowsAffected()
|
affected, err = res.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get RowsAffected:", err)
|
t.Fatal("Failed to get RowsAffected:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if affected != 1 {
|
if affected != 1 {
|
||||||
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
|
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
|
||||||
|
@ -202,63 +180,59 @@ func TestDelete(t *testing.T) {
|
||||||
|
|
||||||
rows, err := db.Query("select id from foo")
|
rows, err := db.Query("select id from foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to select records:", err)
|
t.Fatal("Failed to select records:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
t.Errorf("Fetched row but expected not rows")
|
t.Error("Fetched row but expected not rows")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBooleanRoundtrip(t *testing.T) {
|
func TestBooleanRoundtrip(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to open database:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("DROP TABLE foo")
|
_, err = db.Exec("DROP TABLE foo")
|
||||||
_, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)")
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true)
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert true value:", err)
|
t.Fatal("Failed to insert true value:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false)
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert false value:", err)
|
t.Fatal("Failed to insert false value:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id, value FROM foo")
|
rows, err := db.Query("SELECT id, value FROM foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to query foo table:", err)
|
t.Fatal("Unable to query foo table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id int
|
var id int
|
||||||
var value bool
|
var value bool
|
||||||
|
|
||||||
if err := rows.Scan(&id, &value); err != nil {
|
if err := rows.Scan(&id, &value); err != nil {
|
||||||
t.Errorf("Unable to scan results:", err)
|
t.Error("Unable to scan results:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if id == 1 && !value {
|
if id == 1 && !value {
|
||||||
t.Errorf("Value for id 1 should be true, not false")
|
t.Error("Value for id 1 should be true, not false")
|
||||||
|
|
||||||
} else if id == 2 && value {
|
} else if id == 2 && value {
|
||||||
t.Errorf("Value for id 2 should be false, not true")
|
t.Error("Value for id 2 should be false, not true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -266,43 +240,39 @@ func TestBooleanRoundtrip(t *testing.T) {
|
||||||
func TestTimestamp(t *testing.T) {
|
func TestTimestamp(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to open database:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("DROP TABLE foo")
|
_, err = db.Exec("DROP TABLE foo")
|
||||||
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP)")
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC)
|
timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC)
|
||||||
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(1, ?)", timestamp1)
|
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(1, ?)", timestamp1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert timestamp:", err)
|
t.Fatal("Failed to insert timestamp:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp2 := time.Date(2012, time.April, 6, 23, 22, 0, 0, time.UTC)
|
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())
|
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(2, ?)", timestamp2.Unix())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert timestamp:", err)
|
t.Fatal("Failed to insert timestamp:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(3, ?)", "nonsense")
|
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(3, ?)", "nonsense")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert nonsense:", err)
|
t.Fatal("Failed to insert nonsense:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id, ts FROM foo ORDER BY id ASC")
|
rows, err := db.Query("SELECT id, ts FROM foo ORDER BY id ASC")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to query foo table:", err)
|
t.Fatal("Unable to query foo table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
seen := 0
|
seen := 0
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
|
@ -310,7 +280,7 @@ func TestTimestamp(t *testing.T) {
|
||||||
var ts time.Time
|
var ts time.Time
|
||||||
|
|
||||||
if err := rows.Scan(&id, &ts); err != nil {
|
if err := rows.Scan(&id, &ts); err != nil {
|
||||||
t.Errorf("Unable to scan results:", err)
|
t.Error("Unable to scan results:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,119 +300,103 @@ func TestTimestamp(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if seen != 2 {
|
if seen != 2 {
|
||||||
t.Errorf("Expected to see two valid timestamps")
|
t.Error("Expected to see two valid timestamps")
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure "nonsense" triggered an error
|
// make sure "nonsense" triggered an error
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
if err == nil || !strings.Contains(err.Error(), "cannot parse \"nonsense\"") {
|
if err == nil || !strings.Contains(err.Error(), "cannot parse \"nonsense\"") {
|
||||||
t.Errorf("Expected error from \"nonsense\" timestamp")
|
t.Error("Expected error from \"nonsense\" timestamp")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestBoolean(t *testing.T) {
|
func TestBoolean(t *testing.T) {
|
||||||
db, err := sql.Open("sqlite3", "./foo.db")
|
db, err := sql.Open("sqlite3", "./foo.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to open database:", err)
|
t.Fatal("Failed to open database:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer os.Remove("./foo.db")
|
defer os.Remove("./foo.db")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")
|
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create table:", err)
|
t.Fatal("Failed to create table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool1 := true
|
bool1 := true
|
||||||
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(1, ?)", bool1)
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(1, ?)", bool1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert boolean:", err)
|
t.Fatal("Failed to insert boolean:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool2 := false
|
bool2 := false
|
||||||
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2)
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert boolean:", err)
|
t.Fatal("Failed to insert boolean:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool3 := "nonsense"
|
bool3 := "nonsense"
|
||||||
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3)
|
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to insert nonsense:", err)
|
t.Fatal("Failed to insert nonsense:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id, fbool FROM foo where fbool is ?", bool1)
|
rows, err := db.Query("SELECT id, fbool FROM foo where fbool is ?", bool1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to query foo table:", err)
|
t.Fatal("Unable to query foo table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
counter := 0
|
counter := 0
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
var fbool bool
|
var fbool bool
|
||||||
|
|
||||||
for rows.Next(){
|
for rows.Next() {
|
||||||
if err := rows.Scan(&id, &fbool); err != nil {
|
if err := rows.Scan(&id, &fbool); err != nil {
|
||||||
t.Errorf("Unable to scan results:", err)
|
t.Fatal("Unable to scan results:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
counter ++
|
counter++
|
||||||
}
|
}
|
||||||
|
|
||||||
if counter != 1{
|
if counter != 1 {
|
||||||
t.Errorf("Expected 1 row but %v", counter)
|
t.Fatalf("Expected 1 row but %v", counter)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if id!=1 && fbool != true {
|
if id != 1 && fbool != true {
|
||||||
t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool)
|
t.Fatalf("Value for id 1 should be %v, not %v", bool1, fbool)
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
rows, err = db.Query("SELECT id, fbool FROM foo where fbool is ?", bool2)
|
rows, err = db.Query("SELECT id, fbool FROM foo where fbool is ?", bool2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to query foo table:", err)
|
t.Fatal("Unable to query foo table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
for rows.Next(){
|
for rows.Next() {
|
||||||
if err := rows.Scan(&id, &fbool); err != nil {
|
if err := rows.Scan(&id, &fbool); err != nil {
|
||||||
t.Errorf("Unable to scan results:", err)
|
t.Fatal("Unable to scan results:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
counter ++
|
counter++
|
||||||
}
|
}
|
||||||
|
|
||||||
if counter != 1{
|
if counter != 1 {
|
||||||
t.Errorf("Expected 1 row but %v", counter)
|
t.Fatalf("Expected 1 row but %v", counter)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if id != 2 && fbool != false {
|
if id != 2 && fbool != false {
|
||||||
t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool)
|
t.Fatalf("Value for id 2 should be %v, not %v", bool2, fbool)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// make sure "nonsense" triggered an error
|
// make sure "nonsense" triggered an error
|
||||||
rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3)
|
rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to query foo table:", err)
|
t.Fatal("Unable to query foo table:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows.Next()
|
rows.Next()
|
||||||
err = rows.Scan(&id, &fbool)
|
err = rows.Scan(&id, &fbool)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error from \"nonsense\" bool")
|
t.Error("Expected error from \"nonsense\" bool")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue