generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_test.go
140 lines (113 loc) · 3.22 KB
/
sqlite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package sqlite_test
import (
"database/sql"
"path"
"strconv"
"testing"
"time"
"github.com/maragudk/sqlite"
"github.com/maragudk/sqlite/internal/assert"
)
func TestRegisterDriver(t *testing.T) {
t.Run("can open with default driver name sqlite", func(t *testing.T) {
sqlite.RegisterDriver(sqlite.Options{})
_, err := sql.Open("sqlite", ":memory:")
assert.NoErr(t, err)
})
t.Run("can open with custom driver name", func(t *testing.T) {
sqlite.RegisterDriver(sqlite.Options{Name: "foo"})
_, err := sql.Open("foo", ":memory:")
assert.NoErr(t, err)
})
t.Run("errors on no register", func(t *testing.T) {
_, err := sql.Open("bar", ":memory:")
assert.Err(t, err)
})
}
func TestDB_Open(t *testing.T) {
t.Run("sets default pragmas", func(t *testing.T) {
db := open(t, sqlite.Options{})
tests := []struct {
name string
expected string
}{
{name: "journal_mode", expected: "wal"},
{name: "busy_timeout", expected: "5000"},
{name: "foreign_keys", expected: "1"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var actual string
err := db.QueryRow(`pragma ` + test.name).Scan(&actual)
assert.NoErr(t, err)
assert.Equal(t, test.expected, actual)
})
}
})
t.Run("can set different journal mode", func(t *testing.T) {
db := open(t, sqlite.Options{
JournalMode: sqlite.JournalModeTruncate,
})
var actual string
err := db.QueryRow(`pragma journal_mode`).Scan(&actual)
assert.NoErr(t, err)
assert.Equal(t, sqlite.JournalModeTruncate.String(), actual)
})
}
func TestDB_QueryRow(t *testing.T) {
t.Run("select true, 1, 1.1, 'foo', 'foo'", func(t *testing.T) {
db := open(t, sqlite.Options{})
var b bool
var i int
var f float64
var s string
var d []byte
err := db.QueryRow(`select true, 1, 1.1, 'foo', 'foo'`).Scan(&b, &i, &f, &s, &d)
assert.NoErr(t, err)
assert.Equal(t, true, b)
assert.Equal(t, 1, i)
assert.Equal(t, 1.1, f)
assert.Equal(t, "foo", s)
assert.EqualBytes(t, []byte("foo"), d)
})
t.Run("select true, 1, 1.1, 'foo', 'foo' with args", func(t *testing.T) {
db := open(t, sqlite.Options{})
var b bool
var i int
var f float64
var s string
var d []byte
err := db.QueryRow(`select ?, ?, ?, ?, ?`, true, 1, 1.1, "foo", []byte("foo")).
Scan(&b, &i, &f, &s, &d)
assert.NoErr(t, err)
assert.Equal(t, true, b)
assert.Equal(t, 1, i)
assert.Equal(t, 1.1, f)
assert.Equal(t, "foo", s)
assert.EqualBytes(t, []byte("foo"), d)
})
t.Run("queries an inserted and updated row from a table", func(t *testing.T) {
db := open(t, sqlite.Options{})
_, err := db.Exec(`create table t (v int not null)`)
assert.NoErr(t, err)
_, err = db.Exec(`insert into t values (?)`, 1)
assert.NoErr(t, err)
var v int
err = db.QueryRow(`select * from t`).Scan(&v)
assert.NoErr(t, err)
assert.Equal(t, 1, v)
_, err = db.Exec(`update t set v = ?`, 2)
assert.NoErr(t, err)
err = db.QueryRow(`select * from t`).Scan(&v)
assert.NoErr(t, err)
assert.Equal(t, 2, v)
})
}
func open(t *testing.T, opts sqlite.Options) *sql.DB {
t.Helper()
opts.Name = strconv.Itoa(int(time.Now().UnixNano()))
sqlite.RegisterDriver(opts)
db, err := sql.Open(opts.Name, path.Join(t.TempDir(), "app.db"))
assert.NoErr(t, err)
return db
}