From faa980a2565dcee996faa249ac1d61ae4130edbb Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 6 Aug 2019 17:17:54 -0500 Subject: [PATCH] Add tests for deferred constraint errors Port tests from v3. Fix is implemented in pgconn f0b479097a4868d74e83c938131f5a24d25c49e8. --- batch_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++-- go.sum | 6 ++++++ query_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/batch_test.go b/batch_test.go index cb2da08e..dd65cb9a 100644 --- a/batch_test.go +++ b/batch_test.go @@ -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) +} diff --git a/go.mod b/go.mod index 42f5ca7f..5bcae657 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,9 @@ go 1.12 require ( github.com/cockroachdb/apd v1.1.0 github.com/go-stack/stack v1.8.0 // indirect - github.com/jackc/pgconn v0.0.0-20190528115420-71ec1f782113 + github.com/jackc/pgconn v0.0.0-20190806220711-f0b479097a48 github.com/jackc/pgio v1.0.0 - github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db + github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711 github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0 github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b github.com/mattn/go-colorable v0.1.1 // indirect diff --git a/go.sum b/go.sum index d83bf060..fbe4b5cc 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,14 @@ github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= +github.com/jackc/chunkreader/v2 v2.0.0 h1:DUwgMQuuPnS0rhMXenUtZpqZqrR/30NWY+qQvTpSvEs= +github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3 h1:ZFYpB74Kq8xE9gmfxCmXD6QxZ27ja+j3HwGFc+YurhQ= github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= github.com/jackc/pgconn v0.0.0-20190528115420-71ec1f782113 h1:EpJHD0fHY9s+K1d2gn0YrVNf2MzCZsgtGgnzKqJGnOw= github.com/jackc/pgconn v0.0.0-20190528115420-71ec1f782113/go.mod h1:f8MMBsyH8EXpj7xNt09B6QAWl1OYflD0QeF6BBCYsdM= +github.com/jackc/pgconn v0.0.0-20190806220711-f0b479097a48 h1:M3R/SnoNYpmteW+uJE945ak1brFYxfsaBt2b5gS27V4= +github.com/jackc/pgconn v0.0.0-20190806220711-f0b479097a48/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -20,6 +24,8 @@ github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db h1:UpaKn/gYxzH6/zWyRQH1S260zvKqwJJ4h8+Kf09ooh0= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711 h1:vZp4bYotXUkFx7JUSm7U8KV/7Q0AOdrQxxBBj0ZmZsg= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0 h1:mX93v750WifMD1htCt7vqeolcnpaG1gz8URVGjSzcUM= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= diff --git a/query_test.go b/query_test.go index d24228b0..a7e98ed3 100644 --- a/query_test.go +++ b/query_test.go @@ -396,6 +396,47 @@ func TestConnQueryScanIgnoreColumn(t *testing.T) { ensureConnValid(t, conn) } +// https://github.com/jackc/pgx/issues/570 +func TestConnQueryDeferredError(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);`) + + rows, err := conn.Query(context.Background(), `update t set n=n+1 where id='b' returning *`) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for rows.Next() { + var id string + var n int32 + err = rows.Scan(&id, &n) + if err != nil { + t.Fatal(err) + } + } + + if rows.Err() == nil { + t.Fatal("expected error 23505 but got none") + } + + if err, ok := rows.Err().(*pgconn.PgError); !ok || err.Code != "23505" { + t.Fatalf("expected error 23505, got %v", err) + } + + ensureConnValid(t, conn) +} + func TestConnQueryErrorWhileReturningRows(t *testing.T) { t.Parallel()