feat(backend): add PoolProxy, Querier, and per-test transaction infrastructure
New types and helpers for context-aware DB routing and per-test transactions: - PoolProxy: wraps pgxpool.Pool, routes queries through context transaction when active - Querier: interface accepted by fixture/helper functions for decoupling - ContextWithTx / TxFromContext: store/extract pgx.Tx in context.Context - SetupTestTx (in testutils): begins tx, stores in context, auto-rolls back on cleanup - SetupTestTx (in testtx): package-level variant with semaphore for parallel safety Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
//go:build test
|
||||
// +build test
|
||||
|
||||
package testtx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"crussell/db"
|
||||
)
|
||||
|
||||
// txSemaphore limits concurrent test transactions per package.
|
||||
var txSemaphore = make(chan struct{}, 8)
|
||||
|
||||
// SetupTestTx begins a transaction, stores it in context, and auto-rolls back.
|
||||
func SetupTestTx(t *testing.T) (context.Context, db.Querier) {
|
||||
t.Helper()
|
||||
txSemaphore <- struct{}{}
|
||||
pool := db.Conn.Pool()
|
||||
tx, err := pool.Begin(context.Background())
|
||||
if err != nil {
|
||||
<-txSemaphore
|
||||
t.Fatalf("SetupTestTx: failed to begin transaction: %v", err)
|
||||
}
|
||||
ctx := db.ContextWithTx(context.Background(), tx)
|
||||
t.Cleanup(func() {
|
||||
tx.Rollback(context.Background())
|
||||
<-txSemaphore
|
||||
})
|
||||
return ctx, tx
|
||||
}
|
||||
Reference in New Issue
Block a user