update
This commit is contained in:
46
internal/repository/testhelper_integration_test.go
Normal file
46
internal/repository/testhelper_integration_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
//go:build integration
|
||||
|
||||
// 集成测试的公共辅助:连接真实 PostgreSQL + pgvector。
|
||||
//
|
||||
// 运行方式(先起库,见 scripts/pgvector):
|
||||
//
|
||||
// docker compose -f scripts/pgvector/docker-compose.yml up -d
|
||||
// go test -tags integration ./internal/repository/ -run <TestName> -v
|
||||
//
|
||||
// 默认 `go test ./...` 不会编译本文件(build tag 隔离),CI 无库环境不受影响。
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"fashionapi/internal/config"
|
||||
"fashionapi/internal/database"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// testDB 连真实库并校验所需结构已就绪。
|
||||
//
|
||||
// 结构不再由 AutoMigrate 自动创建(已移除),须先自行准备好库,例如:
|
||||
//
|
||||
// ./bin/dbtool dump -out db_dump.sql
|
||||
// psql -U fashion -d fashion -v ON_ERROR_STOP=1 -f db_dump.sql
|
||||
//
|
||||
// 这里只做存在性校验并给出明确提示,避免后续查询报出难懂的错。
|
||||
func testDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
cfg, err := config.Load("")
|
||||
if err != nil {
|
||||
t.Fatalf("加载配置失败: %v", err)
|
||||
}
|
||||
db, err := database.New(cfg.Database)
|
||||
if err != nil {
|
||||
t.Fatalf("连接数据库失败: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.Close(db) })
|
||||
|
||||
if err := db.Exec("SELECT 1 FROM brand_runway_images LIMIT 1").Error; err != nil {
|
||||
t.Fatalf("brand_runway_images 不可用(库结构未就绪?先 dbtool dump 再 psql -f 灌库): %v", err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
Reference in New Issue
Block a user