-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
325 lines (251 loc) · 6.21 KB
/
exec_test.go
File metadata and controls
325 lines (251 loc) · 6.21 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package litesql
import (
"io"
"os"
"testing"
"time"
"cattlecloud.net/go/scope"
"github.com/shoenig/test/must"
)
const (
timeout = 3 * time.Second
)
var testConfiguration = &Configuration{
Mode: "rwc",
Encoding: "utf8",
BusyTimeout: 1000,
TransactionLock: "immediate",
ForeignKeys: true,
JournalMode: "OFF",
CacheSize: -4000,
AutoVacuum: "incremental",
Synchronous: "normal",
MemoryMapSize: 0,
MaxConnectionsOpen: 1,
}
type user struct {
ID int
Username string
Email string
}
func testSimple(t *testing.T) *LiteDB {
t.Helper()
// give ourselves a short timeout; all in memory
ctx, cancel := scope.WithTTL(t.Context(), timeout)
t.Cleanup(cancel)
// create the underlying sqlite3 database
ldb, oerr := Open(":memory:", testConfiguration)
must.NoError(t, oerr, must.Sprint("unable to open test database"))
// open the sample schema
f, ferr := os.Open("hack/simple.sql")
must.NoError(t, ferr, must.Sprint("unable to open simple.sql file"))
b, berr := io.ReadAll(f)
must.NoError(t, berr, must.Sprint("unable to read simple.sql file"))
stmt := string(b)
// execute the sample schema and populate our test database
_, xerr := ldb.db.ExecContext(ctx, stmt)
must.NoError(t, xerr)
// make sure we close the database once test is complete
t.Cleanup(func() { _ = ldb.Close() })
return ldb
}
func TestLiteDB_QueryRow(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartRead(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `SELECT COUNT(id) FROM users`
row := ldb.QueryRow(ctx, tx, stmt)
var count int
serr := row.Scan(&count)
must.NoError(t, serr)
rerr := row.Err()
must.NoError(t, rerr)
must.Eq(t, 5, count)
}
type pair[T, U any] struct {
A T
B U
}
func TestLiteDB_QueryRows(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartRead(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `
SELECT
id, provider
FROM
oauth
ORDER BY id DESC`
rows, rdone, rerr := ldb.QueryRows(ctx, tx, stmt) // nolint:sqlclosecheck
must.NoError(t, rerr)
defer rdone()
results := make([]pair[int, string], 0, 4)
for rows.Next() {
var (
id int
provider string
)
serr := rows.Scan(&id, &provider)
must.NoError(t, serr, must.Sprint("failure to scan row"))
results = append(results, pair[int, string]{A: id, B: provider})
}
must.NoError(t, rows.Err())
must.Eq(t, []pair[int, string]{
{A: 4, B: "Microsoft"},
{A: 3, B: "Google"},
{A: 2, B: "Apple"},
{A: 1, B: "Testing"},
}, results)
}
func TestLiteDB_ExecID(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartWrite(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `
INSERT INTO users (
oauth_id, username, email
) VALUES (?, ?, ?)`
const (
oid = 2
username = "ned"
email = "ned@example.org"
)
id, ierr := ldb.ExecID(ctx, tx, stmt, oid, username, email)
must.NoError(t, ierr)
must.Eq(t, 6, id) // 6th user insertion
cerr := tx.Commit()
must.NoError(t, cerr)
}
func TestLiteDB_Exec(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartWrite(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `
DELETE FROM users
WHERE
oauth_id = ?`
const oid = 4
derr := ldb.Exec(ctx, tx, ExpectNonZero, stmt, oid)
must.NoError(t, derr)
cerr := tx.Commit()
must.NoError(t, cerr)
}
func TestGlobal_QueryRow(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartRead(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `
SELECT
id, username, email
FROM
users
WHERE
ID = ?`
f := func(sf ScanFunc) (*user, error) {
u := new(user)
err := sf(
&u.ID,
&u.Username,
&u.Email,
)
return u, err
}
const id = 2
user, uerr := QueryRow(ctx, tx, f, stmt, id)
must.NoError(t, uerr)
must.Eq(t, 2, user.ID)
must.Eq(t, "Beth", user.Username)
must.Eq(t, "beth@example.org", user.Email)
}
func TestGlobal_QueryRow_int(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartRead(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `
SELECT standing_id FROM users WHERE id = ?`
f := func(sf ScanFunc) (int, error) {
var standing int
err := sf(
&standing,
)
return standing, err
}
const beth = 2 // (hardcode user id)
standing, uerr := QueryRow(ctx, tx, f, stmt, beth)
must.NoError(t, uerr)
must.Eq(t, 3, standing)
}
func TestGlobal_QueryRows(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartRead(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `
SELECT
id, username, email
FROM
users
ORDER BY id ASC`
f := func(sf ScanFunc) (*user, error) {
u := new(user)
err := sf(
&u.ID,
&u.Username,
&u.Email,
)
return u, err
}
users, uerr := QueryRows(ctx, tx, f, stmt)
must.NoError(t, uerr)
must.SliceLen(t, 5, users)
must.Eq(t, &user{ID: 1, Username: "Admin", Email: "admin@example.org"}, users[0])
must.Eq(t, &user{ID: 2, Username: "Beth", Email: "beth@example.org"}, users[1])
must.Eq(t, &user{ID: 3, Username: "Carl", Email: "carl@example.org"}, users[2])
must.Eq(t, &user{ID: 4, Username: "David", Email: "dave@example.org"}, users[3])
must.Eq(t, &user{ID: 5, Username: "Eve", Email: "eve@example.org"}, users[4])
}
func TestGlobal_QueryRows_int(t *testing.T) {
t.Parallel()
ctx, cancel := scope.WithTTL(t.Context(), timeout)
defer cancel()
ldb := testSimple(t)
tx, xdone, xerr := ldb.StartRead(ctx)
must.NoError(t, xerr)
defer xdone()
const stmt = `SELECT id FROM oauth ORDER BY id DESC`
f := func(sf ScanFunc) (int, error) {
var id int
err := sf(&id)
return id, err
}
oauths, oerr := QueryRows(ctx, tx, f, stmt)
must.NoError(t, oerr)
must.Eq(t, []int{4, 3, 2, 1}, oauths)
}