TestCursorExtendedQuery exercises the extended-query-protocol cursor path (cursorOpDeclare/cursorOpFetch/cursorOpClose) with pgx.
(t *testing.T)
| 253 | // TestCursorExtendedQuery exercises the extended-query-protocol cursor path |
| 254 | // (cursorOpDeclare/cursorOpFetch/cursorOpClose) with pgx. |
| 255 | func TestCursorExtendedQuery(t *testing.T) { |
| 256 | ctx := context.Background() |
| 257 | |
| 258 | t.Run("declare_fetch_close", func(t *testing.T) { |
| 259 | conn := pgxConnect(t) |
| 260 | defer func() { _ = conn.Close(ctx) }() |
| 261 | |
| 262 | _, tag, err := pgxCursorRows(ctx, conn, "DECLARE cur_ext CURSOR FOR SELECT id, name FROM users ORDER BY id") |
| 263 | if err != nil { |
| 264 | t.Fatalf("DECLARE: %v", err) |
| 265 | } |
| 266 | if tag.String() != "DECLARE CURSOR" { |
| 267 | t.Errorf("DECLARE tag = %q, want %q", tag, "DECLARE CURSOR") |
| 268 | } |
| 269 | |
| 270 | got, tag, err := pgxCursorRows(ctx, conn, "FETCH 4 FROM cur_ext") |
| 271 | if err != nil { |
| 272 | t.Fatalf("FETCH 4: %v", err) |
| 273 | } |
| 274 | assertCursorRows(t, "FETCH 4", got, cursorUsers(1, 4)) |
| 275 | if tag.String() != "FETCH 4" { |
| 276 | t.Errorf("FETCH 4 tag = %q, want %q", tag, "FETCH 4") |
| 277 | } |
| 278 | |
| 279 | got, tag, err = pgxCursorRows(ctx, conn, "FETCH ALL FROM cur_ext") |
| 280 | if err != nil { |
| 281 | t.Fatalf("FETCH ALL: %v", err) |
| 282 | } |
| 283 | assertCursorRows(t, "FETCH ALL", got, cursorUsers(5, 10)) |
| 284 | if tag.String() != "FETCH 6" { |
| 285 | t.Errorf("FETCH ALL tag = %q, want %q", tag, "FETCH 6") |
| 286 | } |
| 287 | |
| 288 | _, tag, err = pgxCursorRows(ctx, conn, "CLOSE cur_ext") |
| 289 | if err != nil { |
| 290 | t.Fatalf("CLOSE: %v", err) |
| 291 | } |
| 292 | if tag.String() != "CLOSE CURSOR" { |
| 293 | t.Errorf("CLOSE tag = %q, want %q", tag, "CLOSE CURSOR") |
| 294 | } |
| 295 | |
| 296 | // FETCH after CLOSE: clean SQLSTATE 34000 missing-cursor error. |
| 297 | _, _, err = pgxCursorRows(ctx, conn, "FETCH 4 FROM cur_ext") |
| 298 | var pgErr *pgconn.PgError |
| 299 | if !errors.As(err, &pgErr) { |
| 300 | t.Fatalf("FETCH after CLOSE: error = %v, want *pgconn.PgError", err) |
| 301 | } |
| 302 | if pgErr.Code != "34000" || !strings.Contains(pgErr.Message, `cursor "cur_ext" does not exist`) { |
| 303 | t.Errorf("FETCH after CLOSE: code=%s message=%q, want 34000 missing-cursor", pgErr.Code, pgErr.Message) |
| 304 | } |
| 305 | }) |
| 306 | |
| 307 | t.Run("move_advances_position", func(t *testing.T) { |
| 308 | conn := pgxConnect(t) |
| 309 | defer func() { _ = conn.Close(ctx) }() |
| 310 | |
| 311 | _, _, err := pgxCursorRows(ctx, conn, "DECLARE cur_ext_move CURSOR FOR SELECT id, name FROM users ORDER BY id") |
| 312 | if err != nil { |
nothing calls this directly
no test coverage detected