Merge pull request #57 from gconnell/tempfile

Use file in os.TempDir() for tests.
This commit is contained in:
mattn 2013-05-11 05:53:27 -07:00
commit 6899118eff
1 changed files with 31 additions and 15 deletions

View File

@ -1,18 +1,28 @@
package sqlite package sqlite
import ( import (
"crypto/rand"
"database/sql" "database/sql"
"encoding/hex"
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
) )
func TempFilename() string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), "foo"+hex.EncodeToString(randBytes)+".db")
}
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("drop table foo") _, err = db.Exec("drop table foo")
@ -21,17 +31,18 @@ func TestOpen(t *testing.T) {
t.Fatal("Failed to create table:", err) t.Fatal("Failed to create table:", err)
} }
if stat, err := os.Stat("./foo.db"); err != nil || stat.IsDir() { if stat, err := os.Stat(tempFilename); err != nil || stat.IsDir() {
t.Error("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") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("drop table foo") _, err = db.Exec("drop table foo")
@ -65,11 +76,12 @@ func TestInsert(t *testing.T) {
} }
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("drop table foo") _, err = db.Exec("drop table foo")
@ -129,11 +141,12 @@ func TestUpdate(t *testing.T) {
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("drop table foo") _, err = db.Exec("drop table foo")
@ -189,11 +202,12 @@ func TestDelete(t *testing.T) {
} }
func TestBooleanRoundtrip(t *testing.T) { func TestBooleanRoundtrip(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("DROP TABLE foo") _, err = db.Exec("DROP TABLE foo")
@ -237,11 +251,12 @@ func TestBooleanRoundtrip(t *testing.T) {
} }
func TestTimestamp(t *testing.T) { func TestTimestamp(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("DROP TABLE foo") _, err = db.Exec("DROP TABLE foo")
@ -318,12 +333,13 @@ func TestTimestamp(t *testing.T) {
} }
func TestBoolean(t *testing.T) { func TestBoolean(t *testing.T) {
db, err := sql.Open("sqlite3", "./foo.db") tempFilename := TempFilename()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
} }
defer os.Remove("./foo.db") defer os.Remove(tempFilename)
defer db.Close() defer db.Close()
_, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)") _, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)")