Files
cgalo5758 d1ac278e6b Open every test database through dbtest
internal/testkit/dbtest opens TEST_DATABASE_URL and E2E_DATABASE_URL, never
migrates, and fails with "the test database is behind; run
test/reset-test-db.sh" when the core ledger lacks the highest version of
db.BaseSources(). The opener helpers except the scratch-database and
container openers (26 of the 29) and the inline openers of the database
tests call it; helpers that did more than open (providers, fake farms, the
FedWiki provider row, cleanup of override rows) keep that setup. The
stripe/workflows and workflows/entitlements openers keep their search_path
through WithSearchPath. The closed-database seam tests use Closed.

TestMigration18SetsExistingFedwikiRulesToForceReduce runs on a scratch
database through Scratch and UpTo instead of rolling the shared core ledger
back through goose's global state, because no opener migrates the shared
database any more.

The in-package forms tests of internal/server reached the integrations'
forms only because an external test file imported internal/migrate; a test
file now links internal/integrations explicitly.

TestRegisterProviders_DB is re-pinned from gocognit 37 to 34.

New tests: TestLedgerCheckRefusesAnEmptyDatabaseAndAcceptsTheTestDatabase
(dbtest), TestOpenRefusesAnEmptyDatabaseAndAcceptsTheTestDatabase (dbtest,
through opened, the path Open and OpenE2E share),
TestKitPackagesReachOnlyTheirAllowedModulePackages (testkit). The skip
messages of the openers are unified to "TEST_DATABASE_URL not set, skipping
integration test". 4 breaks: Open skipping its ledger check and the ledger
check accepting an empty database fail the new tests; a kit row that no
longer allows internal/testkit fails the import guard; leaving the fedwiki
rules as they were in migration 00018 fails the migration 18 test on both
sides.
2026-10-07 16:22:18 -05:00

63 lines
1.6 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package dbtest
import (
"fmt"
"strings"
"testing"
)
// recorder is a testing.TB whose Fatalf records the message and stops the
// goroutine it runs on, so a test can read what Open reports.
type recorder struct {
testing.TB
failure string
}
type stopped struct{}
func (r *recorder) Fatalf(format string, args ...any) {
r.failure = fmt.Sprintf(format, args...)
panic(stopped{})
}
// openRecorded runs opened(dsn) and returns what it reported to Fatalf.
func openRecorded(t *testing.T, dsn string) (failure string) {
t.Helper()
r := &recorder{TB: t}
defer func() {
if p := recover(); p != nil {
if _, ok := p.(stopped); !ok {
panic(p)
}
failure = r.failure
}
}()
opened(r, dsn)
return ""
}
func TestLedgerCheckRefusesAnEmptyDatabaseAndAcceptsTheTestDatabase(t *testing.T) {
empty := Connect(t, Scratch(t))
err := ledgerBehind(empty)
if err == nil || !strings.Contains(err.Error(), "the test database is behind; run test/reset-test-db.sh") {
t.Errorf("an empty database: err = %v, want the reset message", err)
}
if err := ledgerBehind(Connect(t, DSN(t))); err != nil {
t.Errorf("the test database: %v", err)
}
}
func TestOpenRefusesAnEmptyDatabaseAndAcceptsTheTestDatabase(t *testing.T) {
if got := openRecorded(t, Scratch(t)); !strings.Contains(got, "the test database is behind; run test/reset-test-db.sh") {
t.Errorf("an empty database: Fatalf = %q, want the reset message", got)
}
if got := openRecorded(t, DSN(t)); got != "" {
t.Errorf("the test database: Fatalf = %q", got)
}
}