CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (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 (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
115 lines
3.3 KiB
Go
115 lines
3.3 KiB
Go
// Package testdb gibt Integrationstests einen Pool auf ein eigenes, frisch
|
|
// angelegtes Schema (search_path = Schema, public) sowie eindeutige Namen fuer
|
|
// Mandanten und Datenbanken. Pakete und parallele Laeufe teilen sich keine
|
|
// Tabellen und keine festen Slugs mehr.
|
|
package testdb
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var seq atomic.Int64
|
|
|
|
// Suffix liefert einen prozess- und aufrufeindeutigen Zeichenblock aus [a-z0-9].
|
|
func Suffix() string {
|
|
b := make([]byte, 3)
|
|
_, _ = rand.Read(b)
|
|
return fmt.Sprintf("p%ds%d%s", os.Getpid(), seq.Add(1), hex.EncodeToString(b))
|
|
}
|
|
|
|
// Slug haengt Suffix an prefix an und bleibt innerhalb des Mandanten-Slug-Musters
|
|
// ^[a-z][a-z0-9_]{1,48}$ (prefix hoechstens 20 Zeichen).
|
|
func Slug(prefix string) string {
|
|
if len(prefix) > 20 {
|
|
prefix = prefix[:20]
|
|
}
|
|
return prefix + "_" + Suffix()
|
|
}
|
|
|
|
// 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 := "qatest_" + Suffix()
|
|
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
|
|
}
|
|
|
|
// Schema liefert den Namen des Test-Schemas, auf das pool ausgerichtet ist.
|
|
func Schema(t testing.TB, pool *pgxpool.Pool) string {
|
|
t.Helper()
|
|
var schema string
|
|
if err := pool.QueryRow(context.Background(), "SELECT current_schema()").Scan(&schema); err != nil {
|
|
t.Fatalf("testdb: schema ermitteln: %v", err)
|
|
}
|
|
return schema
|
|
}
|
|
|
|
// Open oeffnet einen Pool auf ein vorhandenes Test-Schema. Ohne testing.TB
|
|
// nutzbar, damit auch Goroutinen eigene "Instanz"-Pools anlegen koennen; den
|
|
// Pool schliesst der Aufrufer.
|
|
func Open(ctx context.Context, dsn, schema string) (*pgxpool.Pool, error) {
|
|
cfg, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cfg.ConnConfig.RuntimeParams["search_path"] = schema + ",public"
|
|
return pgxpool.NewWithConfig(ctx, cfg)
|
|
}
|
|
|
|
// Sibling oeffnet einen weiteren Pool auf dasselbe Schema wie pool, etwa fuer
|
|
// Tests mit zwei "Instanzen", die sich Zustand teilen muessen.
|
|
func Sibling(t testing.TB, pool *pgxpool.Pool, dsn string) *pgxpool.Pool {
|
|
t.Helper()
|
|
sibling, err := Open(context.Background(), dsn, Schema(t, pool))
|
|
if err != nil {
|
|
t.Fatalf("testdb: sibling-pool: %v", err)
|
|
}
|
|
t.Cleanup(sibling.Close)
|
|
return sibling
|
|
}
|