pg_catalog_test.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package pgserver
  2. import (
  3. "strconv"
  4. "strings"
  5. "testing"
  6. )
  7. func simpleQuery(sql string) *Message {
  8. return &Message{Type: MsgQuery, Data: append([]byte(sql), 0)}
  9. }
  10. // Catalog columns carry PostgreSQL's types over the wire (oid 26, name 19), so
  11. // a driver decodes them without falling back to TEXT.
  12. func TestPgCatalogWireTypes(t *testing.T) {
  13. c, client := newDataConnection(t)
  14. for _, sql := range []string{
  15. "CREATE TABLE wire (id INTEGER PRIMARY KEY, payload JSONB)",
  16. "CREATE INDEX idx_wire_payload ON wire (payload)",
  17. } {
  18. if msgs := runQuery(t, c, client, simpleQuery(sql)); errorCode(msgs[0]) != "" {
  19. t.Fatalf("%s: %s", sql, errorMessage(msgs[0]))
  20. }
  21. }
  22. msgs := runQuery(t, c, client, simpleQuery(`
  23. SELECT c.oid, c.relname, c.relkind
  24. FROM pg_catalog.pg_class c
  25. WHERE c.relname = 'wire'`))
  26. if msg := findError(msgs); msg != nil {
  27. t.Fatalf("catalog query failed: %s", errorMessage(msg))
  28. }
  29. rd := findRowDescription(t, msgs)
  30. if got := rowDescriptionOIDs(t, rd.Data); len(got) != 3 || got[0] != 26 || got[1] != 19 || got[2] != 25 {
  31. t.Fatalf("catalog OIDs = %v, want [26 19 25]", got)
  32. }
  33. rows := catalogRows(t, msgs)
  34. if len(rows) != 1 || rows[0][1] != "wire" || rows[0][2] != "r" {
  35. t.Fatalf("pg_class row = %v", rows)
  36. }
  37. if _, err := strconv.ParseUint(rows[0][0], 10, 32); err != nil {
  38. t.Fatalf("oid %q is not numeric: %v", rows[0][0], err)
  39. }
  40. }
  41. // A join across pg_class/pg_namespace/pg_attribute works over the simple-query
  42. // protocol, which is how migration tools list columns.
  43. func TestPgCatalogJoinOverWire(t *testing.T) {
  44. c, client := newDataConnection(t)
  45. for _, sql := range []string{
  46. "CREATE TABLE wire_join (id INTEGER PRIMARY KEY, label TEXT NOT NULL)",
  47. } {
  48. if msgs := runQuery(t, c, client, simpleQuery(sql)); errorCode(msgs[0]) != "" {
  49. t.Fatalf("%s: %s", sql, errorMessage(msgs[0]))
  50. }
  51. }
  52. msgs := runQuery(t, c, client, simpleQuery(`
  53. SELECT n.nspname, a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod)
  54. FROM pg_catalog.pg_class c
  55. JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  56. JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
  57. WHERE c.relname = 'wire_join' AND a.attnum > 0
  58. ORDER BY a.attnum`))
  59. if msg := findError(msgs); msg != nil {
  60. t.Fatalf("catalog join failed: %s", errorMessage(msg))
  61. }
  62. rows := catalogRows(t, msgs)
  63. if len(rows) != 2 || rows[0][1] != "id" || rows[0][2] != "integer" || rows[1][1] != "label" {
  64. t.Fatalf("catalog join rows = %v", rows)
  65. }
  66. }
  67. // ORM connections issue SET during startup; the engine acknowledges it and
  68. // SHOW reports the retained search path.
  69. func TestSetAndShowSearchPathOverWire(t *testing.T) {
  70. c, client := newDataConnection(t)
  71. if msgs := runQuery(t, c, client, simpleQuery("SET search_path TO public")); errorCode(msgs[0]) != "" {
  72. t.Fatalf("SET search_path failed: %s", errorMessage(msgs[0]))
  73. }
  74. if msgs := runQuery(t, c, client, simpleQuery("SET TIME ZONE 'UTC'")); errorCode(msgs[0]) != "" {
  75. t.Fatalf("SET TIME ZONE failed: %s", errorMessage(msgs[0]))
  76. }
  77. msgs := runQuery(t, c, client, simpleQuery("SHOW search_path"))
  78. if msg := findError(msgs); msg != nil {
  79. t.Fatalf("SHOW search_path failed: %s", errorMessage(msg))
  80. }
  81. rows := catalogRows(t, msgs)
  82. if len(rows) != 1 || rows[0][0] != "public" {
  83. t.Fatalf("SHOW search_path = %v", rows)
  84. }
  85. msgs = runQuery(t, c, client, simpleQuery("SHOW client_encoding"))
  86. if rows := catalogRows(t, msgs); len(rows) != 1 || !strings.EqualFold(rows[0][0], "UTF8") {
  87. t.Fatalf("SHOW client_encoding = %v", rows)
  88. }
  89. }