Files
waitfordb/internal/driver/driver_test.go
T

62 lines
1.6 KiB
Go

package driver
import (
"strings"
"testing"
"git.unkin.net/unkin/waitfordb/internal/config"
)
func TestGetRegisteredDrivers(t *testing.T) {
for _, name := range []string{"postgres", "mysql"} {
if _, err := Get(name); err != nil {
t.Errorf("Get(%q) failed: %v", name, err)
}
}
if _, err := Get("oracle"); err == nil {
t.Error("Get(oracle) should fail")
}
}
func TestPostgresDSN(t *testing.T) {
cfg := config.Config{
Host: "db.example", Port: "5432", User: "appuser",
Password: "p@ss/w:rd", Database: "appdb",
SSLMode: "disable", ConnectTimeout: 5e9,
}
dsn := postgresDSN(cfg)
if !strings.HasPrefix(dsn, "postgres://appuser:") {
t.Errorf("unexpected prefix: %s", dsn)
}
// The special-character password must be percent-escaped, not raw.
if strings.Contains(dsn, "p@ss/w:rd") {
t.Errorf("password not escaped in DSN: %s", dsn)
}
if !strings.Contains(dsn, "db.example:5432") {
t.Errorf("missing host:port: %s", dsn)
}
if !strings.Contains(dsn, "sslmode=disable") {
t.Errorf("missing sslmode: %s", dsn)
}
if !strings.Contains(dsn, "connect_timeout=5") {
t.Errorf("missing connect_timeout: %s", dsn)
}
if !strings.Contains(dsn, "/appdb") {
t.Errorf("missing dbname: %s", dsn)
}
}
func TestMySQLDSN(t *testing.T) {
cfg := config.Config{
Host: "db", Port: "3306", User: "u", Password: "pw",
Database: "app", ConnectTimeout: 5e9,
}
dsn := mysqlDSN(cfg)
if !strings.Contains(dsn, "@tcp(db:3306)/app") {
t.Errorf("unexpected mysql dsn: %s", dsn)
}
if !strings.Contains(dsn, "timeout=5s") {
t.Errorf("missing timeout: %s", dsn)
}
}