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

Function TestConnCopyFromJSON

copy_from_test.go:398–454  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

396}
397
398func TestConnCopyFromJSON(t *testing.T) {
399 t.Parallel()
400
401 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
402 defer cancel()
403
404 conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
405 defer closeConn(t, conn)
406
407 for _, typeName := range []string{"json", "jsonb"} {
408 if _, ok := conn.TypeMap().TypeForName(typeName); !ok {
409 return // No JSON/JSONB type -- must be running against old PostgreSQL
410 }
411 }
412
413 mustExec(t, conn, `create temporary table foo(
414 a json,
415 b jsonb
416 )`)
417
418 inputRows := [][]any{
419 {map[string]any{"foo": "bar"}, map[string]any{"bar": "quz"}},
420 {nil, nil},
421 }
422
423 copyCount, err := conn.CopyFrom(ctx, pgx.Identifier{"foo"}, []string{"a", "b"}, pgx.CopyFromRows(inputRows))
424 if err != nil {
425 t.Errorf("Unexpected error for CopyFrom: %v", err)
426 }
427 if int(copyCount) != len(inputRows) {
428 t.Errorf("Expected CopyFrom to return %d copied rows, but got %d", len(inputRows), copyCount)
429 }
430
431 rows, err := conn.Query(ctx, "select * from foo")
432 if err != nil {
433 t.Errorf("Unexpected error for Query: %v", err)
434 }
435
436 var outputRows [][]any
437 for rows.Next() {
438 row, err := rows.Values()
439 if err != nil {
440 t.Errorf("Unexpected error for rows.Values(): %v", err)
441 }
442 outputRows = append(outputRows, row)
443 }
444
445 if rows.Err() != nil {
446 t.Errorf("Unexpected error for rows.Err(): %v", rows.Err())
447 }
448
449 if !reflect.DeepEqual(inputRows, outputRows) {
450 t.Errorf("Input rows and output rows do not equal: %v -> %v", inputRows, outputRows)
451 }
452
453 ensureConnValid(t, conn)
454}
455

Callers

nothing calls this directly

Calls 11

mustConnectStringFunction · 0.85
mustExecFunction · 0.85
TypeForNameMethod · 0.80
TypeMapMethod · 0.80
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
CopyFromMethod · 0.65
QueryMethod · 0.65
NextMethod · 0.65
ValuesMethod · 0.65
ErrMethod · 0.65

Tested by

no test coverage detected