MCPcopy
hub / github.com/jackc/pgx / TestBeginTxContextCancel

Function TestBeginTxContextCancel

stdlib/sql_test.go:738–767  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

736}
737
738func TestBeginTxContextCancel(t *testing.T) {
739 testWithAllQueryExecModes(t, func(t *testing.T, db *sql.DB) {
740 skipCockroachDB(t, db, "CockroachDB auto commits DDL by default")
741
742 _, err := db.Exec("drop table if exists t")
743 require.NoError(t, err)
744
745 ctx, cancelFn := context.WithCancel(context.Background())
746
747 tx, err := db.BeginTx(ctx, nil)
748 require.NoError(t, err)
749
750 _, err = tx.Exec("create table t(id serial)")
751 require.NoError(t, err)
752
753 cancelFn()
754
755 err = tx.Commit()
756 if err != context.Canceled && err != sql.ErrTxDone {
757 t.Fatalf("err => %v, want %v or %v", err, context.Canceled, sql.ErrTxDone)
758 }
759
760 var n int
761 err = db.QueryRow("select count(*) from t").Scan(&n)
762 var pgErr *pgconn.PgError
763 if !errors.As(err, &pgErr) || pgErr.Code != "42P01" {
764 t.Fatalf(`err => %v, want PgError{Code: "42P01"}`, err)
765 }
766 })
767}
768
769func TestConnRaw(t *testing.T) {
770 testWithAllQueryExecModes(t, func(t *testing.T, db *sql.DB) {

Callers

nothing calls this directly

Calls 7

skipCockroachDBFunction · 0.70
ExecMethod · 0.65
CommitMethod · 0.65
ScanMethod · 0.65
QueryRowMethod · 0.65
BeginTxMethod · 0.45

Tested by

no test coverage detected