Skip to content

Commit 68aef21

Browse files
authored
test: add integration tests for SQLite (#19)
1 parent 19bce79 commit 68aef21

File tree

8 files changed

+78
-12
lines changed

8 files changed

+78
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
tests/coverdata/
22
tests/coverage.out
3+
tests/test.sqlite

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ lint:
1414
@golangci-lint run
1515

1616
test:
17-
@rm -rf tests/coverdata tests/coverage.out && mkdir tests/coverdata
17+
@rm -rf tests/coverdata tests/coverage.out tests/test.sqlite && mkdir tests/coverdata
1818
@go test -race -shuffle=on -cover . -args -test.gocoverdir=$$PWD/tests/coverdata
1919
@$(CONTAINER_RUNNER) compose --file=$$PWD/tests/compose.yaml up --detach
2020
@go test -v -race -coverpkg=go-simpler.org/queries ./tests -args -test.gocoverdir=$$PWD/tests/coverdata

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ db.QueryContext(ctx, "SELECT id, name FROM users")
102102
Integration tests cover the following databases and drivers:
103103
- PostgreSQL with [jackx/pgx][3]
104104
- MySQL with [go-sql-driver/mysql][4]
105+
- SQLite with [modernc.org/sqlite][5]
105106

106107
## 🚧 TODOs
107108

@@ -113,3 +114,4 @@ Integration tests cover the following databases and drivers:
113114
[2]: https://grpc.io/docs/guides/interceptors
114115
[3]: https://github.com/jackc/pgx
115116
[4]: https://github.com/go-sql-driver/mysql
117+
[5]: https://gitlab.com/cznic/sqlite

interceptor_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func TestInterceptor_unimplemented(t *testing.T) {
9393
assert.NoErr[F](t, err)
9494
defer db.Close()
9595

96+
pingFn := func() { _ = db.PingContext(ctx) }
97+
assert.Panics[E](t, pingFn, "queries: driver does not implement driver.Pinger")
98+
9699
execFn := func() { _, _ = db.ExecContext(ctx, "") }
97100
assert.Panics[E](t, execFn, "queries: driver does not implement driver.ExecerContext")
98101

@@ -101,6 +104,9 @@ func TestInterceptor_unimplemented(t *testing.T) {
101104

102105
prepareFn := func() { _, _ = db.PrepareContext(ctx, "") }
103106
assert.Panics[E](t, prepareFn, "queries: driver does not implement driver.ConnPrepareContext")
107+
108+
beginFn := func() { _, _ = db.BeginTx(ctx, nil) }
109+
assert.Panics[E](t, beginFn, "queries: driver does not implement driver.ConnBeginTx")
104110
}
105111

106112
func TestInterceptor_driver(t *testing.T) {

tests/compose.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ services:
99
environment:
1010
- POSTGRES_PASSWORD=postgres
1111
- POSTGRES_DB=postgres
12-
1312
mysql:
1413
# https://hub.docker.com/_/mysql
1514
image: mysql

tests/go.mod

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ require (
77
github.com/jackc/pgx/v5 v5.7.3
88
go-simpler.org/assert v0.9.0
99
go-simpler.org/queries v0.1.0
10+
modernc.org/sqlite v1.38.0
1011
)
1112

1213
require (
1314
filippo.io/edwards25519 v1.1.0 // indirect
15+
github.com/dustin/go-humanize v1.0.1 // indirect
16+
github.com/google/uuid v1.6.0 // indirect
1417
github.com/jackc/pgpassfile v1.0.0 // indirect
1518
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
1619
github.com/jackc/puddle/v2 v2.2.2 // indirect
20+
github.com/mattn/go-isatty v0.0.20 // indirect
21+
github.com/ncruces/go-strftime v0.1.9 // indirect
22+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
1723
golang.org/x/crypto v0.31.0 // indirect
18-
golang.org/x/sync v0.10.0 // indirect
24+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
25+
golang.org/x/sync v0.14.0 // indirect
26+
golang.org/x/sys v0.33.0 // indirect
1927
golang.org/x/text v0.21.0 // indirect
28+
modernc.org/libc v1.65.10 // indirect
29+
modernc.org/mathutil v1.7.1 // indirect
30+
modernc.org/memory v1.11.0 // indirect
2031
)

tests/go.sum

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
7+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
68
github.com/go-sql-driver/mysql v1.9.0 h1:Y0zIbQXhQKmQgTp44Y1dp3wTXcn804QoTptLZT1vtvo=
79
github.com/go-sql-driver/mysql v1.9.0/go.mod h1:pDetrLJeA3oMujJuvXc8RJoasr589B6A9fwzD3QMrqw=
10+
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
11+
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
12+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
13+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
814
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
915
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
1016
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
@@ -13,8 +19,14 @@ github.com/jackc/pgx/v5 v5.7.3 h1:PO1wNKj/bTAwxSJnO1Z4Ai8j4magtqg2SLNjEDzcXQo=
1319
github.com/jackc/pgx/v5 v5.7.3/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
1420
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
1521
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
22+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
23+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
24+
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
25+
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
1626
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1727
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
29+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
1830
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1931
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2032
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -26,11 +38,44 @@ go-simpler.org/queries v0.1.0 h1:rxDyMajsLA1h2Hob09ByHbWkUeYKwWRI6w8REVmfSgE=
2638
go-simpler.org/queries v0.1.0/go.mod h1:qJiiU3cu3fXW7lD0TPx5NZernH/u2xNP6/14IGeeHwU=
2739
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
2840
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
29-
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
30-
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
41+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM=
42+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8=
43+
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
44+
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
45+
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
46+
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
47+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
49+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
3150
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
3251
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
52+
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
53+
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
3354
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3455
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3556
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3657
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
58+
modernc.org/cc/v4 v4.26.1 h1:+X5NtzVBn0KgsBCBe+xkDC7twLb/jNVj9FPgiwSQO3s=
59+
modernc.org/cc/v4 v4.26.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
60+
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=
61+
modernc.org/ccgo/v4 v4.28.0/go.mod h1:JygV3+9AV6SmPhDasu4JgquwU81XAKLd3OKTUDNOiKE=
62+
modernc.org/fileutil v1.3.3 h1:3qaU+7f7xxTUmvU1pJTZiDLAIoJVdUSSauJNHg9yXoA=
63+
modernc.org/fileutil v1.3.3/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
64+
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
65+
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
66+
modernc.org/libc v1.65.10 h1:ZwEk8+jhW7qBjHIT+wd0d9VjitRyQef9BnzlzGwMODc=
67+
modernc.org/libc v1.65.10/go.mod h1:StFvYpx7i/mXtBAfVOjaU0PWZOvIRoZSgXhrwXzr8Po=
68+
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
69+
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
70+
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
71+
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
72+
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
73+
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
74+
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
75+
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
76+
modernc.org/sqlite v1.38.0 h1:+4OrfPQ8pxHKuWG4md1JpR/EYAh3Md7TdejuuzE7EUI=
77+
modernc.org/sqlite v1.38.0/go.mod h1:1Bj+yES4SVvBZ4cBOpVZ6QgesMCKpJZDq0nxYzOpmNE=
78+
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
79+
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
80+
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
81+
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

tests/integration_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import (
1212
"go-simpler.org/assert"
1313
. "go-simpler.org/assert/EF"
1414
"go-simpler.org/queries"
15+
"modernc.org/sqlite"
1516
)
1617

1718
var DBs = map[string]struct {
1819
driver driver.Driver
1920
dsn string
2021
}{
21-
"postgres": {pgx.GetDefaultDriver(), "postgres://postgres:postgres@localhost:5432/postgres"},
22-
"mysql": {new(mysql.MySQLDriver), "root:root@tcp(localhost:3306)/mysql?parseTime=true"},
22+
"postgres": {pgx.GetDefaultDriver(), "postgres://postgres:postgres@localhost:5432/postgres"}, // https://github.com/jackc/pgx
23+
"mysql": {new(mysql.MySQLDriver), "root:root@tcp(localhost:3306)/mysql?parseTime=true"}, // https://github.com/go-sql-driver/mysql
24+
"sqlite": {new(sqlite.Driver), "test.sqlite"}, // https://gitlab.com/cznic/sqlite
2325
}
2426

2527
type User struct {
@@ -106,7 +108,7 @@ func TestIntegration(t *testing.T) {
106108
assert.Equal[E](t, user.Name, TableUsers[0].Name)
107109

108110
var i int
109-
for user, err := range queries.Query[User](ctx, queryer, "SELECT id, name, created_at FROM users") {
111+
for user, err := range queries.Query[User](ctx, queryer, "SELECT id, name, created_at FROM users ORDER BY id") {
110112
assert.NoErr[F](t, err)
111113
assert.Equal[E](t, user.ID, TableUsers[i].ID)
112114
assert.Equal[E](t, user.Name, TableUsers[i].Name)
@@ -140,7 +142,7 @@ func migrate(ctx context.Context, db *sql.DB) error {
140142
args []any
141143
}
142144
migrations := []migration{
143-
{"CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name TEXT, created_at TIMESTAMP DEFAULT NOW())", nil},
145+
{"CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)", nil},
144146
}
145147

146148
var args []any
@@ -150,9 +152,9 @@ func migrate(ctx context.Context, db *sql.DB) error {
150152

151153
switch db.Driver().(type) {
152154
case *pgx.Driver:
153-
migrations = append(migrations, migration{"INSERT INTO users (id, name) VALUES (%$, %$), (%$, %$), (%$, %$) ON CONFLICT DO NOTHING", args})
154-
case *mysql.MySQLDriver:
155-
migrations = append(migrations, migration{"INSERT IGNORE INTO users (id, name) VALUES (%?, %?), (%?, %?), (%?, %?)", args})
155+
migrations = append(migrations, migration{"INSERT INTO users (id, name) VALUES (%$, %$), (%$, %$), (%$, %$)", args})
156+
case *mysql.MySQLDriver, *sqlite.Driver:
157+
migrations = append(migrations, migration{"INSERT INTO users (id, name) VALUES (%?, %?), (%?, %?), (%?, %?)", args})
156158
default:
157159
panic("unreachable")
158160
}

0 commit comments

Comments
 (0)