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

Function TestConnCopyFromBinary

pgconn/pgconn_test.go:2382–2436  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2380}
2381
2382func TestConnCopyFromBinary(t *testing.T) {
2383 t.Parallel()
2384
2385 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
2386 defer cancel()
2387
2388 pgConn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
2389 require.NoError(t, err)
2390 defer closeConn(t, pgConn)
2391
2392 _, err = pgConn.Exec(ctx, `create temporary table foo(
2393 a int4,
2394 b varchar
2395 )`).ReadAll()
2396 require.NoError(t, err)
2397
2398 buf := []byte{}
2399 buf = append(buf, "PGCOPY\n\377\r\n\000"...)
2400 buf = pgio.AppendInt32(buf, 0)
2401 buf = pgio.AppendInt32(buf, 0)
2402
2403 inputRows := [][][]byte{}
2404 for i := range 1000 {
2405 // Number of elements in the tuple
2406 buf = pgio.AppendInt16(buf, int16(2))
2407 a := i
2408
2409 // Length of element for column `a int4`
2410 buf = pgio.AppendInt32(buf, 4)
2411 buf, err = pgtype.NewMap().Encode(pgtype.Int4OID, pgx.BinaryFormatCode, a, buf)
2412 require.NoError(t, err)
2413
2414 b := "foo " + strconv.Itoa(a) + " bar"
2415 lenB := int32(len([]byte(b)))
2416 // Length of element for column `b varchar`
2417 buf = pgio.AppendInt32(buf, lenB)
2418 buf, err = pgtype.NewMap().Encode(pgtype.VarcharOID, pgx.BinaryFormatCode, b, buf)
2419 require.NoError(t, err)
2420
2421 inputRows = append(inputRows, [][]byte{[]byte(strconv.Itoa(a)), []byte(b)})
2422 }
2423
2424 srcBuf := &bytes.Buffer{}
2425 srcBuf.Write(buf)
2426 ct, err := pgConn.CopyFrom(ctx, srcBuf, "COPY foo (a, b) FROM STDIN BINARY;")
2427 require.NoError(t, err)
2428 assert.Equal(t, int64(len(inputRows)), ct.RowsAffected())
2429
2430 result := pgConn.ExecParams(ctx, "select * from foo", nil, nil, nil, nil).Read()
2431 require.NoError(t, result.Err)
2432
2433 assert.Equal(t, inputRows, result.Rows)
2434
2435 ensureConnValid(t, pgConn)
2436}
2437
2438func TestConnCopyFromCanceled(t *testing.T) {
2439 t.Parallel()

Callers

nothing calls this directly

Calls 14

ConnectFunction · 0.92
AppendInt32Function · 0.92
AppendInt16Function · 0.92
NewMapFunction · 0.92
ReadAllMethod · 0.80
RowsAffectedMethod · 0.80
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
ExecMethod · 0.65
EncodeMethod · 0.65
CopyFromMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected