MCPcopy Create free account
hub / github.com/lib/pq / TestEmptyQuery

Function TestEmptyQuery

conn_test.go:265–311  ·  view source on GitHub ↗

Test that EmptyQueryResponses are handled correctly.

(t *testing.T)

Source from the content-addressed store, hash-verified

263
264// Test that EmptyQueryResponses are handled correctly.
265func TestEmptyQuery(t *testing.T) {
266 db := pqtest.MustDB(t)
267
268 res, err := db.Exec("")
269 if err != nil {
270 t.Fatal(err)
271 }
272 if _, err := res.RowsAffected(); err != errNoRowsAffected {
273 t.Fatalf("want %s, got %v", errNoRowsAffected, err)
274 }
275 if _, err := res.LastInsertId(); err != errNoLastInsertID {
276 t.Fatalf("want %s, got %v", errNoLastInsertID, err)
277 }
278
279 have := pqtest.Query[any](t, db, ``)
280 want := []map[string]any{}
281 if !reflect.DeepEqual(have, want) {
282 t.Errorf("\nhave: %#v\nwant: %#v", have, want)
283 }
284
285 stmt := pqtest.Prepare(t, db, "")
286 stmt.MustExec(t)
287 res = stmt.MustExec(t)
288 if _, err := res.RowsAffected(); err != errNoRowsAffected {
289 t.Fatalf("expected %s, got %v", errNoRowsAffected, err)
290 }
291 if _, err := res.LastInsertId(); err != errNoLastInsertID {
292 t.Fatalf("expected %s, got %v", errNoLastInsertID, err)
293 }
294 rows, err := stmt.Query()
295 if err != nil {
296 t.Fatal(err)
297 }
298 cols, err := rows.Columns()
299 if err != nil {
300 t.Fatal(err)
301 }
302 if len(cols) != 0 {
303 t.Fatalf("unexpected number of columns %d in response to an empty query", len(cols))
304 }
305 if rows.Next() {
306 t.Fatal("unexpected row")
307 }
308 if rows.Err() != nil {
309 t.Fatal(rows.Err())
310 }
311}
312
313// Test that rows.Columns() is correct even if there are no result rows.
314func TestEmptyResultSetColumns(t *testing.T) {

Callers

nothing calls this directly

Calls 11

MustDBFunction · 0.92
PrepareFunction · 0.92
RowsAffectedMethod · 0.80
LastInsertIdMethod · 0.80
MustExecMethod · 0.80
ColumnsMethod · 0.80
NextMethod · 0.80
FatalMethod · 0.65
ExecMethod · 0.45
QueryMethod · 0.45
ErrMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…