...

Source file src/github.com/jackc/pgx/v5/pgproto3/trace_test.go

Documentation: github.com/jackc/pgx/v5/pgproto3

     1  package pgproto3_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/jackc/pgx/v5/pgconn"
    11  	"github.com/jackc/pgx/v5/pgproto3"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestTrace(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
    19  	defer cancel()
    20  
    21  	conn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
    22  	require.NoError(t, err)
    23  	defer conn.Close(ctx)
    24  
    25  	if conn.ParameterStatus("crdb_version") != "" {
    26  		t.Skip("Skipping message trace on CockroachDB as it varies slightly from PostgreSQL")
    27  	}
    28  
    29  	traceOutput := &bytes.Buffer{}
    30  	conn.Frontend().Trace(traceOutput, pgproto3.TracerOptions{
    31  		SuppressTimestamps: true,
    32  		RegressMode:        true,
    33  	})
    34  
    35  	result := conn.ExecParams(ctx, "select n from generate_series(1,5) n", nil, nil, nil, nil).Read()
    36  	require.NoError(t, result.Err)
    37  
    38  	expected := `F	Parse	45	 "" "select n from generate_series(1,5) n" 0
    39  F	Bind	13	 "" "" 0 0 0
    40  F	Describe	7	 P ""
    41  F	Execute	10	 "" 0
    42  F	Sync	5
    43  B	ParseComplete	5
    44  B	BindComplete	5
    45  B	RowDescription	27	 1 "n" 0 0 23 4 -1 0
    46  B	DataRow	12	 1 1 '1'
    47  B	DataRow	12	 1 1 '2'
    48  B	DataRow	12	 1 1 '3'
    49  B	DataRow	12	 1 1 '4'
    50  B	DataRow	12	 1 1 '5'
    51  B	CommandComplete	14	 "SELECT 5"
    52  B	ReadyForQuery	6	 I
    53  `
    54  
    55  	require.Equal(t, expected, traceOutput.String())
    56  }
    57  

View as plain text