package db import ( "testing" "time" ) func TestRebind(t *testing.T) { for in, want := range map[string]string{ `SELECT a FROM t WHERE b = ? AND c = ?`: `SELECT a FROM t WHERE b = $1 AND c = $2`, `SELECT '?' || "a?" FROM t WHERE x = ? -- why?` + "\n" + `AND y = ?`: `SELECT '?' || "a?" FROM t WHERE x = $1 -- why?` + "\n" + `AND y = $2`, `SELECT 'it''s ?' WHERE a = ? /* ? */ AND b = ?`: `SELECT 'it''s ?' WHERE a = $1 /* ? */ AND b = $2`, `WHERE email LIKE '%@' || ?`: `WHERE email COLLATE nocase LIKE '%@' || $1`, `WHERE url NOT LIKE 'http%' AND k like ?`: `WHERE url COLLATE nocase NOT LIKE 'http%' AND k COLLATE nocase like $1`, `WHERE page LIKE ? ESCAPE '\'`: `WHERE page COLLATE nocase LIKE $1 ESCAPE '\'`, `SELECT 'LIKE', likely FROM t`: `SELECT 'LIKE', likely FROM t`, } { if got := rebind(in); got != want { t.Errorf("rebind(%q)\n got %q\nwant %q", in, got, want) } } } func TestPgArgs(t *testing.T) { at := time.Date(2026, 11, 25, 8, 0, 0, 0, time.UTC) in := []any{true, false, at, "x", int64(3), nil} got := pgArgs(in) if got[0] != int64(1) || got[1] != int64(0) || got[2] != "2026-11-25 08:00:00+00:00" || got[3] != "x" || got[4] != int64(3) || got[5] != nil { t.Fatalf("%#v", got) } if in[0] != true { t.Fatal("changed the caller's slice") } plain := []any{"a", 1} if out := pgArgs(plain); &out[0] != &plain[0] { t.Error("copied args that needed no change") } }