diff --git a/sqlite3.go b/sqlite3.go index d56bed3..2179cf2 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -586,7 +586,7 @@ func errorString(err Error) string { // file:test.db?cache=shared&mode=memory // :memory: // file::memory: -// go-sqlite handle especially query parameters. +// go-sqlite3 adds the following query parameters to those used by SQLite: // _loc=XXX // Specify location of time format. It's possible to specify "auto". // _busy_timeout=XXX diff --git a/sqlite3_fts3_test.go b/sqlite3_fts3_test.go index 884ca4c..beb5ada 100644 --- a/sqlite3_fts3_test.go +++ b/sqlite3_fts3_test.go @@ -12,7 +12,7 @@ import ( ) func TestFTS3(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) diff --git a/sqlite3_test.go b/sqlite3_test.go index 0239c78..90bee51 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -6,12 +6,11 @@ package sqlite3 import ( - "crypto/rand" "database/sql" "database/sql/driver" - "encoding/hex" "errors" "fmt" + "io/ioutil" "net/url" "os" "path/filepath" @@ -25,15 +24,19 @@ import ( "github.com/mattn/go-sqlite3/sqlite3_test" ) -func TempFilename() string { - randBytes := make([]byte, 16) - rand.Read(randBytes) - return filepath.Join(os.TempDir(), "foo"+hex.EncodeToString(randBytes)+".db") +func TempFilename(t *testing.T) string { + f, err := ioutil.TempFile("", "go-sqlite3-test-") + if err != nil { + t.Fatal(err) + } + f.Close() + return f.Name() } func doTestOpen(t *testing.T, option string) (string, error) { var url string - tempFilename := TempFilename() + tempFilename := TempFilename(t) + defer os.Remove(tempFilename) if option != "" { url = tempFilename + option } 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) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -111,7 +135,7 @@ func TestClose(t *testing.T) { } func TestInsert(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -150,7 +174,7 @@ func TestInsert(t *testing.T) { } func TestUpdate(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -215,7 +239,7 @@ func TestUpdate(t *testing.T) { } func TestDelete(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -276,7 +300,7 @@ func TestDelete(t *testing.T) { } func TestBooleanRoundtrip(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -325,7 +349,7 @@ func TestBooleanRoundtrip(t *testing.T) { } func TestTimestamp(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -408,7 +432,7 @@ func TestTimestamp(t *testing.T) { } func TestBoolean(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -500,7 +524,7 @@ func TestBoolean(t *testing.T) { } func TestFloat32(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -538,7 +562,7 @@ func TestFloat32(t *testing.T) { } func TestNull(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -570,7 +594,7 @@ func TestNull(t *testing.T) { } func TestTransaction(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -630,7 +654,7 @@ func TestTransaction(t *testing.T) { } func TestWAL(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -678,7 +702,7 @@ func TestWAL(t *testing.T) { func TestTimezoneConversion(t *testing.T) { zones := []string{"UTC", "US/Central", "US/Pacific", "Local"} for _, tz := range zones { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename+"?_loc="+url.QueryEscape(tz)) if err != nil { t.Fatal("Failed to open database:", err) @@ -786,7 +810,7 @@ func TestSuite(t *testing.T) { // TODO: Execer & Queryer currently disabled // https://github.com/mattn/go-sqlite3/issues/82 func TestExecer(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -806,7 +830,7 @@ func TestExecer(t *testing.T) { } func TestQueryer(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -847,7 +871,7 @@ func TestQueryer(t *testing.T) { } func TestStress(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -885,7 +909,7 @@ func TestStress(t *testing.T) { func TestDateTimeLocal(t *testing.T) { zone := "Asia/Tokyo" - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename+"?_loc="+zone) if err != nil { t.Fatal("Failed to open database:", err) @@ -952,7 +976,7 @@ func TestVersion(t *testing.T) { } func TestNumberNamedParams(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -988,7 +1012,7 @@ func TestNumberNamedParams(t *testing.T) { } func TestStringContainingZero(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) @@ -1048,7 +1072,7 @@ func (t TimeStamp) Value() (driver.Value, error) { } func TestDateTimeNow(t *testing.T) { - tempFilename := TempFilename() + tempFilename := TempFilename(t) db, err := sql.Open("sqlite3", tempFilename) if err != nil { t.Fatal("Failed to open database:", err) diff --git a/sqlite3_test/sqltest.go b/sqlite3_test/sqltest.go index 782e15f..84b65d9 100644 --- a/sqlite3_test/sqltest.go +++ b/sqlite3_test/sqltest.go @@ -275,12 +275,11 @@ func TestPreparedStmt(t *testing.T) { } const nRuns = 10 - ch := make(chan bool) + var wg sync.WaitGroup for i := 0; i < nRuns; i++ { + wg.Add(1) go func() { - defer func() { - ch <- true - }() + defer wg.Done() for j := 0; j < 10; j++ { count := 0 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++ { - <-ch - } + wg.Wait() } // Benchmarks need to use panic() since b.Error errors are lost when