CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
// Package testdb liefert Integrationstests einen Pool auf ein eigenes,
|
|
// frisch angelegtes Schema (search_path = Schema, public). Tabellen, die ein
|
|
// Test per CREATE TABLE anlegt, entstehen damit nur in diesem Schema; Pakete,
|
|
// Laeufe und parallele Checkouts teilen sich keine Tabellen mehr.
|
|
package testdb
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var seq atomic.Int64
|
|
|
|
// Pool legt ein Schema an und liefert einen darauf ausgerichteten Pool;
|
|
// beides wird per t.Cleanup wieder entfernt.
|
|
func Pool(t testing.TB, dsn string) *pgxpool.Pool {
|
|
t.Helper()
|
|
pool, _ := PoolWithSchema(t, dsn, nil)
|
|
return pool
|
|
}
|
|
|
|
// PoolWithSchema wie Pool, zusaetzlich mit dem Schemanamen und der
|
|
// Moeglichkeit, die Verbindungskonfiguration anzupassen.
|
|
func PoolWithSchema(t testing.TB, dsn string, adjust func(*pgxpool.Config)) (*pgxpool.Pool, string) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
admin, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
t.Fatalf("testdb: verbindung: %v", err)
|
|
}
|
|
schema := fmt.Sprintf("connecttest_%d_%d", time.Now().UnixNano(), seq.Add(1))
|
|
if _, err := admin.Exec(ctx, "CREATE SCHEMA "+schema); err != nil {
|
|
admin.Close()
|
|
t.Fatalf("testdb: schema anlegen: %v", err)
|
|
}
|
|
|
|
cfg, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
admin.Close()
|
|
t.Fatalf("testdb: dsn: %v", err)
|
|
}
|
|
cfg.ConnConfig.RuntimeParams["search_path"] = schema + ",public"
|
|
if adjust != nil {
|
|
adjust(cfg)
|
|
}
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
admin.Close()
|
|
t.Fatalf("testdb: pool: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
pool.Close()
|
|
_, _ = admin.Exec(context.Background(), "DROP SCHEMA "+schema+" CASCADE")
|
|
admin.Close()
|
|
})
|
|
return pool, schema
|
|
}
|