完善 PostgreSQL 仓储兼容测试

This commit is contained in:
xuans committed 2026-09-16 13:17:44 +08:00
1 parent eaf474c8e7
commit e007bdab1f
5 files changed
+105 -28

No files matched your search

@@ -3,6 +3,7 @@ package database
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"testing"
@@ -61,6 +62,16 @@ func TestMySQLMigrationStatementsRecognizeRecoverableChanges(t *testing.T) {
t.Errorf("可恢复的 MySQL 迁移语句未被识别:%q", item.statement)
}
}
func TestPostgresMigrationFilesAreEmbedded(t *testing.T) {
for version := 1; version <= latestMigrationVersion; version++ {
path := "migrations/postgres/" + fmt.Sprintf("%04d_%s.sql", version, migrationName(version))
content, err := migrationFiles.ReadFile(path)
if err != nil || len(content) == 0 {
t.Fatalf("PostgreSQL 迁移文件不可用:%s: %v", path, err)
}
}
}
func TestSQLiteUpgradeAddsServerValidationCommand(t *testing.T) {
dir := t.TempDir()
cfg := config.Default(dir).Database
+35 -2
View File
@@ -3,6 +3,7 @@ package database
import (
"context"
"path/filepath"
"strings"
"testing"
"time"
@@ -13,13 +14,45 @@ import (
func TestOpenRejectsUnknownDriver(t *testing.T) {
cfg := config.Default(t.TempDir()).Database
cfg.Driver = "postgres"
cfg.Driver = "unknown"
cfg.DSN = "unused"
if _, err := Open(context.Background(), cfg); err == nil {
t.Fatal("未知数据库驱动未被拒绝")
}
}
func TestNormalizePostgresDSNRequiresVerifiedTLS(t *testing.T) {
cases := []string{
"postgres://user:password@db.example.test:5432/scert",
"postgres://user:password@db.example.test:5432/scert?sslmode=disable",
"host=db.example.test port=5432 user=user password=password dbname=scert sslmode=require",
}
for _, dsn := range cases {
if _, err := normalizePostgresDSN(dsn); err == nil {
t.Fatalf("未拒绝不安全 PostgreSQL TLS 配置:%s", dsn)
}
}
valid := "postgres://user:password@db.example.test:5432/scert?sslmode=verify-full"
if _, err := normalizePostgresDSN(valid); err != nil {
t.Fatalf("有效 PostgreSQL TLS 配置被拒绝:%v", err)
}
}
func TestSanitizeDSNRedactsPostgresPassword(t *testing.T) {
for _, item := range []struct {
name string
dsn string
}{
{name: "URL", dsn: "postgres://user:secret@db.example.test:5432/scert?sslmode=verify-full"},
{name: "关键字", dsn: "host=db.example.test user=user password=secret dbname=scert sslmode=verify-full"},
} {
sanitized := SanitizeDSN("postgres", item.dsn)
if sanitized == "" || strings.Contains(sanitized, "secret") {
t.Fatalf("%s PostgreSQL DSN 未正确脱敏:%s", item.name, sanitized)
}
}
}
func TestNormalizeMySQLDSNEnablesTimeParsing(t *testing.T) {
dsn, err := normalizeMySQLDSN("user:password@tcp(127.0.0.1:3306)/scert?charset=utf8mb4")
if err != nil {
@@ -62,7 +95,7 @@ func TestSQLiteMigrationIsIdempotentAndCreatesBaseTables(t *testing.T) {
if tables != 7 {
t.Fatalf("基础数据表数量不正确:%d", tables)
}
if err := Migrate(context.Background(), db.DB, "postgres"); err == nil {
if err := Migrate(context.Background(), db.DB, "unknown"); err == nil {
t.Fatal("未知迁移驱动未被拒绝")
}
}