From 178b4c1a641644d8fea93a0fd28e9087de626c85 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 12 Oct 2019 10:46:09 -0500 Subject: [PATCH] Add test for larger batches --- batch_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/batch_test.go b/batch_test.go index 380f3719..a41d2a16 100644 --- a/batch_test.go +++ b/batch_test.go @@ -8,6 +8,7 @@ import ( "github.com/jackc/pgconn" "github.com/jackc/pgconn/stmtcache" "github.com/jackc/pgx/v4" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -137,6 +138,47 @@ func TestConnSendBatch(t *testing.T) { ensureConnValid(t, conn) } +func TestConnSendBatchMany(t *testing.T) { + t.Parallel() + + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) + defer closeConn(t, conn) + + sql := `create temporary table ledger( + id serial primary key, + description varchar not null, + amount int not null + );` + mustExec(t, conn, sql) + + batch := &pgx.Batch{} + + numInserts := 1000 + + for i := 0; i < numInserts; i++ { + batch.Queue("insert into ledger(description, amount) values($1, $2)", "q1", 1) + } + batch.Queue("select count(*) from ledger") + + br := conn.SendBatch(context.Background(), batch) + + for i := 0; i < numInserts; i++ { + ct, err := br.Exec() + assert.NoError(t, err) + assert.EqualValues(t, 1, ct.RowsAffected()) + } + + var actualInserts int + err := br.QueryRow().Scan(&actualInserts) + assert.NoError(t, err) + assert.EqualValues(t, numInserts, actualInserts) + + err = br.Close() + require.NoError(t, err) + + ensureConnValid(t, conn) +} + func TestConnSendBatchWithPreparedStatement(t *testing.T) { t.Parallel()