feat(ci): enable unit test for backend (#552)

This commit is contained in:
IPender 2025-08-15 17:33:23 +08:00 committed by GitHub
parent f040a511e4
commit 18e45b333f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 2929 additions and 2810 deletions

121
.github/workflows/ci@backend.yml vendored Normal file
View File

@ -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

View File

@ -20,8 +20,6 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"strings"
"testing" "testing"
"time" "time"
@ -38,9 +36,6 @@ import (
func setupTestDB(t *testing.T) (*gorm.DB, rdb.RDB) { 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" 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)) db, err := gorm.Open(mysql.Open(dsn))
assert.NoError(t, err) assert.NoError(t, err)
@ -51,7 +46,7 @@ func setupTestDB(t *testing.T) (*gorm.DB, rdb.RDB) {
return db, NewService(db, idGen) 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 { for _, tableName := range tableNames {
db.WithContext(context.Background()).Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName)) 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) { func TestAlterTable(t *testing.T) {
db, svc := setupTestDB(t) db, svc := setupTestDB(t)
defer cleanupTestDB(t, db, "test_table") defer cleanupTestDB(t, db, "test_table")
@ -253,10 +247,9 @@ func TestInsertData(t *testing.T) {
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_insert_table ( 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, name VARCHAR(255) NOT NULL,
age INT, age INT
PRIMARY KEY (id)
) )
`).Error `).Error
assert.NoError(t, err, "Failed to create test table") assert.NoError(t, err, "Failed to create test table")
@ -315,11 +308,10 @@ func TestUpdateData(t *testing.T) {
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_update_table ( 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, name VARCHAR(255) NOT NULL,
age INT, age INT,
status VARCHAR(20) DEFAULT 'active', status VARCHAR(20) DEFAULT 'active'
PRIMARY KEY (id)
) )
`).Error `).Error
assert.NoError(t, err, "Failed to create test table") assert.NoError(t, err, "Failed to create test table")
@ -370,10 +362,9 @@ func TestDeleteData(t *testing.T) {
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_delete_table ( 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, name VARCHAR(255) NOT NULL,
age INT, age INT
PRIMARY KEY (id)
) )
`).Error `).Error
assert.NoError(t, err, "Failed to create test table") assert.NoError(t, err, "Failed to create test table")
@ -415,14 +406,13 @@ func TestSelectData(t *testing.T) {
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_select_table ( 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, name VARCHAR(255) NOT NULL,
age BIGINT, age BIGINT,
status VARCHAR(20) DEFAULT 'active', status VARCHAR(20) DEFAULT 'active',
score FLOAT, score FLOAT,
score2 DOUBLE DEFAULT '90.5', score2 DOUBLE DEFAULT '90.5',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY (id)
) )
`).Error `).Error
assert.NoError(t, err, "Failed to create test table") assert.NoError(t, err, "Failed to create test table")
@ -524,17 +514,15 @@ func TestSelectData(t *testing.T) {
} }
func TestExecuteSQL(t *testing.T) { func TestExecuteSQL(t *testing.T) {
t.Run("success", func(t *testing.T) { t.Run("success", func(t *testing.T) {
db, svc := setupTestDB(t) db, svc := setupTestDB(t)
defer cleanupTestDB(t, db, "test_sql_table") defer cleanupTestDB(t, db, "test_sql_table")
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_sql_table ( 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, name VARCHAR(255) NOT NULL,
age INT, age INT
PRIMARY KEY (id)
) )
`).Error `).Error
assert.NoError(t, err, "Failed to create test table") assert.NoError(t, err, "Failed to create test table")
@ -580,10 +568,9 @@ func TestExecuteSQL(t *testing.T) {
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_sql_table ( 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, name VARCHAR(255) NOT NULL,
age INT, age INT
PRIMARY KEY (id)
) )
`).Error `).Error
assert.NoError(t, err, "Failed to create test table") assert.NoError(t, err, "Failed to create test table")
@ -616,11 +603,10 @@ func TestUpsertData(t *testing.T) {
err := db.Exec(` err := db.Exec(`
CREATE TABLE IF NOT EXISTS test_upsert_table ( 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, name VARCHAR(255) NOT NULL,
age INT, age INT,
status VARCHAR(20) DEFAULT 'active', status VARCHAR(20) DEFAULT 'active',
PRIMARY KEY (id),
UNIQUE KEY idx_name (name) UNIQUE KEY idx_name (name)
) )
`).Error `).Error