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

Function TestTxSendBatch

batch_test.go:731–797  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

729}
730
731func TestTxSendBatch(t *testing.T) {
732 t.Parallel()
733
734 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
735 defer cancel()
736
737 pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
738 sql := `create temporary table ledger1(
739 id serial primary key,
740 description varchar not null
741 );`
742 mustExec(t, conn, sql)
743
744 sql = `create temporary table ledger2(
745 id int primary key,
746 amount int not null
747 );`
748 mustExec(t, conn, sql)
749
750 tx, _ := conn.Begin(ctx)
751 batch := &pgx.Batch{}
752 batch.Queue("insert into ledger1(description) values($1) returning id", "q1")
753
754 br := tx.SendBatch(context.Background(), batch)
755
756 var id int
757 err := br.QueryRow().Scan(&id)
758 if err != nil {
759 t.Error(err)
760 }
761 br.Close()
762
763 batch = &pgx.Batch{}
764 batch.Queue("insert into ledger2(id,amount) values($1, $2)", id, 2)
765 batch.Queue("select amount from ledger2 where id = $1", id)
766
767 br = tx.SendBatch(ctx, batch)
768
769 ct, err := br.Exec()
770 if err != nil {
771 t.Error(err)
772 }
773 if ct.RowsAffected() != 1 {
774 t.Errorf("ct.RowsAffected() => %v, want %v", ct.RowsAffected(), 1)
775 }
776
777 var amount int
778 err = br.QueryRow().Scan(&amount)
779 if err != nil {
780 t.Error(err)
781 }
782
783 br.Close()
784 tx.Commit(ctx)
785
786 var count int
787 conn.QueryRow(ctx, "select count(1) from ledger1 where id = $1", id).Scan(&count)
788 if count != 1 {

Callers

nothing calls this directly

Calls 12

QueueMethod · 0.95
RunWithQueryExecModesFunction · 0.92
mustExecFunction · 0.85
RowsAffectedMethod · 0.80
BeginMethod · 0.65
SendBatchMethod · 0.65
ScanMethod · 0.65
QueryRowMethod · 0.65
CloseMethod · 0.65
ExecMethod · 0.65
CommitMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected