Files
sqlite/backend_parity_test.go
zooqueenandHanzo Dev e671357630 csqlite v0.1.2 — the two backends answer the same SQL, with no tags
This driver stands in front of two backends, and they did not agree. The cgo one
(csqlite) compiled acos/cos/sin/radians/sqrt and fts5 only behind build tags; the
pure-Go one (modernc) always had them. So a cgo build produced a binary whose SQL
surface was SMALLER than production's, and nothing said so until a query came
back "no such function: acos".

hanzoai/base papered over that with a compile-time assertion: a cgo build without
`-tags sqlite_math_functions` fails to compile. It was pointing at something real,
but making one of the two modes refuse to build is not the two modes agreeing --
and it is why `go build ./...` and `go test ./...`, on any machine where CGO
defaults on, do not work in this tree.

csqlite v0.1.2 compiles ENABLE_MATH_FUNCTIONS and ENABLE_FTS5 unconditionally, so
the asymmetry is gone where it was introduced rather than asserted downstream.
Both modes now build and pass with NO tags.

The test asserts the PROPERTY, not the tag standing in for it: run it under
CGO_ENABLED=1 and 0, it must pass both. Verified it can fail -- pointed back at
csqlite v0.1.0 it reports "no such function: acos" under CGO=1, which is the
runtime bug base's compile error existed to prevent.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-06 21:15:35 -07:00

47 lines
1.8 KiB
Go

package sqlite_test
import (
"database/sql"
"testing"
_ "github.com/hanzoai/sqlite"
)
// The two backends must answer the SAME SQL, with NO build tags.
//
// This replaces a compile-time assertion in hanzoai/base that refused to build
// a cgo binary unless the caller passed `-tags sqlite_math_functions`. That
// guard was pointing at something real — the cgo backend used to expose a
// SMALLER SQL surface than the pure-Go one, so `acos` existed in production and
// not in a developer's build — but it fixed the asymmetry by making one of the
// two modes fail to compile, which is not two modes agreeing.
//
// csqlite compiles ENABLE_MATH_FUNCTIONS and ENABLE_FTS5 unconditionally now, so
// the asymmetry is gone at its source. This asserts the property directly rather
// than asserting a tag that stands in for it: run it under CGO_ENABLED=1 and
// CGO_ENABLED=0 and it must pass both times. Nothing here needs a tag, and
// nothing downstream should need one either.
func TestBackendParity(t *testing.T) {
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
// The five hanzoai/base's search layer generates (geoDistance).
var acos, cos, sin, radians, sqrt float64
if err := db.QueryRow(`select acos(1), cos(0), sin(0), radians(180), sqrt(4)`).
Scan(&acos, &cos, &sin, &radians, &sqrt); err != nil {
t.Fatalf("math functions missing on this backend: %v", err)
}
if sqrt != 2 || cos != 1 {
t.Fatalf("math answered wrong: sqrt(4)=%v cos(0)=%v", sqrt, cos)
}
// The sanctioned full-text engine; FTS3 is upstream-mattn legacy the pure-Go
// side does not have, so parity is defined on FTS5.
if _, err := db.Exec(`create virtual table fts using fts5(body)`); err != nil {
t.Fatalf("fts5 missing on this backend: %v", err)
}
}