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) } }