Skip to content

Commit 8f835ed

Browse files
authored
Use memdb for tests. (#131)
1 parent 40db26c commit 8f835ed

21 files changed

+154
-70
lines changed

driver/driver_test.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
"errors"
88
"math"
99
"net/url"
10-
"path/filepath"
1110
"testing"
1211
"time"
1312

1413
"github.com/ncruces/go-sqlite3"
1514
_ "github.com/ncruces/go-sqlite3/embed"
1615
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
1716
"github.com/ncruces/go-sqlite3/internal/util"
18-
"github.com/ncruces/go-sqlite3/vfs"
17+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1918
)
2019

2120
func Test_Open_dir(t *testing.T) {
@@ -38,8 +37,11 @@ func Test_Open_dir(t *testing.T) {
3837

3938
func Test_Open_pragma(t *testing.T) {
4039
t.Parallel()
40+
tmp := memdb.TestDB(t, url.Values{
41+
"_pragma": {"busy_timeout(1000)"},
42+
})
4143

42-
db, err := sql.Open("sqlite3", "file::memory:?_pragma=busy_timeout(1000)")
44+
db, err := sql.Open("sqlite3", tmp)
4345
if err != nil {
4446
t.Fatal(err)
4547
}
@@ -57,8 +59,11 @@ func Test_Open_pragma(t *testing.T) {
5759

5860
func Test_Open_pragma_invalid(t *testing.T) {
5961
t.Parallel()
62+
tmp := memdb.TestDB(t, url.Values{
63+
"_pragma": {"busy_timeout 1000"},
64+
})
6065

61-
db, err := sql.Open("sqlite3", "file::memory:?_pragma=busy_timeout+1000")
66+
db, err := sql.Open("sqlite3", tmp)
6267
if err != nil {
6368
t.Fatal(err)
6469
}
@@ -81,14 +86,13 @@ func Test_Open_pragma_invalid(t *testing.T) {
8186
}
8287

8388
func Test_Open_txLock(t *testing.T) {
84-
if !vfs.SupportsFileLocking {
85-
t.Skip("skipping without locks")
86-
}
8789
t.Parallel()
90+
tmp := memdb.TestDB(t, url.Values{
91+
"_txlock": {"exclusive"},
92+
"_pragma": {"busy_timeout(1000)"},
93+
})
8894

89-
db, err := sql.Open("sqlite3", "file:"+
90-
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db"))+
91-
"?_txlock=exclusive&_pragma=busy_timeout(0)")
95+
db, err := sql.Open("sqlite3", tmp)
9296
if err != nil {
9397
t.Fatal(err)
9498
}
@@ -119,8 +123,11 @@ func Test_Open_txLock(t *testing.T) {
119123

120124
func Test_Open_txLock_invalid(t *testing.T) {
121125
t.Parallel()
126+
tmp := memdb.TestDB(t, url.Values{
127+
"_txlock": {"xclusive"},
128+
})
122129

123-
_, err := sql.Open("sqlite3", "file::memory:?_txlock=xclusive")
130+
_, err := sql.Open("sqlite3", tmp+"_txlock=xclusive")
124131
if err == nil {
125132
t.Fatal("want error")
126133
}
@@ -130,17 +137,16 @@ func Test_Open_txLock_invalid(t *testing.T) {
130137
}
131138

132139
func Test_BeginTx(t *testing.T) {
133-
if !vfs.SupportsFileLocking {
134-
t.Skip("skipping without locks")
135-
}
136140
t.Parallel()
141+
tmp := memdb.TestDB(t, url.Values{
142+
"_txlock": {"exclusive"},
143+
"_pragma": {"busy_timeout(0)"},
144+
})
137145

138146
ctx, cancel := context.WithCancel(context.Background())
139147
defer cancel()
140148

141-
db, err := sql.Open("sqlite3", "file:"+
142-
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db"))+
143-
"?_txlock=exclusive&_pragma=busy_timeout(0)")
149+
db, err := sql.Open("sqlite3", tmp)
144150
if err != nil {
145151
t.Fatal(err)
146152
}
@@ -182,8 +188,9 @@ func Test_BeginTx(t *testing.T) {
182188

183189
func Test_Prepare(t *testing.T) {
184190
t.Parallel()
191+
tmp := memdb.TestDB(t)
185192

186-
db, err := sql.Open("sqlite3", ":memory:")
193+
db, err := sql.Open("sqlite3", tmp)
187194
if err != nil {
188195
t.Fatal(err)
189196
}
@@ -222,11 +229,12 @@ func Test_Prepare(t *testing.T) {
222229

223230
func Test_QueryRow_named(t *testing.T) {
224231
t.Parallel()
232+
tmp := memdb.TestDB(t)
225233

226234
ctx, cancel := context.WithCancel(context.Background())
227235
defer cancel()
228236

229-
db, err := sql.Open("sqlite3", ":memory:")
237+
db, err := sql.Open("sqlite3", tmp)
230238
if err != nil {
231239
t.Fatal(err)
232240
}
@@ -274,8 +282,9 @@ func Test_QueryRow_named(t *testing.T) {
274282

275283
func Test_QueryRow_blob_null(t *testing.T) {
276284
t.Parallel()
285+
tmp := memdb.TestDB(t)
277286

278-
db, err := sql.Open("sqlite3", ":memory:")
287+
db, err := sql.Open("sqlite3", tmp)
279288
if err != nil {
280289
t.Fatal(err)
281290
}
@@ -310,7 +319,11 @@ func Test_time(t *testing.T) {
310319

311320
for _, fmt := range []string{"auto", "sqlite", "rfc3339", time.ANSIC} {
312321
t.Run(fmt, func(t *testing.T) {
313-
db, err := sql.Open("sqlite3", "file::memory:?_timefmt="+url.QueryEscape(fmt))
322+
tmp := memdb.TestDB(t, url.Values{
323+
"_timefmt": {fmt},
324+
})
325+
326+
db, err := sql.Open("sqlite3", tmp)
314327
if err != nil {
315328
t.Fatal(err)
316329
}

ext/array/array_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
_ "github.com/ncruces/go-sqlite3/embed"
1313
"github.com/ncruces/go-sqlite3/ext/array"
1414
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
15+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1516
)
1617

1718
func Example_driver() {
18-
db, err := driver.Open(":memory:", array.Register)
19+
db, err := driver.Open("file:/test.db?vfs=memdb", array.Register)
1920
if err != nil {
2021
log.Fatal(err)
2122
}
@@ -87,8 +88,9 @@ func Example() {
8788

8889
func Test_cursor_Column(t *testing.T) {
8990
t.Parallel()
91+
tmp := memdb.TestDB(t)
9092

91-
db, err := driver.Open(":memory:", array.Register)
93+
db, err := driver.Open(tmp, array.Register)
9294
if err != nil {
9395
t.Fatal(err)
9496
}

ext/fileio/fileio_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
_ "github.com/ncruces/go-sqlite3/embed"
1313
"github.com/ncruces/go-sqlite3/ext/fileio"
1414
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
15+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1516
)
1617

1718
func Test_lsmode(t *testing.T) {
1819
t.Parallel()
20+
tmp := memdb.TestDB(t)
1921

20-
db, err := driver.Open(":memory:", fileio.Register)
22+
db, err := driver.Open(tmp, fileio.Register)
2123
if err != nil {
2224
t.Fatal(err)
2325
}
@@ -51,7 +53,9 @@ func Test_readfile(t *testing.T) {
5153

5254
for _, fsys := range []fs.FS{nil, os.DirFS(".")} {
5355
t.Run("", func(t *testing.T) {
54-
db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
56+
tmp := memdb.TestDB(t)
57+
58+
db, err := driver.Open(tmp, func(c *sqlite3.Conn) error {
5559
fileio.RegisterFS(c, fsys)
5660
return nil
5761
})

ext/fileio/fsdir_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ import (
1313
_ "github.com/ncruces/go-sqlite3/embed"
1414
"github.com/ncruces/go-sqlite3/ext/fileio"
1515
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
16+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1617
)
1718

1819
func Test_fsdir(t *testing.T) {
1920
t.Parallel()
2021

2122
for _, fsys := range []fs.FS{nil, os.DirFS(".")} {
2223
t.Run("", func(t *testing.T) {
23-
db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
24+
tmp := memdb.TestDB(t)
25+
26+
db, err := driver.Open(tmp, func(c *sqlite3.Conn) error {
2427
fileio.RegisterFS(c, fsys)
2528
return nil
2629
})

ext/fileio/write_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
"github.com/ncruces/go-sqlite3/driver"
1111
_ "github.com/ncruces/go-sqlite3/embed"
1212
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
13+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1314
)
1415

1516
func Test_writefile(t *testing.T) {
1617
t.Parallel()
18+
tmp := memdb.TestDB(t)
1719

18-
db, err := driver.Open(":memory:", Register)
20+
db, err := driver.Open(tmp, Register)
1921
if err != nil {
2022
t.Fatal(err)
2123
}

ext/hash/hash_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ncruces/go-sqlite3/driver"
1111
_ "github.com/ncruces/go-sqlite3/embed"
1212
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
13+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1314
_ "golang.org/x/crypto/blake2b"
1415
_ "golang.org/x/crypto/blake2s"
1516
_ "golang.org/x/crypto/md4"
@@ -19,6 +20,7 @@ import (
1920

2021
func TestRegister(t *testing.T) {
2122
t.Parallel()
23+
tmp := memdb.TestDB(t)
2224

2325
tests := []struct {
2426
name string
@@ -52,7 +54,7 @@ func TestRegister(t *testing.T) {
5254
{"blake2b('', 256)", "0E5751C026E543B2E8AB2EB06099DAA1D1E5DF47778F7787FAAB45CDF12FE3A8"},
5355
}
5456

55-
db, err := driver.Open(":memory:", Register)
57+
db, err := driver.Open(tmp, Register)
5658
if err != nil {
5759
t.Fatal(err)
5860
}

ext/lines/lines_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
_ "github.com/ncruces/go-sqlite3/embed"
1616
"github.com/ncruces/go-sqlite3/ext/lines"
1717
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
18+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1819
)
1920

2021
func Example() {
21-
db, err := driver.Open(":memory:", lines.Register)
22+
db, err := driver.Open("file:/test.db?vfs=memdb", lines.Register)
2223
if err != nil {
2324
log.Fatal(err)
2425
}
@@ -66,8 +67,9 @@ func Example() {
6667

6768
func Test_lines(t *testing.T) {
6869
t.Parallel()
70+
tmp := memdb.TestDB(t)
6971

70-
db, err := driver.Open(":memory:", lines.Register)
72+
db, err := driver.Open(tmp, lines.Register)
7173
if err != nil {
7274
log.Fatal(err)
7375
}
@@ -96,8 +98,9 @@ func Test_lines(t *testing.T) {
9698

9799
func Test_lines_error(t *testing.T) {
98100
t.Parallel()
101+
tmp := memdb.TestDB(t)
99102

100-
db, err := driver.Open(":memory:", lines.Register)
103+
db, err := driver.Open(tmp, lines.Register)
101104
if err != nil {
102105
log.Fatal(err)
103106
}
@@ -120,8 +123,9 @@ func Test_lines_error(t *testing.T) {
120123

121124
func Test_lines_read(t *testing.T) {
122125
t.Parallel()
126+
tmp := memdb.TestDB(t)
123127

124-
db, err := driver.Open(":memory:", lines.Register)
128+
db, err := driver.Open(tmp, lines.Register)
125129
if err != nil {
126130
log.Fatal(err)
127131
}
@@ -151,8 +155,9 @@ func Test_lines_read(t *testing.T) {
151155

152156
func Test_lines_test(t *testing.T) {
153157
t.Parallel()
158+
tmp := memdb.TestDB(t)
154159

155-
db, err := driver.Open(":memory:", lines.Register)
160+
db, err := driver.Open(tmp, lines.Register)
156161
if err != nil {
157162
log.Fatal(err)
158163
}

ext/regexp/regexp_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"github.com/ncruces/go-sqlite3/driver"
77
_ "github.com/ncruces/go-sqlite3/embed"
88
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
9+
"github.com/ncruces/go-sqlite3/vfs/memdb"
910
)
1011

1112
func TestRegister(t *testing.T) {
1213
t.Parallel()
14+
tmp := memdb.TestDB(t)
1315

14-
db, err := driver.Open(":memory:", Register)
16+
db, err := driver.Open(tmp, Register)
1517
if err != nil {
1618
t.Fatal(err)
1719
}
@@ -45,8 +47,9 @@ func TestRegister(t *testing.T) {
4547

4648
func TestRegister_errors(t *testing.T) {
4749
t.Parallel()
50+
tmp := memdb.TestDB(t)
4851

49-
db, err := driver.Open(":memory:", Register)
52+
db, err := driver.Open(tmp, Register)
5053
if err != nil {
5154
t.Fatal(err)
5255
}

ext/uuid/uuid_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"github.com/ncruces/go-sqlite3/driver"
88
_ "github.com/ncruces/go-sqlite3/embed"
99
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
10+
"github.com/ncruces/go-sqlite3/vfs/memdb"
1011
)
1112

1213
func Test_generate(t *testing.T) {
1314
t.Parallel()
15+
tmp := memdb.TestDB(t)
1416

15-
db, err := driver.Open(":memory:", Register)
17+
db, err := driver.Open(tmp, Register)
1618
if err != nil {
1719
t.Fatal(err)
1820
}
@@ -130,8 +132,9 @@ func Test_generate(t *testing.T) {
130132

131133
func Test_convert(t *testing.T) {
132134
t.Parallel()
135+
tmp := memdb.TestDB(t)
133136

134-
db, err := driver.Open(":memory:", Register)
137+
db, err := driver.Open(tmp, Register)
135138
if err != nil {
136139
t.Fatal(err)
137140
}

0 commit comments

Comments
 (0)