49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
|
|
package aiagent
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
||
|
|
|
||
|
|
"github.com/glebarez/sqlite"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
"gorm.io/gorm/schema"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSyncSchemaCreatesEveryRegisteredTableWithModulePrefix(t *testing.T) {
|
||
|
|
hostDB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
|
||
|
|
NamingStrategy: schema.NamingStrategy{TablePrefix: "host_", SingularTable: true},
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("open database: %v", err)
|
||
|
|
}
|
||
|
|
if err := SyncSchema(hostDB, "iot_ai_"); err != nil {
|
||
|
|
t.Fatalf("SyncSchema() error = %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var tableNames []string
|
||
|
|
if err := hostDB.Raw("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'iot_ai_%'").Scan(&tableNames).Error; err != nil {
|
||
|
|
t.Fatalf("list AI Agent tables: %v", err)
|
||
|
|
}
|
||
|
|
if len(tableNames) != len(models.Models) {
|
||
|
|
t.Fatalf("AI Agent table count = %d, registered model count = %d", len(tableNames), len(models.Models))
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, forbidden := range []string{
|
||
|
|
"iot_ai_migration",
|
||
|
|
"iot_ai_user",
|
||
|
|
"iot_ai_admin",
|
||
|
|
"iot_ai_role",
|
||
|
|
"iot_ai_permission",
|
||
|
|
"iot_ai_token",
|
||
|
|
} {
|
||
|
|
var count int64
|
||
|
|
if err := hostDB.Raw("SELECT COUNT(1) FROM sqlite_master WHERE type = 'table' AND name = ?", forbidden).Scan(&count).Error; err != nil {
|
||
|
|
t.Fatalf("check forbidden table %s: %v", forbidden, err)
|
||
|
|
}
|
||
|
|
if count != 0 {
|
||
|
|
t.Fatalf("forbidden table %s must not be created", forbidden)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|