Skip to content

Commit

Permalink
test: update unit test github workflow
Browse files Browse the repository at this point in the history
test: add test config

test: add test steps for mysql

test: update go version

test: remove verbose test

test: fix dsn

tests: fix dsn

tests: add test case
  • Loading branch information
tr1v3r committed Aug 19, 2022
1 parent 8a4398b commit 6bd5b69
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ updates:
directory: /
schedule:
interval: weekly
- package-ecosystem: gomod
directory: /tests
schedule:
interval: weekly
47 changes: 46 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,49 @@ jobs:
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.sum') }}

- name: Tests
run: go test
run: go test ./...

mysql:
strategy:
matrix:
dbversion: ['mysql:latest', 'mysql:5.7', 'mariadb:latest']
go: ['1.19', '1.18', '1.17', '1.16']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

services:
mysql:
image: ${{ matrix.dbversion }}
env:
MYSQL_DATABASE: gen
MYSQL_USER: gen
MYSQL_PASSWORD: gen
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
ports:
- 9910:3306
options: >-
--health-cmd "mysqladmin ping -ugen -pgen"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: Check out code into the Go module directory
uses: actions/checkout@v3


- name: go mod package cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}

- name: Tests
run: GITHUB_ACTION=true GORM_DIALECT=mysql GORM_DSN="gen:gen@tcp(localhost:9910)/gen?charset=utf8&parseTime=True" ./tests/test.sh

5 changes: 5 additions & 0 deletions field/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ func TestExpr_Build(t *testing.T) {
ExpectedVars: []interface{}{3},
Result: "`age`<<?",
},
{
Expr: field.NewInt("", "age").Add(1).Mul(2).Div(3),
ExpectedVars: []interface{}{1, 2, 3},
Result: "((`age`+?)*?)/?",
},
// ======================== float ========================
{
Expr: field.NewFloat64("", "score").Add(3.0),
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go.sum
10 changes: 10 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Test Guide

```bash
cd tests
# prepare test databases
docker-compose up

# run all tests
./tests_all.sh
```
12 changes: 12 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'

services:
mysql:
image: 'mysql/mysql-server:latest'
ports:
- 9910:3306
environment:
- MYSQL_DATABASE=gen
- MYSQL_USER=gen
- MYSQL_PASSWORD=gen
- MYSQL_RANDOM_ROOT_PASSWORD="yes"
10 changes: 10 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module gorm.io/gen/tests

go 1.16

require (
gorm.io/driver/mysql v1.3.6
gorm.io/gorm v1.23.8
)

replace gorm.io/gen => ../
59 changes: 59 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash -e

# dialects=("sqlite" "mysql" "postgres" "sqlserver")
dialects=("mysql")

if [[ $(pwd) == *"gen/tests"* ]]; then
cd ..
fi

if [ -d tests ]
then
cd tests
go get -u -t ./...
go mod download
go mod tidy
cd ..
fi

# SqlServer for Mac M1
if [[ -z $GITHUB_ACTION ]]; then
if [ -d tests ]
then
cd tests
if [[ $(uname -a) == *" arm64" ]]; then
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker-compose start || true
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gen') IS NULL CREATE DATABASE gen" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gen') IS NULL CREATE LOGIN gen WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gen') IS NULL CREATE USER gen FROM LOGIN gen; ALTER SERVER ROLE sysadmin ADD MEMBER [gen];" > /dev/null || true
else
docker-compose start
fi
cd ..
fi
fi

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

if [ "$GEN_VERBOSE" = "" ]
then
if [ -d tests ]
then
cd tests
GORM_DIALECT=${dialect} go test -race -count=1 ./...
cd ..
fi
else
if [ -d tests ]
then
cd tests
GORM_DIALECT=${dialect} go test -race -count=1 -v ./...
cd ..
fi
fi
fi
done
22 changes: 22 additions & 0 deletions tests/tests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests_test

import (
"fmt"
"os"

"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func init() {
dsn := os.Getenv("GORM_DSN")
if dsn == "" {
dsn = "gen:gen@tcp(localhost:9910)/gen?charset=utf8&parseTime=True&loc=Local"
}

db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(fmt.Errorf("open mysql fail: %w", err))
}
_ = db
}

0 comments on commit 6bd5b69

Please sign in to comment.