fix TestBoolean

This commit is contained in:
jander 2012-05-27 00:09:12 +08:00
parent 1bfaa5b7d2
commit ed17eae07a
1 changed files with 46 additions and 20 deletions

View File

@ -342,12 +342,13 @@ func TestTimestamp(t *testing.T) {
func TestBoolean(t *testing.T) { func TestBoolean(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:") db, err := sql.Open("sqlite3", "./foo.db")
if err != nil { if err != nil {
t.Errorf("Failed to open database:", err) t.Errorf("Failed to open database:", err)
return return
} }
defer db.Close() defer db.Close()
defer os.Remove("./foo.db")
_, 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 {
@ -376,36 +377,61 @@ func TestBoolean(t *testing.T) {
return return
} }
rows, err := db.Query("SELECT id, fbool FROM foo where (fbool is ?) or (fbool is ?);", true, false) rows, err := db.Query("SELECT id, fbool FROM foo where fbool is ?", bool1)
if err != nil {
t.Errorf("Unable to query foo table:", err)
return
}
counter := 0
var id int
var fbool bool
for rows.Next(){
if err := rows.Scan(&id, &fbool); err != nil {
t.Errorf("Unable to scan results:", err)
return
}
counter ++
}
if counter != 1{
t.Errorf("Expected 1 row but %v", counter)
return
}
if id!=1 && fbool != true {
t.Errorf("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)
if err != nil { if err != nil {
t.Errorf("Unable to query foo table:", err) t.Errorf("Unable to query foo table:", err)
return return
} }
seen := 0 counter = 0
var id int for rows.Next(){
var fbool bool
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.Errorf("Unable to scan results:", err)
continue return
}
counter ++
} }
if id == 1 && fbool != bool1 { if counter != 1{
t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool) t.Errorf("Expected 1 row but %v", counter)
return
} }
if id == 2 && fbool != bool2 { if id != 2 && fbool != false {
t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool) t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool)
} return
seen ++
} }
if seen != 2 {
t.Errorf("Expected to see two bool")
}
// 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)