json_contract_test.go 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package pgserver
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. )
  7. // jsonErrorField extracts a named field from an ErrorResponse.
  8. func jsonErrorField(t *testing.T, msg *Message, want byte) string {
  9. t.Helper()
  10. if msg == nil || msg.Type != MsgErrorResponse {
  11. t.Fatalf("message type = %v, want ErrorResponse", msg)
  12. }
  13. data := msg.Data
  14. for i := 0; i < len(data); {
  15. if data[i] == 0 {
  16. break
  17. }
  18. field := data[i]
  19. i++
  20. end := bytes.IndexByte(data[i:], 0)
  21. if end < 0 {
  22. break
  23. }
  24. value := string(data[i : i+end])
  25. if field == want {
  26. return value
  27. }
  28. i += end + 1
  29. }
  30. return ""
  31. }
  32. // The supported JSON contract is TEXT storage plus the JSON1 function surface.
  33. // JSON/JSONB type names are accepted as text-affinity aliases; there is no
  34. // native binary jsonb type or ->> operator.
  35. func TestJSONTextContractOverSimpleQuery(t *testing.T) {
  36. c, client := newDataConnection(t)
  37. runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte("CREATE TABLE settings (id INTEGER PRIMARY KEY, body TEXT)"), 0)})
  38. runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`INSERT INTO settings (id, body) VALUES (1, '{"theme":"dark","widgets":[1,2]}')`), 0)})
  39. msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_extract(body, '$.theme'), json_type(body), json_valid(body) FROM settings WHERE id = 1`), 0)})
  40. values := dataRowValues(t, msgs[1])
  41. if len(values) != 3 || values[0] != "dark" || values[1] != "object" || values[2] != "1" {
  42. t.Fatalf("json round trip = %v, want [dark object 1]", values)
  43. }
  44. // Partial update through json_set, without rewriting the whole document.
  45. runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`UPDATE settings SET body = json_set(body, '$.theme', 'light') WHERE id = 1`), 0)})
  46. msgs = runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_extract(body, '$.theme'), json_extract(body, '$.widgets[1]') FROM settings WHERE id = 1`), 0)})
  47. values = dataRowValues(t, msgs[1])
  48. if len(values) != 2 || values[0] != "light" || values[1] != "2" {
  49. t.Fatalf("json partial update = %v, want [light 2]", values)
  50. }
  51. // JSON and JSONB column type names are text-affinity aliases.
  52. runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte("CREATE TABLE docs (id INTEGER PRIMARY KEY, body JSON)"), 0)})
  53. runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`INSERT INTO docs (id, body) VALUES (1, '{"a":1}')`), 0)})
  54. msgs = runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_extract(body, '$.a') FROM docs`), 0)})
  55. if got := dataRowValues(t, msgs[1]); len(got) != 1 || got[0] != "1" {
  56. t.Fatalf("JSON column extract = %v, want [1]", got)
  57. }
  58. }
  59. func TestJSONInvalidInputOverSimpleQuery(t *testing.T) {
  60. c, client := newDataConnection(t)
  61. msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_extract('not json', '$.a')`), 0)})
  62. if len(msgs) != 2 || msgs[0].Type != MsgErrorResponse {
  63. t.Fatalf("expected ErrorResponse+ReadyForQuery, got %v", msgs)
  64. }
  65. if msg := jsonErrorField(t, msgs[0], ErrorFieldMessage); !strings.Contains(msg, "malformed JSON") {
  66. t.Fatalf("error message = %q, want a malformed-JSON error", msg)
  67. }
  68. // json_valid is the non-throwing way to probe a value.
  69. msgs = runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_valid('not json')`), 0)})
  70. if got := dataRowValues(t, msgs[1]); len(got) != 1 || got[0] != "0" {
  71. t.Fatalf("json_valid = %v, want [0]", got)
  72. }
  73. }
  74. func TestJSONArrowOperatorUnsupported(t *testing.T) {
  75. c, client := newDataConnection(t)
  76. msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT '{"a":1}' -> '$.a'`), 0)})
  77. if len(msgs) == 0 || msgs[0].Type != MsgErrorResponse {
  78. t.Fatalf("expected a parse error for the -> operator, got %v", msgs)
  79. }
  80. }