feat(ci): enable unit test for backend (#552)
This commit is contained in:
parent
f040a511e4
commit
18e45b333f
|
|
@ -0,0 +1,121 @@
|
|||
name: Backend Tests
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- 'backend/**'
|
||||
- 'docker/atlas/**'
|
||||
- '.github/workflows/ci@backend.yml'
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- 'backend/**'
|
||||
- 'docker/atlas/**'
|
||||
- '.github/workflows/ci@backend.yml'
|
||||
# Allows you to run this workflow manually from the Actions tab
|
||||
workflow_dispatch:
|
||||
|
||||
|
||||
env:
|
||||
DEFAULT_GO_VERSION: "1.24"
|
||||
|
||||
jobs:
|
||||
backend-unit-test:
|
||||
name: backend-unit-test
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
repository-projects: write
|
||||
env:
|
||||
COVERAGE_FILE: coverage.out
|
||||
BREAKDOWN_FILE: main.breakdown
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.DEFAULT_GO_VERSION }}
|
||||
|
||||
# - name: Shutdown Ubuntu MySQL
|
||||
# run: service mysql stop
|
||||
|
||||
- name: Set Up MySQL
|
||||
uses: mirromutth/mysql-action@v1.1
|
||||
with:
|
||||
host port: 3306
|
||||
container port: 3306
|
||||
character set server: 'utf8mb4'
|
||||
collation server: 'utf8mb4_general_ci'
|
||||
mysql version: '8.4.5'
|
||||
mysql database: 'opencoze'
|
||||
mysql root password: 'root'
|
||||
|
||||
- name: Verify MySQL Startup
|
||||
run: |
|
||||
echo "Waiting for MySQL to be ready..."
|
||||
for i in {1..60}; do
|
||||
if cat /proc/net/tcp | grep 0CEA; then
|
||||
echo "MySQL port 3306 is listening!"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for MySQL port... ($i/60)"
|
||||
sleep 1
|
||||
done
|
||||
echo "Final verification: MySQL port 3306 is accessible"
|
||||
|
||||
- name: Install MySQL Client
|
||||
run: sudo apt-get update && sudo apt-get install -y mysql-client
|
||||
|
||||
- name: Initialize Database
|
||||
run: mysql -h 127.0.0.1 -P 3306 -u root -proot opencoze < docker/volumes/mysql/schema.sql
|
||||
|
||||
- name: Run Go Test
|
||||
run: |
|
||||
modules=`find . -name "go.mod" -exec dirname {} \;`
|
||||
echo $modules
|
||||
list=""
|
||||
coverpkg=""
|
||||
if [[ ! -f "go.work" ]];then go work init;fi
|
||||
for module in $modules; do go work use $module; list=$module"/... "$list; coverpkg=$module"/...,"$coverpkg; done
|
||||
go work sync
|
||||
go test -race -v -coverprofile=${{ env.COVERAGE_FILE }} -gcflags="all=-l -N" -coverpkg=$coverpkg $list
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v5
|
||||
with:
|
||||
name: coze-studio-backend
|
||||
env_vars: GOLANG,Coze-Studio,BACKEND
|
||||
fail_ci_if_error: 'false'
|
||||
files: ${{ env.COVERAGE_FILE }}
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
|
||||
- name: Shutdown MySQL
|
||||
if: always()
|
||||
continue-on-error: true
|
||||
run: docker rm -f $(docker ps -q --filter "ancestor=mysql:8.4.5")
|
||||
benchmark-test:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
repository-projects: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.DEFAULT_GO_VERSION }}
|
||||
|
||||
- name: Run Go Benchmark
|
||||
run: |
|
||||
modules=`find . -name "go.mod" -exec dirname {} \;`
|
||||
echo $modules
|
||||
list=""
|
||||
coverpkg=""
|
||||
if [[ ! -f "go.work" ]];then go work init;fi
|
||||
for module in $modules; do go work use $module; list=$module"/... "$list; coverpkg=$module"/...,"$coverpkg; done
|
||||
go work sync
|
||||
go test -race -v -bench=. -benchmem -run=none -gcflags="all=-l -N" $list
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -20,8 +20,6 @@ import (
|
|||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -38,9 +36,6 @@ import (
|
|||
|
||||
func setupTestDB(t *testing.T) (*gorm.DB, rdb.RDB) {
|
||||
dsn := "root:root@tcp(127.0.0.1:3306)/opencoze?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
if os.Getenv("CI_JOB_NAME") != "" {
|
||||
dsn = strings.ReplaceAll(dsn, "127.0.0.1", "mysql")
|
||||
}
|
||||
db, err := gorm.Open(mysql.Open(dsn))
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
@ -51,7 +46,7 @@ func setupTestDB(t *testing.T) (*gorm.DB, rdb.RDB) {
|
|||
return db, NewService(db, idGen)
|
||||
}
|
||||
|
||||
func cleanupTestDB(t *testing.T, db *gorm.DB, tableNames ...string) {
|
||||
func cleanupTestDB(_ *testing.T, db *gorm.DB, tableNames ...string) {
|
||||
for _, tableName := range tableNames {
|
||||
db.WithContext(context.Background()).Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName))
|
||||
}
|
||||
|
|
@ -126,7 +121,6 @@ func TestCreateTable(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAlterTable(t *testing.T) {
|
||||
|
||||
db, svc := setupTestDB(t)
|
||||
defer cleanupTestDB(t, db, "test_table")
|
||||
|
||||
|
|
@ -253,10 +247,9 @@ func TestInsertData(t *testing.T) {
|
|||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_insert_table (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age INT,
|
||||
PRIMARY KEY (id)
|
||||
age INT
|
||||
)
|
||||
`).Error
|
||||
assert.NoError(t, err, "Failed to create test table")
|
||||
|
|
@ -315,11 +308,10 @@ func TestUpdateData(t *testing.T) {
|
|||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_update_table (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age INT,
|
||||
status VARCHAR(20) DEFAULT 'active',
|
||||
PRIMARY KEY (id)
|
||||
status VARCHAR(20) DEFAULT 'active'
|
||||
)
|
||||
`).Error
|
||||
assert.NoError(t, err, "Failed to create test table")
|
||||
|
|
@ -370,10 +362,9 @@ func TestDeleteData(t *testing.T) {
|
|||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_delete_table (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age INT,
|
||||
PRIMARY KEY (id)
|
||||
age INT
|
||||
)
|
||||
`).Error
|
||||
assert.NoError(t, err, "Failed to create test table")
|
||||
|
|
@ -415,14 +406,13 @@ func TestSelectData(t *testing.T) {
|
|||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_select_table (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age BIGINT,
|
||||
status VARCHAR(20) DEFAULT 'active',
|
||||
score FLOAT,
|
||||
score2 DOUBLE DEFAULT '90.5',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`).Error
|
||||
assert.NoError(t, err, "Failed to create test table")
|
||||
|
|
@ -524,17 +514,15 @@ func TestSelectData(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteSQL(t *testing.T) {
|
||||
|
||||
t.Run("success", func(t *testing.T) {
|
||||
db, svc := setupTestDB(t)
|
||||
defer cleanupTestDB(t, db, "test_sql_table")
|
||||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_sql_table (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age INT,
|
||||
PRIMARY KEY (id)
|
||||
age INT
|
||||
)
|
||||
`).Error
|
||||
assert.NoError(t, err, "Failed to create test table")
|
||||
|
|
@ -580,10 +568,9 @@ func TestExecuteSQL(t *testing.T) {
|
|||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_sql_table (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age INT,
|
||||
PRIMARY KEY (id)
|
||||
age INT
|
||||
)
|
||||
`).Error
|
||||
assert.NoError(t, err, "Failed to create test table")
|
||||
|
|
@ -616,11 +603,10 @@ func TestUpsertData(t *testing.T) {
|
|||
|
||||
err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS test_upsert_table (
|
||||
id INT NOT NULL,
|
||||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
age INT,
|
||||
status VARCHAR(20) DEFAULT 'active',
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY idx_name (name)
|
||||
)
|
||||
`).Error
|
||||
|
|
|
|||
Loading…
Reference in New Issue