Use memdb for tests. (#131)

This commit is contained in:
Nuno Cruces
2024-08-05 21:25:47 +01:00
committed by GitHub
parent 40db26c1dd
commit 8f835eda79
21 changed files with 154 additions and 70 deletions

View File

@@ -45,7 +45,7 @@ func (t params) mustExec(sql string, args ...interface{}) sql.Result {
func (sqliteDB) RunTest(t *testing.T, fn func(params)) {
db, err := sql.Open("sqlite3", "file:"+
filepath.Join(t.TempDir(), "foo.db")+
filepath.ToSlash(filepath.Join(t.TempDir(), "foo.db"))+
"?_pragma=busy_timeout(10000)&_pragma=synchronous(off)")
if err != nil {
t.Fatalf("foo.db open fail: %v", err)

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"math"
"net/url"
"os"
"path/filepath"
"strings"
@@ -13,6 +14,7 @@ import (
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs"
"github.com/ncruces/go-sqlite3/vfs/memdb"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
@@ -727,8 +729,11 @@ func TestConn_DBName(t *testing.T) {
func TestConn_AutoVacuumPages(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t, url.Values{
"_pragma": {"auto_vacuum(full)"},
})
db, err := sqlite3.Open("file:test.db?vfs=memdb&_pragma=auto_vacuum(full)")
db, err := sqlite3.Open(tmp)
if err != nil {
t.Fatal(err)
}

View File

@@ -12,6 +12,7 @@ import (
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs"
_ "github.com/ncruces/go-sqlite3/vfs/adiantum"
"github.com/ncruces/go-sqlite3/vfs/memdb"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
@@ -65,7 +66,7 @@ func TestDB_utf16(t *testing.T) {
func TestDB_memdb(t *testing.T) {
t.Parallel()
testDB(t, "file:test.db?vfs=memdb")
testDB(t, memdb.TestDB(t))
}
func TestDB_adiantum(t *testing.T) {

View File

@@ -8,15 +8,17 @@ import (
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func TestDriver(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := driver.Open(":memory:", nil, func(c *sqlite3.Conn) error {
db, err := driver.Open(tmp, nil, func(c *sqlite3.Conn) error {
return c.Exec(`PRAGMA optimize`)
})
if err != nil {

View File

@@ -11,16 +11,18 @@ import (
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
"github.com/ncruces/julianday"
)
func TestJSON(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := driver.Open(":memory:")
db, err := driver.Open(tmp)
if err != nil {
t.Fatal(err)
}

View File

@@ -2,6 +2,7 @@ package tests
import (
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
@@ -31,7 +32,7 @@ func Test_parallel(t *testing.T) {
}
name := "file:" +
filepath.Join(t.TempDir(), "test.db") +
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
@@ -45,7 +46,7 @@ func Test_wal(t *testing.T) {
}
name := "file:" +
filepath.Join(t.TempDir(), "test.db") +
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(wal)" +
"&_pragma=synchronous(off)"
@@ -61,8 +62,9 @@ func Test_memdb(t *testing.T) {
iter = 5000
}
memdb.Create("test.db", nil)
name := "file:/test.db?vfs=memdb"
name := memdb.TestDB(t, url.Values{
"_pragma": {"busy_timeout(10000)"},
})
testParallel(t, name, iter)
testIntegrity(t, name)
}
@@ -82,7 +84,10 @@ func Test_adiantum(t *testing.T) {
name := "file:" +
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
"?vfs=adiantum" +
"&_pragma=hexkey(e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855)"
"&_pragma=hexkey(e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855)" +
"&_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
testParallel(t, name, iter)
testIntegrity(t, name)
}
@@ -98,7 +103,7 @@ func TestMultiProcess(t *testing.T) {
file := filepath.Join(t.TempDir(), "test.db")
t.Setenv("TestMultiProcess_dbfile", file)
name := "file:" + file +
name := "file:" + filepath.ToSlash(file) +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
@@ -133,7 +138,7 @@ func TestChildProcess(t *testing.T) {
t.SkipNow()
}
name := "file:" + file +
name := "file:" + filepath.ToSlash(file) +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
@@ -177,8 +182,9 @@ func Benchmark_memdb(b *testing.B) {
sqlite3.Initialize()
b.ResetTimer()
memdb.Create("test.db", nil)
name := "file:/test.db?vfs=memdb"
name := memdb.TestDB(b, url.Values{
"_pragma": {"busy_timeout(10000)"},
})
testParallel(b, name, b.N)
}
@@ -218,11 +224,6 @@ func testParallel(t testing.TB, name string, n int) {
}
defer db.Close()
err = db.BusyTimeout(10 * time.Second)
if err != nil {
return err
}
stmt, _, err := db.Prepare(`SELECT id, name FROM users`)
if err != nil {
return err

View File

@@ -11,6 +11,7 @@ import (
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func TestTimeFormat_Encode(t *testing.T) {
@@ -132,11 +133,12 @@ func TestTimeFormat_Decode(t *testing.T) {
func TestTimeFormat_Scanner(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := driver.Open(":memory:")
db, err := driver.Open(tmp)
if err != nil {
t.Fatal(err)
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func TestConn_Transaction_exec(t *testing.T) {
@@ -254,14 +254,15 @@ func TestConn_Transaction_interrupted(t *testing.T) {
func TestConn_Transaction_busy(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
db1, err := sqlite3.Open("file:/test.db?vfs=memdb")
db1, err := sqlite3.Open(tmp)
if err != nil {
t.Fatal(err)
}
defer db1.Close()
db2, err := sqlite3.Open("file:/test.db?vfs=memdb&_pragma=busy_timeout(10000)")
db2, err := sqlite3.Open(tmp + "&_pragma=busy_timeout(10000)")
if err != nil {
t.Fatal(err)
}