| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package pgserver
- import (
- "bytes"
- "strings"
- "testing"
- )
- // jsonErrorField extracts a named field from an ErrorResponse.
- func jsonErrorField(t *testing.T, msg *Message, want byte) string {
- t.Helper()
- if msg == nil || msg.Type != MsgErrorResponse {
- t.Fatalf("message type = %v, want ErrorResponse", msg)
- }
- data := msg.Data
- for i := 0; i < len(data); {
- if data[i] == 0 {
- break
- }
- field := data[i]
- i++
- end := bytes.IndexByte(data[i:], 0)
- if end < 0 {
- break
- }
- value := string(data[i : i+end])
- if field == want {
- return value
- }
- i += end + 1
- }
- return ""
- }
- // The JSON (text) contract is TEXT storage plus the JSON1 function surface and
- // the -> / ->> operators. The JSONB type is native: it stores canonical JSON
- // text and reports OID 3802 (see jsonb_contract_test.go), while JSON keeps the
- // text-backed JSON1 behavior exercised here.
- func TestJSONTextContractOverSimpleQuery(t *testing.T) {
- c, client := newDataConnection(t)
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte("CREATE TABLE settings (id INTEGER PRIMARY KEY, body TEXT)"), 0)})
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`INSERT INTO settings (id, body) VALUES (1, '{"theme":"dark","widgets":[1,2]}')`), 0)})
- 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)})
- values := dataRowValues(t, msgs[1])
- if len(values) != 3 || values[0] != "dark" || values[1] != "object" || values[2] != "1" {
- t.Fatalf("json round trip = %v, want [dark object 1]", values)
- }
- // Partial update through json_set, without rewriting the whole document.
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`UPDATE settings SET body = json_set(body, '$.theme', 'light') WHERE id = 1`), 0)})
- 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)})
- values = dataRowValues(t, msgs[1])
- if len(values) != 2 || values[0] != "light" || values[1] != "2" {
- t.Fatalf("json partial update = %v, want [light 2]", values)
- }
- // A JSON column keeps the text-backed JSON1 behavior (native JSONB is
- // covered separately).
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte("CREATE TABLE docs (id INTEGER PRIMARY KEY, body JSON)"), 0)})
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`INSERT INTO docs (id, body) VALUES (1, '{"a":1}')`), 0)})
- msgs = runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_extract(body, '$.a') FROM docs`), 0)})
- if got := dataRowValues(t, msgs[1]); len(got) != 1 || got[0] != "1" {
- t.Fatalf("JSON column extract = %v, want [1]", got)
- }
- }
- func TestJSONInvalidInputOverSimpleQuery(t *testing.T) {
- c, client := newDataConnection(t)
- msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_extract('not json', '$.a')`), 0)})
- if len(msgs) != 2 || msgs[0].Type != MsgErrorResponse {
- t.Fatalf("expected ErrorResponse+ReadyForQuery, got %v", msgs)
- }
- if msg := jsonErrorField(t, msgs[0], ErrorFieldMessage); !strings.Contains(msg, "malformed JSON") {
- t.Fatalf("error message = %q, want a malformed-JSON error", msg)
- }
- // json_valid is the non-throwing way to probe a value.
- msgs = runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT json_valid('not json')`), 0)})
- if got := dataRowValues(t, msgs[1]); len(got) != 1 || got[0] != "0" {
- t.Fatalf("json_valid = %v, want [0]", got)
- }
- }
- func TestJSONArrowOperatorsOverSimpleQuery(t *testing.T) {
- c, client := newDataConnection(t)
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte("CREATE TABLE settings (id INTEGER PRIMARY KEY, body TEXT)"), 0)})
- runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`INSERT INTO settings (id, body) VALUES (1, '{"theme":"dark","widgets":[1,2]}')`), 0)})
- msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT body ->> 'theme', body -> 'widgets' -> 0 FROM settings WHERE id = 1`), 0)})
- values := dataRowValues(t, msgs[1])
- if len(values) != 2 || values[0] != "dark" || values[1] != "1" {
- t.Fatalf("arrow operators = %v, want [dark 1]", values)
- }
- // Malformed JSON through an arrow operator is the same error json_extract throws.
- msgs = runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(`SELECT 'not json' ->> 'a'`), 0)})
- if len(msgs) != 2 || msgs[0].Type != MsgErrorResponse {
- t.Fatalf("expected ErrorResponse+ReadyForQuery, got %v", msgs)
- }
- if msg := jsonErrorField(t, msgs[0], ErrorFieldMessage); !strings.Contains(msg, "malformed JSON") {
- t.Fatalf("error message = %q, want a malformed-JSON error", msg)
- }
- }
|