Files
ai-agent/internal/bootstrap/db_test.go
T
t 18c9354095 refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。

- 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。

- 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。

- 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
2026-08-28 22:23:13 +08:00

80 lines
2.3 KiB
Go

package bootstrap
import (
"testing"
"code.tczkiot.com/wlw/ai-agent/internal/models"
"code.tczkiot.com/wlw/ai-agent/internal/pkg/config"
"github.com/glebarez/sqlite"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func TestNewDialector(t *testing.T) {
t.Parallel()
cases := []struct {
dbType string
want string
}{
{dbType: "postgres", want: "postgres"},
{dbType: "postgresql", want: "postgres"},
{dbType: " PostgreSQL ", want: "postgres"},
}
for _, tt := range cases {
t.Run(tt.dbType, func(t *testing.T) {
t.Parallel()
dialector, err := newDialector(config.DBConfig{Type: tt.dbType, DSN: "postgres-dsn"})
if err != nil {
t.Fatalf("newDialector() error = %v", err)
}
if got := dialector.Name(); got != tt.want {
t.Fatalf("dialector.Name() = %q, want %q", got, tt.want)
}
})
}
}
func TestNewDialectorRejectsRemovedAndUnsupportedTypes(t *testing.T) {
t.Parallel()
for _, dbType := range []string{"sqlite", "mysql", "oracle", ""} {
if _, err := newDialector(config.DBConfig{Type: dbType}); err == nil {
t.Fatalf("newDialector(%q) error = nil, want unsupported type error", dbType)
}
}
}
func TestUseDatabaseSharesConnectionWithoutChangingHostNaming(t *testing.T) {
// SQLite remains only as a lightweight in-memory unit-test fixture. Runtime
// database initialization accepts PostgreSQL exclusively.
host, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
NamingStrategy: schema.NamingStrategy{TablePrefix: "iot_"},
})
if err != nil {
t.Fatalf("gorm.Open() error = %v", err)
}
hostTable := host.NamingStrategy.TableName("Conversation")
if err := UseDatabase(host, "iot_ai_"); err != nil {
t.Fatalf("UseDatabase() error = %v", err)
}
moduleDB := sqls.DB()
statement := &gorm.Statement{DB: moduleDB}
if err := statement.Parse(&models.Conversation{}); err != nil {
t.Fatalf("Statement.Parse() error = %v", err)
}
if statement.Schema.Table != "iot_ai_conversation" {
t.Fatalf("module table=%q want iot_ai_conversation", statement.Schema.Table)
}
if got := host.NamingStrategy.TableName("Conversation"); got != hostTable {
t.Fatalf("host table changed from %q to %q", hostTable, got)
}
if moduleDB.ConnPool != host.ConnPool {
t.Fatal("module database does not share the host connection pool")
}
}