(t *testing.T)
| 102 | } |
| 103 | |
| 104 | func 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 | |
| 151 | func TestTxCommitSerializationFailure(t *testing.T) { |
| 152 | t.Parallel() |
nothing calls this directly
no test coverage detected