All checks were successful
continuous-integration/drone/push Build is passing
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|