(t *testing.T)
| 158 | } |
| 159 | |
| 160 | func 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 |
nothing calls this directly
no test coverage detected