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

Function TestTxCommitWhenDeferredConstraintFailure

tx_test.go:104–149  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

102}
103
104func TestTxCommitWhenDeferredConstraintFailure(t *testing.T) {
105 t.Parallel()
106
107 conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
108 defer closeConn(t, conn)
109
110 pgxtest.SkipCockroachDB(t, conn, "Server does not support deferred constraint (https://github.com/cockroachdb/cockroach/issues/31632)")
111
112 createSql := `
113 create temporary table foo(
114 id integer,
115 unique (id) initially deferred
116 );
117 `
118
119 if _, err := conn.Exec(context.Background(), createSql); err != nil {
120 t.Fatalf("Failed to create table: %v", err)
121 }
122
123 tx, err := conn.Begin(context.Background())
124 if err != nil {
125 t.Fatalf("conn.Begin failed: %v", err)
126 }
127
128 if _, err := tx.Exec(context.Background(), "insert into foo(id) values (1)"); err != nil {
129 t.Fatalf("tx.Exec failed: %v", err)
130 }
131
132 if _, err := tx.Exec(context.Background(), "insert into foo(id) values (1)"); err != nil {
133 t.Fatalf("tx.Exec failed: %v", err)
134 }
135
136 err = tx.Commit(context.Background())
137 if pgErr, ok := err.(*pgconn.PgError); !ok || pgErr.Code != "23505" {
138 t.Fatalf("Expected unique constraint violation 23505, got %#v", err)
139 }
140
141 var n int64
142 err = conn.QueryRow(context.Background(), "select count(*) from foo").Scan(&n)
143 if err != nil {
144 t.Fatalf("QueryRow Scan failed: %v", err)
145 }
146 if n != 0 {
147 t.Fatalf("Did not receive correct number of rows: %v", n)
148 }
149}
150
151func TestTxCommitSerializationFailure(t *testing.T) {
152 t.Parallel()

Callers

nothing calls this directly

Calls 8

SkipCockroachDBFunction · 0.92
mustConnectStringFunction · 0.85
closeConnFunction · 0.70
ExecMethod · 0.65
BeginMethod · 0.65
CommitMethod · 0.65
ScanMethod · 0.65
QueryRowMethod · 0.65

Tested by

no test coverage detected