(t *testing.T)
| 729 | } |
| 730 | |
| 731 | func 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 { |
nothing calls this directly
no test coverage detected