Skip to content

Commit 326225c

Browse files
committed
feat(tests): support pure-go sqlite driver
This tests the sqlite with/out CGO, updates the minimum tests go version to 1.18, and adds the latest go version `^1` to the sqlite CI tests. Related: go-gorm/sqlite#161
1 parent 2066138 commit 326225c

File tree

6 files changed

+126
-57
lines changed

6 files changed

+126
-57
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
sqlite:
1717
strategy:
1818
matrix:
19-
go: ['1.19', '1.18']
19+
go: ['^1', '1.19', '1.18']
2020
platform: [ubuntu-latest] # can not run in windows OS
2121
runs-on: ${{ matrix.platform }}
2222

tests/go.mod

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
module gorm.io/gorm/tests
22

3-
go 1.16
3+
go 1.18
4+
5+
replace gorm.io/driver/sqlite => github.com/aymanbagabas/sqlite v1.5.3-0.20230705235344-744e464ebb48
46

57
require (
68
github.com/google/uuid v1.3.0
7-
github.com/jackc/pgx/v5 v5.3.1 // indirect
89
github.com/jinzhu/now v1.1.5
9-
github.com/lib/pq v1.10.8
10-
github.com/mattn/go-sqlite3 v1.14.16 // indirect
11-
gorm.io/driver/mysql v1.5.0
12-
gorm.io/driver/postgres v1.5.0
13-
gorm.io/driver/sqlite v1.5.0
10+
github.com/lib/pq v1.10.9
11+
gorm.io/driver/mysql v1.5.1
12+
gorm.io/driver/postgres v1.5.2
13+
gorm.io/driver/sqlite v1.5.2
1414
gorm.io/driver/sqlserver v1.5.1
15-
gorm.io/gorm v1.25.1
15+
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
16+
)
17+
18+
require (
19+
github.com/dustin/go-humanize v1.0.1 // indirect
20+
github.com/go-sql-driver/mysql v1.7.1 // indirect
21+
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
22+
github.com/golang-sql/sqlexp v0.1.0 // indirect
23+
github.com/jackc/pgpassfile v1.0.0 // indirect
24+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
25+
github.com/jackc/pgx/v5 v5.4.1 // indirect
26+
github.com/jinzhu/inflection v1.0.0 // indirect
27+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
28+
github.com/mattn/go-isatty v0.0.19 // indirect
29+
github.com/mattn/go-sqlite3 v1.14.17 // indirect
30+
github.com/microsoft/go-mssqldb v1.3.0 // indirect
31+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
32+
golang.org/x/crypto v0.11.0 // indirect
33+
golang.org/x/mod v0.12.0 // indirect
34+
golang.org/x/sys v0.10.0 // indirect
35+
golang.org/x/text v0.11.0 // indirect
36+
golang.org/x/tools v0.11.0 // indirect
37+
lukechampine.com/uint128 v1.3.0 // indirect
38+
modernc.org/cc/v3 v3.41.0 // indirect
39+
modernc.org/ccgo/v3 v3.16.14 // indirect
40+
modernc.org/libc v1.24.1 // indirect
41+
modernc.org/mathutil v1.6.0 // indirect
42+
modernc.org/memory v1.6.0 // indirect
43+
modernc.org/opt v0.1.3 // indirect
44+
modernc.org/sqlite v1.23.1 // indirect
45+
modernc.org/strutil v1.1.3 // indirect
46+
modernc.org/token v1.1.0 // indirect
1647
)
1748

1849
replace gorm.io/gorm => ../

tests/tests_all.sh

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,71 @@
33
dialects=("sqlite" "mysql" "postgres" "sqlserver" "tidb")
44

55
if [[ $(pwd) == *"gorm/tests"* ]]; then
6-
cd ..
6+
cd ..
77
fi
88

9-
if [ -d tests ]
10-
then
11-
cd tests
12-
go get -u -t ./...
13-
go mod download
14-
go mod tidy
15-
cd ..
9+
if [ -d tests ]; then
10+
cd tests
11+
go get -u -t ./...
12+
go mod download
13+
go mod tidy
14+
cd ..
1615
fi
1716

1817
# SqlServer for Mac M1
1918
if [[ -z $GITHUB_ACTION ]]; then
20-
if [ -d tests ]
21-
then
22-
cd tests
23-
if [[ $(uname -a) == *" arm64" ]]; then
24-
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker-compose start || true
25-
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
26-
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" > /dev/null || true
27-
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true
28-
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" > /dev/null || true
29-
else
30-
docker-compose start
31-
fi
32-
cd ..
33-
fi
19+
if [ -d tests ]; then
20+
cd tests
21+
if [[ $(uname -a) == *" arm64" ]]; then
22+
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker-compose start || true
23+
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
24+
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" >/dev/null || true
25+
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" >/dev/null || true
26+
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" >/dev/null || true
27+
else
28+
docker-compose start
29+
fi
30+
cd ..
31+
fi
3432
fi
3533

34+
for dialect in "${dialects[@]}"; do
35+
if [ "$GORM_DIALECT" = "" ] || [ "$GORM_DIALECT" = "${dialect}" ]; then
36+
echo "testing ${dialect}..."
3637

37-
for dialect in "${dialects[@]}" ; do
38-
if [ "$GORM_DIALECT" = "" ] || [ "$GORM_DIALECT" = "${dialect}" ]
39-
then
40-
echo "testing ${dialect}..."
41-
42-
if [ "$GORM_VERBOSE" = "" ]
43-
then
44-
GORM_DIALECT=${dialect} go test -race -count=1 ./...
45-
if [ -d tests ]
46-
then
47-
cd tests
48-
GORM_DIALECT=${dialect} go test -race -count=1 ./...
49-
cd ..
50-
fi
51-
else
52-
GORM_DIALECT=${dialect} go test -race -count=1 -v ./...
53-
if [ -d tests ]
54-
then
55-
cd tests
56-
GORM_DIALECT=${dialect} go test -race -count=1 -v ./...
57-
cd ..
58-
fi
59-
fi
60-
fi
38+
cmd="GORM_DIALECT=${dialect} go test"
39+
tags=("")
40+
41+
if [ "$GORM_DIALECT" = "sqlite" ]; then
42+
# Test SQLite pure-go driver
43+
tags+=("pure")
44+
fi
45+
46+
for tag in "${tags[@]}"; do
47+
echo "testing ${dialect} with tag '${tag}'"
48+
49+
if [ -n "$tag" ]; then
50+
cmd="$cmd -tags ${tag}"
51+
fi
52+
53+
cmd="$cmd -race -count=1 ./..."
54+
55+
56+
if [ "$GORM_VERBOSE" = "" ]; then
57+
eval $cmd
58+
if [ -d tests ]; then
59+
cd tests
60+
eval $cmd
61+
cd ..
62+
fi
63+
else
64+
eval $cmd
65+
if [ -d tests ]; then
66+
cd tests
67+
eval $cmd
68+
cd ..
69+
fi
70+
fi
71+
done
72+
fi
6173
done

tests/tests_cgo_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build cgo && !pure
2+
// +build cgo,!pure
3+
4+
package tests_test
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
var (
12+
sqliteDSN = filepath.Join(os.TempDir(), "gorm.db")
13+
)

tests/tests_pure_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !cgo || (cgo && pure)
2+
// +build !cgo cgo,pure
3+
4+
package tests_test
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
var (
12+
sqliteDSN = filepath.Join(os.TempDir(), "gorm.db?_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)")
13+
)

tests/tests_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"log"
55
"math/rand"
66
"os"
7-
"path/filepath"
87
"time"
98

109
"gorm.io/driver/mysql"
@@ -45,6 +44,7 @@ func init() {
4544
RunMigrations()
4645
if DB.Dialector.Name() == "sqlite" {
4746
DB.Exec("PRAGMA foreign_keys = ON")
47+
DB.Exec("PRAGMA busy_timeout = 5000")
4848
}
4949
}
5050
}
@@ -89,7 +89,7 @@ func OpenTestConnection(cfg *gorm.Config) (db *gorm.DB, err error) {
8989
db, err = gorm.Open(mysql.Open(dbDSN), cfg)
9090
default:
9191
log.Println("testing sqlite3...")
92-
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), cfg)
92+
db, err = gorm.Open(sqlite.Open(sqliteDSN), cfg)
9393
}
9494

9595
if err != nil {

0 commit comments

Comments
 (0)