Files
ARTEX/server/testmain_test.go
T
BaaRai a422a071f6 server: skip Postgres-backed tests when no database is configured
Without ARTEX_PG_DSN and without a config file, go test ./server/ failed
44 tests with "no database config found" instead of skipping them. The
tests go through NewManager and t.Fatal on its error: 35 through the
shared fixture trafficEvidenceServer (directly, or through
verificationFixture and newNotifyFixture), 2 through ingestServer, and 7
call NewManager themselves. The 7 already carry a skip on m.pg == nil,
but it sits after the t.Fatal and is never reached.

Add skipWithoutDSN, which calls db.DSN() and skips with the message the
db package and assembly_test.go already use ("no database config (%v) —
skipping"), and call it before NewManager at those 9 sites.

Nothing changes when a database is configured: db.DSN() succeeds, the
helper returns, and a NewManager error (for example an unreachable
Postgres) still fails the test.
2026-11-10 16:28:26 +01:00

45 lines
1.3 KiB
Go

package server
import (
"database/sql"
"os"
"testing"
"github.com/Autumn-27/artex/db"
_ "github.com/jackc/pgx/v5/stdlib"
)
// TestMain acquires a PostgreSQL advisory lock (7337741002) for the entire
// server test suite so cross-package DELETE cleanup races with db/agent
// packages are avoided when running `go test ./...`.
func TestMain(m *testing.M) {
// Own database per package (#80): with separate databases, packages stop
// contending on shared tables and the advisory lock below stops
// serializing go test ./... across packages.
db.SuffixTestDatabase("server")
dsn, _, err := db.DSN()
if err != nil {
os.Exit(m.Run())
}
conn, err := sql.Open("pgx", dsn)
if err != nil || conn.Ping() != nil {
os.Exit(m.Run())
}
defer conn.Close()
if _, err := conn.Exec(`SELECT pg_advisory_lock(7337741002)`); err != nil {
os.Exit(m.Run())
}
defer conn.Exec(`SELECT pg_advisory_unlock(7337741002)`) //nolint:errcheck
os.Exit(m.Run())
}
// skipWithoutDSN skips the test when neither ARTEX_PG_DSN nor a config file
// supplies a database, so fixtures that t.Fatal on a NewManager error skip
// instead of failing. A configured database that cannot be reached still fails.
func skipWithoutDSN(t *testing.T) {
t.Helper()
if _, _, err := db.DSN(); err != nil {
t.Skipf("no database config (%v) — skipping", err)
}
}