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
3 changed files with 2929 additions and 2810 deletions

View File

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