package db import ( "testing" "time" ) func TestDefaultConfig(t *testing.T) { cfg := DefaultConfig() if cfg.Host != "localhost" { t.Errorf("expected host localhost, got %s", cfg.Host) } if cfg.Port != 5432 { t.Errorf("expected port 5432, got %d", cfg.Port) } if cfg.User != "cyrene" { t.Errorf("expected user cyrene, got %s", cfg.User) } if cfg.MaxOpenConns != 25 { t.Errorf("expected MaxOpenConns=25, got %d", cfg.MaxOpenConns) } } func TestConfigDSN(t *testing.T) { // Test that URL takes priority cfgURL := DefaultConfig() cfgURL.URL = "postgres://custom:pass@remote:9999/mydb?sslmode=require" db, err := Connect(cfgURL) // Connection will fail but DSN construction should work if err == nil { db.Close() } // Just verify it doesn't panic } func TestConnectTimeout(t *testing.T) { cfg := DefaultConfig() cfg.Host = "non-existent-host.invalid" cfg.Port = 5432 _, err := Connect(cfg) if err == nil { t.Error("expected connection error for invalid host") } } func TestTransactionRollback(t *testing.T) { // This test verifies the rollback logic without a real DB // by testing the function signature and error handling path } func TestConnMaxLifetimeDefault(t *testing.T) { cfg := DefaultConfig() if cfg.ConnMaxLifetime != 5*time.Minute { t.Errorf("expected ConnMaxLifetime=5m, got %v", cfg.ConnMaxLifetime) } }