Merge branch 'pr/207'

This commit is contained in:
Yasuhiro Matsumoto 2015-11-02 11:53:42 +09:00
commit 167173a31f
4 changed files with 56 additions and 35 deletions

View File

@ -586,7 +586,7 @@ func errorString(err Error) string {
// file:test.db?cache=shared&mode=memory // file:test.db?cache=shared&mode=memory
// :memory: // :memory:
// file::memory: // file::memory:
// go-sqlite handle especially query parameters. // go-sqlite3 adds the following query parameters to those used by SQLite:
// _loc=XXX // _loc=XXX
// Specify location of time format. It's possible to specify "auto". // Specify location of time format. It's possible to specify "auto".
// _busy_timeout=XXX // _busy_timeout=XXX

View File

@ -12,7 +12,7 @@ import (
) )
func TestFTS3(t *testing.T) { func TestFTS3(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)

View File

@ -6,12 +6,11 @@
package sqlite3 package sqlite3
import ( import (
"crypto/rand"
"database/sql" "database/sql"
"database/sql/driver" "database/sql/driver"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -25,15 +24,19 @@ import (
"github.com/mattn/go-sqlite3/sqlite3_test" "github.com/mattn/go-sqlite3/sqlite3_test"
) )
func TempFilename() string { func TempFilename(t *testing.T) string {
randBytes := make([]byte, 16) f, err := ioutil.TempFile("", "go-sqlite3-test-")
rand.Read(randBytes) if err != nil {
return filepath.Join(os.TempDir(), "foo"+hex.EncodeToString(randBytes)+".db") t.Fatal(err)
}
f.Close()
return f.Name()
} }
func doTestOpen(t *testing.T, option string) (string, error) { func doTestOpen(t *testing.T, option string) (string, error) {
var url string var url string
tempFilename := TempFilename() tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
if option != "" { if option != "" {
url = tempFilename + option url = tempFilename + option
} else { } else {
@ -84,8 +87,29 @@ func TestOpen(t *testing.T) {
} }
} }
func TestReadonly(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
db1, err := sql.Open("sqlite3", "file:"+tempFilename)
if err != nil {
t.Fatal(err)
}
db1.Exec("CREATE TABLE test (x int, y float)")
db2, err := sql.Open("sqlite3", "file:"+tempFilename+"?mode=ro")
if err != nil {
t.Fatal(err)
}
_ = db2
_, err = db2.Exec("INSERT INTO test VALUES (1, 3.14)")
if err == nil {
t.Fatal("didn't expect INSERT into read-only database to work")
}
}
func TestClose(t *testing.T) { func TestClose(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -111,7 +135,7 @@ func TestClose(t *testing.T) {
} }
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -150,7 +174,7 @@ func TestInsert(t *testing.T) {
} }
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -215,7 +239,7 @@ func TestUpdate(t *testing.T) {
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -276,7 +300,7 @@ func TestDelete(t *testing.T) {
} }
func TestBooleanRoundtrip(t *testing.T) { func TestBooleanRoundtrip(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -325,7 +349,7 @@ func TestBooleanRoundtrip(t *testing.T) {
} }
func TestTimestamp(t *testing.T) { func TestTimestamp(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -408,7 +432,7 @@ func TestTimestamp(t *testing.T) {
} }
func TestBoolean(t *testing.T) { func TestBoolean(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -500,7 +524,7 @@ func TestBoolean(t *testing.T) {
} }
func TestFloat32(t *testing.T) { func TestFloat32(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -538,7 +562,7 @@ func TestFloat32(t *testing.T) {
} }
func TestNull(t *testing.T) { func TestNull(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -570,7 +594,7 @@ func TestNull(t *testing.T) {
} }
func TestTransaction(t *testing.T) { func TestTransaction(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -630,7 +654,7 @@ func TestTransaction(t *testing.T) {
} }
func TestWAL(t *testing.T) { func TestWAL(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -678,7 +702,7 @@ func TestWAL(t *testing.T) {
func TestTimezoneConversion(t *testing.T) { func TestTimezoneConversion(t *testing.T) {
zones := []string{"UTC", "US/Central", "US/Pacific", "Local"} zones := []string{"UTC", "US/Central", "US/Pacific", "Local"}
for _, tz := range zones { for _, tz := range zones {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename+"?_loc="+url.QueryEscape(tz)) db, err := sql.Open("sqlite3", tempFilename+"?_loc="+url.QueryEscape(tz))
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -786,7 +810,7 @@ func TestSuite(t *testing.T) {
// TODO: Execer & Queryer currently disabled // TODO: Execer & Queryer currently disabled
// https://github.com/mattn/go-sqlite3/issues/82 // https://github.com/mattn/go-sqlite3/issues/82
func TestExecer(t *testing.T) { func TestExecer(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -806,7 +830,7 @@ func TestExecer(t *testing.T) {
} }
func TestQueryer(t *testing.T) { func TestQueryer(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -847,7 +871,7 @@ func TestQueryer(t *testing.T) {
} }
func TestStress(t *testing.T) { func TestStress(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -885,7 +909,7 @@ func TestStress(t *testing.T) {
func TestDateTimeLocal(t *testing.T) { func TestDateTimeLocal(t *testing.T) {
zone := "Asia/Tokyo" zone := "Asia/Tokyo"
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", tempFilename+"?_loc="+zone) db, err := sql.Open("sqlite3", tempFilename+"?_loc="+zone)
if err != nil { if err != nil {
t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
@ -952,7 +976,7 @@ func TestVersion(t *testing.T) {
} }
func TestNumberNamedParams(t *testing.T) { func TestNumberNamedParams(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -988,7 +1012,7 @@ func TestNumberNamedParams(t *testing.T) {
} }
func TestStringContainingZero(t *testing.T) { func TestStringContainingZero(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)
@ -1048,7 +1072,7 @@ func (t TimeStamp) Value() (driver.Value, error) {
} }
func TestDateTimeNow(t *testing.T) { func TestDateTimeNow(t *testing.T) {
tempFilename := TempFilename() tempFilename := TempFilename(t)
db, err := sql.Open("sqlite3", 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)

View File

@ -275,12 +275,11 @@ func TestPreparedStmt(t *testing.T) {
} }
const nRuns = 10 const nRuns = 10
ch := make(chan bool) var wg sync.WaitGroup
for i := 0; i < nRuns; i++ { for i := 0; i < nRuns; i++ {
wg.Add(1)
go func() { go func() {
defer func() { defer wg.Done()
ch <- true
}()
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
count := 0 count := 0
if err := sel.QueryRow().Scan(&count); err != nil && err != sql.ErrNoRows { if err := sel.QueryRow().Scan(&count); err != nil && err != sql.ErrNoRows {
@ -294,9 +293,7 @@ func TestPreparedStmt(t *testing.T) {
} }
}() }()
} }
for i := 0; i < nRuns; i++ { wg.Wait()
<-ch
}
} }
// Benchmarks need to use panic() since b.Error errors are lost when // Benchmarks need to use panic() since b.Error errors are lost when