db.DB wraps database/sql for either database. The SQL stays SQLite's: on Postgres each statement's ? become $n and LIKE ignores case as SQLite's does, and the Postgres schema (postgres.sql, schema 29 as a whole) adds SQLite's instr, json_extract and two-argument max/min, and a nocase ICU collation. Needs Postgres 18. LastInsertId became db.Insert (RETURNING id), INSERT OR IGNORE became ON CONFLICT DO NOTHING, upserts qualify their columns, and three queries group by every column they use. SUPERMARKET_TEST_POSTGRES runs the test suite on Postgres, a new schema per test; all tests pass on both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
39 lines
1.6 KiB
Go
39 lines
1.6 KiB
Go
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")
|
|
}
|
|
}
|