go-sqlite3-hak5/sqlite3_test.go

399 lines
9.1 KiB
Go
Raw Normal View History

2011-11-12 18:52:02 +00:00
package sqlite
import (
2012-01-25 00:23:44 +00:00
"database/sql"
2011-11-12 18:52:02 +00:00
"os"
2011-12-07 11:40:21 +00:00
"testing"
"time"
2011-11-12 18:52:02 +00:00
)
func TestOpen(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Fatal("Failed to open database:", err)
2011-11-12 18:52:02 +00:00
}
defer os.Remove("./foo.db")
defer db.Close()
2011-11-12 18:52:02 +00:00
2011-11-15 01:41:43 +00:00
_, err = db.Exec("drop table foo")
2011-11-12 18:52:02 +00:00
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Fatal("Failed to create table:", err)
2011-11-12 18:52:02 +00:00
}
2011-12-07 11:40:21 +00:00
if stat, err := os.Stat("./foo.db"); err != nil || stat.IsDir() {
t.Error("Failed to create ./foo.db")
2011-11-12 18:52:02 +00:00
}
}
func TestInsert(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Fatal("Failed to open database:", err)
2011-11-12 18:52:02 +00:00
}
defer os.Remove("./foo.db")
defer db.Close()
2011-11-12 18:52:02 +00:00
2011-11-15 01:41:43 +00:00
_, err = db.Exec("drop table foo")
2011-11-12 18:52:02 +00:00
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Fatal("Failed to create table:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
res, err := db.Exec("insert into foo(id) values(123)")
2011-11-12 18:52:02 +00:00
if err != nil {
t.Fatal("Failed to insert record:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
affected, _ := res.RowsAffected()
if affected != 1 {
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
2011-11-15 02:03:31 +00:00
}
2011-11-12 18:52:02 +00:00
rows, err := db.Query("select id from foo")
if err != nil {
t.Fatal("Failed to select records:", err)
2011-11-12 18:52:02 +00: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) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Fatal("Failed to open database:", err)
2011-11-12 18:52:02 +00:00
}
defer os.Remove("./foo.db")
defer db.Close()
2011-11-12 18:52:02 +00:00
2011-11-15 01:41:43 +00:00
_, err = db.Exec("drop table foo")
2011-11-12 18:52:02 +00:00
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Fatal("Failed to create table:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
res, err := db.Exec("insert into foo(id) values(123)")
2011-11-12 18:52:02 +00:00
if err != nil {
t.Fatal("Failed to insert record:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
expected, err := res.LastInsertId()
if err != nil {
t.Fatal("Failed to get LastInsertId:", err)
2011-11-15 02:03:31 +00:00
}
affected, _ := res.RowsAffected()
if err != nil {
t.Fatal("Failed to get RowsAffected:", err)
2011-11-15 02:03:31 +00:00
}
if affected != 1 {
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
2011-11-15 02:03:31 +00:00
}
2011-11-12 18:52:02 +00:00
2011-11-15 02:03:31 +00:00
res, err = db.Exec("update foo set id = 234")
2011-11-12 18:52:02 +00:00
if err != nil {
t.Fatal("Failed to update record:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
lastId, err := res.LastInsertId()
if err != nil {
t.Fatal("Failed to get LastInsertId:", err)
2011-11-15 02:03:31 +00:00
}
if expected != lastId {
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
}
affected, _ = res.RowsAffected()
if err != nil {
t.Fatal("Failed to get RowsAffected:", err)
2011-11-15 02:03:31 +00:00
}
if affected != 1 {
t.Fatalf("Expected %d for affected rows, but %d:", 1, affected)
2011-11-15 02:03:31 +00:00
}
2011-11-12 18:52:02 +00:00
rows, err := db.Query("select id from foo")
if err != nil {
t.Fatal("Failed to select records:", err)
2011-11-12 18:52:02 +00: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) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Fatal("Failed to open database:", err)
2011-11-12 18:52:02 +00:00
}
defer os.Remove("./foo.db")
defer db.Close()
2011-11-12 18:52:02 +00:00
2011-11-15 01:41:43 +00:00
_, err = db.Exec("drop table foo")
2011-11-12 18:52:02 +00:00
_, err = db.Exec("create table foo (id integer)")
if err != nil {
t.Fatal("Failed to create table:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
res, err := db.Exec("insert into foo(id) values(123)")
2011-11-12 18:52:02 +00:00
if err != nil {
t.Fatal("Failed to insert record:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
expected, err := res.LastInsertId()
if err != nil {
t.Fatal("Failed to get LastInsertId:", err)
2011-11-15 02:03:31 +00:00
}
affected, err := res.RowsAffected()
if err != nil {
t.Fatal("Failed to get RowsAffected:", err)
2011-11-15 02:03:31 +00:00
}
if affected != 1 {
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
}
2011-11-12 18:52:02 +00:00
2011-11-15 02:03:31 +00:00
res, err = db.Exec("delete from foo where id = 123")
2011-11-12 18:52:02 +00:00
if err != nil {
t.Fatal("Failed to delete record:", err)
2011-11-12 18:52:02 +00:00
}
2011-11-15 02:03:31 +00:00
lastId, err := res.LastInsertId()
if err != nil {
t.Fatal("Failed to get LastInsertId:", err)
2011-11-15 02:03:31 +00:00
}
if expected != lastId {
t.Errorf("Expected %q for last Id, but %q:", expected, lastId)
}
affected, err = res.RowsAffected()
if err != nil {
t.Fatal("Failed to get RowsAffected:", err)
2011-11-15 02:03:31 +00:00
}
if affected != 1 {
t.Errorf("Expected %d for cout of affected rows, but %q:", 1, affected)
}
2011-11-12 18:52:02 +00:00
rows, err := db.Query("select id from foo")
if err != nil {
t.Fatal("Failed to select records:", err)
2011-11-12 18:52:02 +00:00
}
defer rows.Close()
if rows.Next() {
t.Error("Fetched row but expected not rows")
2011-11-12 18:52:02 +00:00
}
}
func TestBooleanRoundtrip(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer os.Remove("./foo.db")
defer db.Close()
_, err = db.Exec("DROP TABLE foo")
_, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)")
if err != nil {
t.Fatal("Failed to create table:", err)
}
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true)
if err != nil {
t.Fatal("Failed to insert true value:", err)
}
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false)
if err != nil {
t.Fatal("Failed to insert false value:", err)
}
rows, err := db.Query("SELECT id, value FROM foo")
if err != nil {
t.Fatal("Unable to query foo table:", err)
}
defer rows.Close()
for rows.Next() {
var id int
var value bool
2012-02-20 07:14:49 +00:00
if err := rows.Scan(&id, &value); err != nil {
t.Error("Unable to scan results:", err)
continue
}
if id == 1 && !value {
t.Error("Value for id 1 should be true, not false")
} else if id == 2 && value {
t.Error("Value for id 2 should be false, not true")
}
}
}
func TestTimestamp(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer os.Remove("./foo.db")
defer db.Close()
_, err = db.Exec("DROP TABLE foo")
2012-12-30 00:24:53 +00:00
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP, dt DATETIME)")
if err != nil {
t.Fatal("Failed to create table:", err)
}
timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC)
2012-12-30 00:24:53 +00:00
timestamp2 := time.Date(2006, time.January, 2, 15, 4, 5, 3000000, time.UTC)
tests := []struct {
value interface{}
expected time.Time
}{
{timestamp1, timestamp1},
{timestamp1.Unix(), timestamp1},
{"nonsense", time.Time{}},
{"0000-00-00 00:00:00", time.Time{}},
{timestamp1.In(time.FixedZone("TEST", -7*3600)), timestamp1},
{"2012-11-04", time.Date(2012, time.November, 4, 0, 0, 0, 0, time.UTC)},
{timestamp2, timestamp2},
{"2006-01-02 15:04:05.003", timestamp2},
}
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-29 22:47:17 +00:00
}
2012-12-30 00:24:53 +00:00
rows, err := db.Query("SELECT id, ts, dt FROM foo ORDER BY id ASC")
if err != nil {
t.Fatal("Unable to query foo table:", err)
}
defer rows.Close()
seen := 0
for rows.Next() {
var id int
2012-12-30 00:24:53 +00:00
var ts, dt time.Time
2012-12-30 00:24:53 +00:00
if err := rows.Scan(&id, &ts, &dt); err != nil {
t.Error("Unable to scan results:", err)
continue
}
2012-12-30 00:24:53 +00:00
if id < 0 || id >= len(tests) {
t.Error("Bad row id: ", id)
continue
}
2012-12-30 00:24:53 +00:00
seen++
if !tests[id].expected.Equal(ts) {
t.Errorf("Timestamp value for id %v should be %v, not %v", id, tests[id].expected, ts)
}
2012-12-30 00:24:53 +00:00
if !tests[id].expected.Equal(dt) {
t.Errorf("Datetime value for id %v should be %v, not %v", id, tests[id].expected, dt)
2012-12-29 22:47:17 +00:00
}
}
2012-12-30 00:24:53 +00:00
if seen != len(tests) {
t.Errorf("Expected to see %d rows", len(tests))
}
}
2012-05-25 15:16:03 +00:00
func TestBoolean(t *testing.T) {
2012-05-26 16:09:12 +00:00
db, err := sql.Open("sqlite3", "./foo.db")
2012-05-25 15:16:03 +00:00
if err != nil {
t.Fatal("Failed to open database:", err)
2012-05-25 15:16:03 +00:00
}
2012-05-26 16:09:12 +00:00
defer os.Remove("./foo.db")
defer db.Close()
2012-05-25 15:16:03 +00:00
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")
if err != nil {
t.Fatal("Failed to create table:", err)
2012-05-25 15:16:03 +00:00
}
2012-05-25 15:16:03 +00:00
bool1 := true
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(1, ?)", bool1)
if err != nil {
t.Fatal("Failed to insert boolean:", err)
2012-05-25 15:16:03 +00:00
}
bool2 := false
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2)
if err != nil {
t.Fatal("Failed to insert boolean:", err)
2012-05-25 15:16:03 +00:00
}
bool3 := "nonsense"
_, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3)
if err != nil {
t.Fatal("Failed to insert nonsense:", err)
2012-05-25 15:16:03 +00:00
}
rows, err := db.Query("SELECT id, fbool FROM foo where fbool = ?", bool1)
2012-05-25 15:16:03 +00:00
if err != nil {
t.Fatal("Unable to query foo table:", err)
2012-05-25 15:16:03 +00:00
}
2012-05-26 16:09:12 +00:00
counter := 0
2012-05-25 15:16:03 +00:00
var id int
var fbool bool
for rows.Next() {
2012-05-25 15:16:03 +00:00
if err := rows.Scan(&id, &fbool); err != nil {
t.Fatal("Unable to scan results:", err)
2012-05-25 15:16:03 +00:00
}
counter++
2012-05-26 16:09:12 +00:00
}
if counter != 1 {
t.Fatalf("Expected 1 row but %v", counter)
2012-05-26 16:09:12 +00:00
}
if id != 1 && fbool != true {
t.Fatalf("Value for id 1 should be %v, not %v", bool1, fbool)
}
rows, err = db.Query("SELECT id, fbool FROM foo where fbool = ?", bool2)
2012-05-26 16:09:12 +00:00
if err != nil {
t.Fatal("Unable to query foo table:", err)
2012-05-26 16:09:12 +00:00
}
2012-05-26 16:09:12 +00:00
counter = 0
2012-05-25 15:16:03 +00:00
for rows.Next() {
2012-05-26 16:09:12 +00:00
if err := rows.Scan(&id, &fbool); err != nil {
t.Fatal("Unable to scan results:", err)
2012-05-25 15:16:03 +00:00
}
counter++
2012-05-25 15:16:03 +00:00
}
if counter != 1 {
t.Fatalf("Expected 1 row but %v", counter)
2012-05-25 15:16:03 +00:00
}
2012-05-26 16:09:12 +00:00
if id != 2 && fbool != false {
t.Fatalf("Value for id 2 should be %v, not %v", bool2, fbool)
2012-05-26 16:09:12 +00:00
}
2012-05-25 15:16:03 +00:00
// make sure "nonsense" triggered an error
rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3)
if err != nil {
t.Fatal("Unable to query foo table:", err)
2012-05-25 15:16:03 +00:00
}
rows.Next()
err = rows.Scan(&id, &fbool)
if err == nil {
t.Error("Expected error from \"nonsense\" bool")
2012-05-25 15:16:03 +00:00
}
}