improve postgres support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-10 19:40:20 +11:00
parent e0cbc20140
commit 3e3d8c2eb0
7 changed files with 743 additions and 9 deletions

View File

@@ -0,0 +1,78 @@
package db
import "testing"
func TestResolveDriver(t *testing.T) {
tests := []struct {
name string
configuredDriver string
dsn string
wantDriver string
wantInferred bool
wantErr bool
}{
{
name: "explicit postgres uri",
configuredDriver: "postgres",
dsn: "postgres://user:pass@localhost:5432/vctp?sslmode=disable",
wantDriver: "postgres",
},
{
name: "postgresql alias",
configuredDriver: "postgresql",
dsn: "postgres://user:pass@localhost:5432/vctp?sslmode=disable",
wantDriver: "postgres",
},
{
name: "infer postgres uri",
dsn: "postgres://user:pass@localhost:5432/vctp?sslmode=disable",
wantDriver: "postgres",
wantInferred: true,
},
{
name: "infer postgres key value dsn",
dsn: "host=localhost port=5432 user=postgres password=secret dbname=vctp sslmode=disable",
wantDriver: "postgres",
wantInferred: true,
},
{
name: "default sqlite",
dsn: "/var/lib/vctp/db.sqlite3",
wantDriver: "sqlite",
},
{
name: "sqlite alias",
configuredDriver: "sqlite3",
dsn: "/var/lib/vctp/db.sqlite3",
wantDriver: "sqlite",
},
{
name: "reject sqlite postgres mismatch",
configuredDriver: "sqlite",
dsn: "postgres://user:pass@localhost:5432/vctp?sslmode=disable",
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
driver, inferred, err := ResolveDriver(tc.configuredDriver, tc.dsn)
if tc.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if driver != tc.wantDriver {
t.Fatalf("driver mismatch: got %q want %q", driver, tc.wantDriver)
}
if inferred != tc.wantInferred {
t.Fatalf("inferred mismatch: got %t want %t", inferred, tc.wantInferred)
}
})
}
}