Skip to content

Commit 8f09f85

Browse files
committed
Setup github action as CI
1 parent 767aaea commit 8f09f85

File tree

3 files changed

+176
-6
lines changed

3 files changed

+176
-6
lines changed

.github/workflows/tests.yml

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches-ignore:
6+
- 'gh-pages'
7+
pull_request:
8+
branches-ignore:
9+
- 'gh-pages'
10+
11+
jobs:
12+
# Label of the container job
13+
sqlite:
14+
strategy:
15+
matrix:
16+
go: ['1.15']
17+
platform: [ubuntu-latest, macos-latest] # can not run in windows OS
18+
runs-on: ${{ matrix.platform }}
19+
20+
steps:
21+
- name: Set up Go 1.x
22+
uses: actions/setup-go@v2
23+
with:
24+
go-version: ${{ matrix.go }}
25+
26+
- name: Check out code into the Go module directory
27+
uses: actions/checkout@v2
28+
29+
- name: go mod pakcage cache
30+
uses: actions/cache@v2
31+
with:
32+
path: ~/go/pkg/mod
33+
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}
34+
35+
- name: Tests
36+
run: GORM_DIALECT=sqlite ./test_all.sh
37+
38+
mysql:
39+
strategy:
40+
matrix:
41+
dbversion: ['mysql:latest', 'mysql:5.7', 'mysql:5.6', 'mariadb:latest']
42+
go: ['1.15', '1.14', '1.13']
43+
platform: [ubuntu-latest]
44+
runs-on: ${{ matrix.platform }}
45+
46+
services:
47+
mysql:
48+
image: ${{ matrix.dbversion }}
49+
env:
50+
MYSQL_DATABASE: gorm
51+
MYSQL_USER: gorm
52+
MYSQL_PASSWORD: gorm
53+
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
54+
ports:
55+
- 9910:3306
56+
options: >-
57+
--health-cmd "mysqladmin ping -ugorm -pgorm"
58+
--health-interval 10s
59+
--health-start-period 10s
60+
--health-timeout 5s
61+
--health-retries 10
62+
63+
steps:
64+
- name: Set up Go 1.x
65+
uses: actions/setup-go@v2
66+
with:
67+
go-version: ${{ matrix.go }}
68+
69+
- name: Check out code into the Go module directory
70+
uses: actions/checkout@v2
71+
72+
73+
- name: go mod pakcage cache
74+
uses: actions/cache@v2
75+
with:
76+
path: ~/go/pkg/mod
77+
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}
78+
79+
- name: Tests
80+
run: GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True" ./test_all.sh
81+
82+
postgres:
83+
strategy:
84+
matrix:
85+
dbversion: ['postgres:latest', 'postgres:11', 'postgres:10']
86+
go: ['1.15', '1.14', '1.13']
87+
platform: [ubuntu-latest] # can not run in macOS and widnowsOS
88+
runs-on: ${{ matrix.platform }}
89+
90+
services:
91+
postgres:
92+
image: ${{ matrix.dbversion }}
93+
env:
94+
POSTGRES_PASSWORD: gorm
95+
POSTGRES_USER: gorm
96+
POSTGRES_DB: gorm
97+
TZ: Asia/Shanghai
98+
ports:
99+
- 9920:5432
100+
# Set health checks to wait until postgres has started
101+
options: >-
102+
--health-cmd pg_isready
103+
--health-interval 10s
104+
--health-timeout 5s
105+
--health-retries 5
106+
107+
steps:
108+
- name: Set up Go 1.x
109+
uses: actions/setup-go@v2
110+
with:
111+
go-version: ${{ matrix.go }}
112+
113+
- name: Check out code into the Go module directory
114+
uses: actions/checkout@v2
115+
116+
- name: go mod pakcage cache
117+
uses: actions/cache@v2
118+
with:
119+
path: ~/go/pkg/mod
120+
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}
121+
122+
- name: Tests
123+
run: GORM_DIALECT=postgres GORM_DSN="user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai" ./test_all.sh
124+
125+
sqlserver:
126+
strategy:
127+
matrix:
128+
go: ['1.15', '1.14', '1.13']
129+
platform: [ubuntu-latest] # can not run test in macOS and windows
130+
runs-on: ${{ matrix.platform }}
131+
132+
services:
133+
mssql:
134+
image: mcmoe/mssqldocker:latest
135+
env:
136+
ACCEPT_EULA: Y
137+
SA_PASSWORD: LoremIpsum86
138+
MSSQL_DB: gorm
139+
MSSQL_USER: gorm
140+
MSSQL_PASSWORD: LoremIpsum86
141+
ports:
142+
- 9930:1433
143+
options: >-
144+
--health-cmd="/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P LoremIpsum86 -l 30 -Q \"SELECT 1\" || exit 1"
145+
--health-start-period 10s
146+
--health-interval 10s
147+
--health-timeout 5s
148+
--health-retries 10
149+
150+
steps:
151+
- name: Set up Go 1.x
152+
uses: actions/setup-go@v2
153+
with:
154+
go-version: ${{ matrix.go }}
155+
156+
- name: Check out code into the Go module directory
157+
uses: actions/checkout@v2
158+
159+
- name: go mod pakcage cache
160+
uses: actions/cache@v2
161+
with:
162+
path: ~/go/pkg/mod
163+
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}
164+
165+
- name: Tests
166+
run: GORM_DIALECT=sqlserver GORM_DSN="sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" ./test_all.sh

main_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@ func init() {
2525

2626
func OpenTestConnection() (db *gorm.DB, err error) {
2727
dbDSN := os.Getenv("GORM_DSN")
28-
switch os.Getenv("GORM_DIALECT") {
28+
dialect := os.Getenv("GORM_DIALECT")
29+
switch dialect {
2930
case "mysql":
3031
log.Println("testing mysql...")
3132
if dbDSN == "" {
3233
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=UTC"
3334
}
3435
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
35-
case "postgres":
36-
log.Println("testing postgres...")
36+
case "postgres", "postgres_simple":
37+
log.Printf("testing %v...", dialect)
3738
if dbDSN == "" {
3839
dbDSN = "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
3940
}
40-
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{})
41-
// db, err = gorm.Open(postgres.New(postgres.Config{DSN: dbDSN, PreferSimpleProtocol: true}), &gorm.Config{})
41+
if dialect == "postgres" {
42+
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{})
43+
} else {
44+
db, err = gorm.Open(postgres.New(postgres.Config{DSN: dbDSN, PreferSimpleProtocol: true}), &gorm.Config{})
45+
}
4246
case "sqlserver":
4347
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
4448
// CREATE DATABASE gorm;

test_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -e
22

3-
dialects=("postgres" "mysql" "mssql" "sqlite")
3+
dialects=("postgres" "postgres_simple" "mysql" "mssql" "sqlite")
44

55
for dialect in "${dialects[@]}" ; do
66
GORM_DIALECT=${dialect} go test --tags "json1"

0 commit comments

Comments
 (0)