Skip to content

Commit ae05c66

Browse files
committed
Integration test suite (in separate module). ⤵️
..lest we pollute the main module's dependency listing. This suite utilizes an in-memory sqlite database to avoid any need for a running database server.
1 parent 5a0fb45 commit ae05c66

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test: fmt
44
GORACE="atexit_sleep_ms=50" go test -timeout=1s -race -covermode=atomic ./...
5+
GORACE="atexit_sleep_ms=50" go test -count=1 github.com/smarty/sqldb/integration
56

67
fmt:
78
go mod tidy && go fmt ./...

go.work

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.25
2+
3+
use (
4+
integration
5+
.
6+
)

integration/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/smarty/sqldb/integration
2+
3+
go 1.25
4+
5+
require (
6+
github.com/mattn/go-sqlite3 v1.14.32
7+
github.com/smarty/gunit/v2 v2.0.0-20250910224800-b24d6a1628bf
8+
github.com/smarty/sqldb/v3 v3.0.0
9+
)
10+
11+
replace github.com/smarty/sqldb/v3 => ../

integration/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
2+
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
3+
github.com/smarty/gunit/v2 v2.0.0-20250910224800-b24d6a1628bf h1:2z0AVf/sIEX+34bp8O/elAQwPoPwMuC5pXjZNqgbuuY=
4+
github.com/smarty/gunit/v2 v2.0.0-20250910224800-b24d6a1628bf/go.mod h1:8zgLMlQPLDFO5SVz7TrnUS1ADFiHHMChB8zPIQ4R4Ys=

integration/integration_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package integration
2+
3+
import (
4+
"database/sql"
5+
"testing"
6+
7+
"github.com/smarty/gunit/v2"
8+
"github.com/smarty/gunit/v2/better"
9+
"github.com/smarty/gunit/v2/should"
10+
"github.com/smarty/sqldb/v3"
11+
12+
_ "github.com/mattn/go-sqlite3"
13+
)
14+
15+
const CreateInsert = `
16+
17+
DROP TABLE IF EXISTS sqldb_integration_test;
18+
19+
CREATE TABLE sqldb_integration_test (
20+
id INTEGER PRIMARY KEY AUTOINCREMENT,
21+
name TEXT NOT NULL
22+
);
23+
24+
INSERT INTO sqldb_integration_test (name) VALUES (?);
25+
INSERT INTO sqldb_integration_test (name) VALUES (?);
26+
INSERT INTO sqldb_integration_test (name) VALUES (?);
27+
INSERT INTO sqldb_integration_test (name) VALUES (?);
28+
`
29+
const query = `
30+
SELECT id, name
31+
FROM sqldb_integration_test;
32+
`
33+
34+
func Test(t *testing.T) {
35+
db, err := sql.Open("sqlite3", ":memory:")
36+
gunit.So(t, err, better.BeNil)
37+
38+
tx, err := db.BeginTx(t.Context(), nil)
39+
gunit.So(t, err, better.BeNil)
40+
41+
count, err := sqldb.ExecuteStatements(t.Context(), tx, CreateInsert,
42+
"a",
43+
"b",
44+
"c",
45+
"d",
46+
)
47+
gunit.So(t, err, better.BeNil)
48+
gunit.So(t, count, should.Equal, 4)
49+
50+
rows, err := tx.QueryContext(t.Context(), query)
51+
gunit.So(t, err, should.BeNil)
52+
53+
records := map[int]string{}
54+
err = sqldb.BindAll(rows, err, func(scanner sqldb.Scanner) error {
55+
var id int
56+
var name string
57+
defer func() { records[id] = name }()
58+
return scanner.Scan(&id, &name)
59+
})
60+
gunit.So(t, err, better.BeNil)
61+
gunit.So(t, records, should.Equal, map[int]string{
62+
1: "a",
63+
2: "b",
64+
3: "c",
65+
4: "d",
66+
})
67+
68+
err = tx.Rollback()
69+
gunit.So(t, err, better.BeNil)
70+
}

0 commit comments

Comments
 (0)