@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user