Files
dbx/dbxtest/tx_test.go
Aleksey Shakhmatov 2c9af28548
Some checks failed
CI / test (push) Failing after 13s
Add production features: slog adapter, scan helpers, slow query logging, pool stats, tracer passthrough, test tx isolation
- slog.go: SlogLogger adapts *slog.Logger to dbx.Logger interface
- scan.go: Collect[T] and CollectOne[T] generic helpers using pgx.RowToStructByName
- cluster.go: slow query logging via Config.SlowQueryThreshold (Warn level in queryEnd)
- stats.go: PoolStats with Cluster.Stats() aggregating pool stats across all nodes
- config.go/node.go: NodeConfig.Tracer passthrough for pgx.QueryTracer (OpenTelemetry)
- options.go: WithSlowQueryThreshold and WithTracer functional options
- dbxtest/tx.go: RunInTx runs callback in always-rolled-back transaction for test isolation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 00:19:26 +03:00

51 lines
1.2 KiB
Go

package dbxtest_test
import (
"context"
"testing"
"git.codelab.vc/pkg/dbx"
"git.codelab.vc/pkg/dbx/dbxtest"
)
func TestRunInTx(t *testing.T) {
c := dbxtest.NewTestCluster(t)
ctx := context.Background()
// Create a table that persists across the test.
_, err := c.Exec(ctx, `CREATE TABLE IF NOT EXISTS test_run_in_tx (id int)`)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_, _ = c.Exec(context.Background(), `DROP TABLE IF EXISTS test_run_in_tx`)
})
dbxtest.RunInTx(t, c, func(ctx context.Context, q dbx.Querier) {
_, err := q.Exec(ctx, `INSERT INTO test_run_in_tx (id) VALUES (1)`)
if err != nil {
t.Fatal(err)
}
// Row should be visible within the transaction.
var count int
err = q.QueryRow(ctx, `SELECT count(*) FROM test_run_in_tx`).Scan(&count)
if err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("expected 1 row in tx, got %d", count)
}
})
// After RunInTx returns, the transaction was rolled back; row should not exist.
var count int
err = c.QueryRow(ctx, `SELECT count(*) FROM test_run_in_tx`).Scan(&count)
if err != nil {
t.Fatal(err)
}
if count != 0 {
t.Errorf("expected 0 rows after rollback, got %d", count)
}
}