package postgres import "testing" // TestRebind covers the translation the shared component stores depend on. // It runs without a database, which matters: it is the one part of the // PostgreSQL driver that can be tested on a machine without PostgreSQL, and // a mistake here breaks every statement at once. func TestRebind(t *testing.T) { for name, tc := range map[string]struct{ in, want string }{ "no placeholders": { in: `SELECT COUNT(*) FROM user_accounts`, want: `SELECT COUNT(*) FROM user_accounts`, }, "one placeholder": { in: `SELECT password_hash FROM user_accounts WHERE localpart = ?`, want: `SELECT password_hash FROM user_accounts WHERE localpart = $1`, }, "numbered in order": { in: `INSERT INTO user_accounts (localpart, admin) VALUES (?, ?)`, want: `INSERT INTO user_accounts (localpart, admin) VALUES ($1, $2)`, }, "past nine": { in: `VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, want: `VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`, }, "leaves the escape clause alone": { in: `WHERE localpart LIKE ? ESCAPE '!'`, want: `WHERE localpart LIKE $1 ESCAPE '!'`, }, } { t.Run(name, func(t *testing.T) { if got := rebind(tc.in); got != tc.want { t.Errorf("rebind(%q) = %q, want %q", tc.in, got, tc.want) } }) } } func TestIsUniqueViolationIgnoresOtherErrors(t *testing.T) { if isUniqueViolation(nil) { t.Error("nil is not a unique violation") } if isUniqueViolation(errOther{}) { t.Error("an unrelated error was reported as a unique violation") } } type errOther struct{} func (errOther) Error() string { return "something else went wrong" }