仓库自包含化:Docker/CI/文档/规范入库,路由分组拆分
CI / test (push) Successful in 15s

This commit is contained in:
Wang Defa
2026-07-09 19:18:04 +08:00
parent b9a3e97e84
commit 3e0389c1e9
83 changed files with 20241 additions and 248 deletions
+52
View File
@@ -0,0 +1,52 @@
package database
import (
"path/filepath"
"strings"
"testing"
)
func TestBuildDialector(t *testing.T) {
tests := []struct {
name string
driver string
wantName string
wantErr string
}{
{name: "默认空驱动走 sqlite", driver: "", wantName: "sqlite"},
{name: "显式 sqlite", driver: "sqlite", wantName: "sqlite"},
{name: "mysql", driver: "mysql", wantName: "mysql"},
{name: "postgres", driver: "postgres", wantName: "postgres"},
{name: "未知驱动报错", driver: "oracle", wantErr: "unsupported DB_DRIVER"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d, err := buildDialector(tt.driver, "user:pass@tcp(127.0.0.1)/db", "test.db")
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want contains %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("buildDialector: %v", err)
}
if got := d.Name(); got != tt.wantName {
t.Fatalf("dialector name = %q, want %q", got, tt.wantName)
}
})
}
}
func TestOpenSQLiteMigrates(t *testing.T) {
path := filepath.Join(t.TempDir(), "test.db")
db, err := Open("sqlite", "", path)
if err != nil {
t.Fatalf("Open sqlite: %v", err)
}
for _, table := range []string{"users", "oci_configs", "ai_channels", "log_events"} {
if !db.Migrator().HasTable(table) {
t.Fatalf("table %s not migrated", table)
}
}
}