MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestHandleQueryEmitsCorrectSQLSTATEOnDuckDBErrors

Function TestHandleQueryEmitsCorrectSQLSTATEOnDuckDBErrors

server/conn_test.go:1618–1662  ·  view source on GitHub ↗

TestHandleQueryEmitsCorrectSQLSTATEOnDuckDBErrors exercises the full handleQuery → wire-frame path so PG-aware clients (psycopg2, SQLAlchemy, dlt) receive the SQLSTATE they branch on. The test injects canonical DuckDB error strings via the executor mock, then parses the resulting ErrorResponse frame

(t *testing.T)

Source from the content-addressed store, hash-verified

1616// DuckDB error strings via the executor mock, then parses the resulting
1617// ErrorResponse frame out of the writer buffer and asserts the 'C' field.
1618func TestHandleQueryEmitsCorrectSQLSTATEOnDuckDBErrors(t *testing.T) {
1619 cases := []struct {
1620 name string
1621 duckErr string
1622 wantCode string
1623 }{
1624 {"missing table", "Catalog Error: Table with name nope does not exist!", "42P01"},
1625 {"missing schema", "Catalog Error: Schema with name missing_schema does not exist!", "3F000"},
1626 {"missing function", "Catalog Error: Scalar Function with name no_such_func does not exist!", "42883"},
1627 {"missing column", "Binder Error: Referenced column \"missing_col\" not found in FROM clause!", "42703"},
1628 {"parser syntax", "Parser Error: syntax error at or near \"FORM\"", "42601"},
1629 {"conversion", "Conversion Error: Could not convert string 'abc' to INT32", "22P02"},
1630 {"unique violation", "Constraint Error: Duplicate key \"id: 1\" violates primary key constraint", "23505"},
1631 {"permission", "Permission Error: not allowed to write here", "42501"},
1632 }
1633
1634 for _, tc := range cases {
1635 t.Run(tc.name, func(t *testing.T) {
1636 clientSide, serverSide := net.Pipe()
1637 defer func() { _ = clientSide.Close() }()
1638 defer func() { _ = serverSide.Close() }()
1639
1640 var out bytes.Buffer
1641 c := &clientConn{
1642 server: &Server{activeQueries: make(map[BackendKey]context.CancelFunc)},
1643 conn: clientSide,
1644 reader: bufio.NewReader(clientSide),
1645 writer: bufio.NewWriter(&out),
1646 ctx: context.Background(),
1647 cancel: func() {},
1648 txStatus: txStatusIdle,
1649 executor: &queryErrorExecutor{err: errors.New(tc.duckErr)},
1650 }
1651
1652 if err := c.handleQuery([]byte("SELECT 1\x00")); err != nil {
1653 t.Fatalf("handleQuery returned error: %v", err)
1654 }
1655
1656 code := extractErrorResponseField(t, out.Bytes(), 'C')
1657 if code != tc.wantCode {
1658 t.Fatalf("SQLSTATE: got %q, want %q (duckErr=%q)", code, tc.wantCode, tc.duckErr)
1659 }
1660 })
1661 }
1662}
1663
1664// extractErrorResponseField scans wire-protocol frames for an ErrorResponse
1665// ('E') message and returns the requested field value (e.g., 'C' for

Callers

nothing calls this directly

Calls 4

handleQueryMethod · 0.95
RunMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected