package store import ( "errors" "fmt" "testing" "github.com/jackc/pgx/v5/pgconn" ) func TestADuplicateIsRecognisedOnEitherDatabase(t *testing.T) { for _, c := range []struct { name string err error want bool }{ {"sqlite", errors.New("constraint failed: UNIQUE constraint failed: users.email (2067)"), true}, // What pgx returns, wrapped as database/sql passes it on: "unique" // in lower case, which a check for "UNIQUE" missed. {"postgres", fmt.Errorf("exec: %w", &pgconn.PgError{Code: "23505", Message: `duplicate key value violates unique constraint "users_email_key"`}), true}, {"postgres, as text", errors.New(`ERROR: duplicate key value violates unique constraint "users_email_key" (SQLSTATE 23505)`), true}, {"another postgres error", &pgconn.PgError{Code: "23502", Message: "null value in column"}, false}, {"nothing", nil, false}, } { if got := isUniqueViolation(c.err); got != c.want { t.Errorf("%s: %v, want %v", c.name, got, c.want) } } }