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

Function TestScript

internal/pgmock/pgmock_test.go:19–91  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestScript(t *testing.T) {
20 script := &pgmock.Script{
21 Steps: pgmock.AcceptUnauthenticatedConnRequestSteps(),
22 }
23 script.Steps = append(script.Steps, pgmock.ExpectMessage(&pgproto3.Query{String: "select 42"}))
24 script.Steps = append(script.Steps, pgmock.SendMessage(&pgproto3.RowDescription{
25 Fields: []pgproto3.FieldDescription{
26 {
27 Name: []byte("?column?"),
28 TableOID: 0,
29 TableAttributeNumber: 0,
30 DataTypeOID: 23,
31 DataTypeSize: 4,
32 TypeModifier: -1,
33 Format: 0,
34 },
35 },
36 }))
37 script.Steps = append(script.Steps, pgmock.SendMessage(&pgproto3.DataRow{
38 Values: [][]byte{[]byte("42")},
39 }))
40 script.Steps = append(script.Steps, pgmock.SendMessage(&pgproto3.CommandComplete{CommandTag: []byte("SELECT 1")}))
41 script.Steps = append(script.Steps, pgmock.SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'}))
42 script.Steps = append(script.Steps, pgmock.ExpectMessage(&pgproto3.Terminate{}))
43
44 ln, err := net.Listen("tcp", "127.0.0.1:")
45 require.NoError(t, err)
46 defer ln.Close()
47
48 serverErrChan := make(chan error, 1)
49 go func() {
50 defer close(serverErrChan)
51
52 conn, err := ln.Accept()
53 if err != nil {
54 serverErrChan <- err
55 return
56 }
57 defer conn.Close()
58
59 err = conn.SetDeadline(time.Now().Add(time.Second))
60 if err != nil {
61 serverErrChan <- err
62 return
63 }
64
65 err = script.Run(pgproto3.NewBackend(conn, conn))
66 if err != nil {
67 serverErrChan <- err
68 return
69 }
70 }()
71
72 host, port, _ := strings.Cut(ln.Addr().String(), ":")
73 connStr := fmt.Sprintf("sslmode=disable host=%s port=%s", host, port)
74
75 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
76 defer cancel()

Callers

nothing calls this directly

Calls 12

RunMethod · 0.95
ExpectMessageFunction · 0.92
SendMessageFunction · 0.92
NewBackendFunction · 0.92
ConnectFunction · 0.92
ReadAllMethod · 0.80
CloseMethod · 0.65
ExecMethod · 0.65
LenMethod · 0.65
SetDeadlineMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected