2026-04-09 10:01:23 +08:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2026-08-20 21:46:55 +08:00
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/config"
|
2026-08-28 22:23:13 +08:00
|
|
|
|
|
|
|
|
"github.com/glebarez/sqlite"
|
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"gorm.io/gorm/schema"
|
2026-04-09 10:01:23 +08:00
|
|
|
)
|
|
|
|
|
|
2026-08-20 21:46:55 +08:00
|
|
|
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()
|
2026-08-28 22:23:13 +08:00
|
|
|
dialector, err := newDialector(config.DBConfig{Type: tt.dbType, DSN: "postgres-dsn"})
|
2026-08-20 21:46:55 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
func TestNewDialectorRejectsRemovedAndUnsupportedTypes(t *testing.T) {
|
2026-08-20 21:46:55 +08:00
|
|
|
t.Parallel()
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
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)
|
|
|
|
|
}
|
2026-08-20 21:46:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
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)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
hostTable := host.NamingStrategy.TableName("Conversation")
|
2026-04-09 10:01:23 +08:00
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
if err := UseDatabase(host, "iot_ai_"); err != nil {
|
|
|
|
|
t.Fatalf("UseDatabase() error = %v", err)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
moduleDB := sqls.DB()
|
|
|
|
|
statement := &gorm.Statement{DB: moduleDB}
|
|
|
|
|
if err := statement.Parse(&models.Conversation{}); err != nil {
|
|
|
|
|
t.Fatalf("Statement.Parse() error = %v", err)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
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")
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
}
|