2
0

Add tests for deferred constraint errors

Port tests from v3.

Fix is implemented in pgconn f0b479097a.
This commit is contained in:
Jack Christensen
2019-08-06 17:17:54 -05:00
parent 3028821487
commit faa980a256
4 changed files with 99 additions and 2 deletions
+50
View File
@@ -565,3 +565,53 @@ func TestTxSendBatchRollback(t *testing.T) {
ensureConnValid(t, conn)
}
func TestConnBeginBatchDeferredError(t *testing.T) {
t.Parallel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)
mustExec(t, conn, `create temporary table t (
id text primary key,
n int not null,
unique (n) deferrable initially deferred
);
insert into t (id, n) values ('a', 1), ('b', 2), ('c', 3);`)
batch := &pgx.Batch{}
batch.Queue(`update t set n=n+1 where id='b' returning *`,
nil,
nil,
[]int16{pgx.BinaryFormatCode},
)
br := conn.SendBatch(context.Background(), batch)
rows, err := br.QueryResults()
if err != nil {
t.Error(err)
}
for rows.Next() {
var id string
var n int32
err = rows.Scan(&id, &n)
if err != nil {
t.Fatal(err)
}
}
err = br.Close()
if err == nil {
t.Fatal("expected error 23505 but got none")
}
if err, ok := err.(*pgconn.PgError); !ok || err.Code != "23505" {
t.Fatalf("expected error 23505, got %v", err)
}
ensureConnValid(t, conn)
}