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

Function TestConnSendBatchQueuedQuery

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

Source from the content-addressed store, hash-verified

158}
159
160func TestConnSendBatchQueuedQuery(t *testing.T) {
161 t.Parallel()
162
163 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
164 defer cancel()
165
166 pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
167 pgxtest.SkipCockroachDB(t, conn, "Server serial type is incompatible with test")
168
169 sql := `create temporary table ledger(
170 id serial primary key,
171 description varchar not null,
172 amount int not null
173 );`
174 mustExec(t, conn, sql)
175
176 batch := &pgx.Batch{}
177
178 batch.Queue("insert into ledger(description, amount) values($1, $2)", "q1", 1).Exec(func(ct pgconn.CommandTag) error {
179 assert.EqualValues(t, 1, ct.RowsAffected())
180 return nil
181 })
182
183 batch.Queue("insert into ledger(description, amount) values($1, $2)", "q2", 2).Exec(func(ct pgconn.CommandTag) error {
184 assert.EqualValues(t, 1, ct.RowsAffected())
185 return nil
186 })
187
188 batch.Queue("insert into ledger(description, amount) values($1, $2)", "q3", 3).Exec(func(ct pgconn.CommandTag) error {
189 assert.EqualValues(t, 1, ct.RowsAffected())
190 return nil
191 })
192
193 selectFromLedgerExpectedRows := []struct {
194 id int32
195 description string
196 amount int32
197 }{
198 {1, "q1", 1},
199 {2, "q2", 2},
200 {3, "q3", 3},
201 }
202
203 batch.Queue("select id, description, amount from ledger order by id").Query(func(rows pgx.Rows) error {
204 rowCount := 0
205 var id int32
206 var description string
207 var amount int32
208 _, err := pgx.ForEachRow(rows, []any{&id, &description, &amount}, func() error {
209 assert.Equal(t, selectFromLedgerExpectedRows[rowCount].id, id)
210 assert.Equal(t, selectFromLedgerExpectedRows[rowCount].description, description)
211 assert.Equal(t, selectFromLedgerExpectedRows[rowCount].amount, amount)
212 rowCount++
213
214 return nil
215 })
216 assert.NoError(t, err)
217 return nil

Callers

nothing calls this directly

Calls 11

QueueMethod · 0.95
RunWithQueryExecModesFunction · 0.92
SkipCockroachDBFunction · 0.92
mustExecFunction · 0.85
RowsAffectedMethod · 0.80
ExecMethod · 0.65
QueryMethod · 0.65
QueryRowMethod · 0.65
ScanMethod · 0.65
CloseMethod · 0.65
SendBatchMethod · 0.65

Tested by

no test coverage detected