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

Function TestFunctionsJSON

tests/integration/functions_test.go:252–301  ·  view source on GitHub ↗

TestFunctionsJSON tests JSON functions

(t *testing.T)

Source from the content-addressed store, hash-verified

250
251// TestFunctionsJSON tests JSON functions
252func TestFunctionsJSON(t *testing.T) {
253 tests := []QueryTest{
254 // Construction
255 {Name: "json_build_object", Query: "SELECT json_build_object('a', 1, 'b', 2)"},
256 {Name: "json_build_array", Query: "SELECT json_build_array(1, 2, 3)"},
257 {Name: "to_json", Query: "SELECT to_json('hello')"},
258 {Name: "to_jsonb", Query: "SELECT to_jsonb('hello')"},
259 {Name: "array_to_json", Query: "SELECT array_to_json(ARRAY[1, 2, 3])"},
260
261 // Extraction
262 {Name: "json_extract_path", Query: "SELECT json_extract_path('{\"a\": {\"b\": 1}}'::JSON, 'a', 'b')"},
263 {Name: "json_extract_path_text", Query: "SELECT json_extract_path_text('{\"a\": {\"b\": 1}}'::JSON, 'a', 'b')"},
264
265 // Querying
266 {Name: "json_typeof_object", Query: "SELECT json_typeof('{\"a\": 1}'::JSON)"},
267 {Name: "json_typeof_array", Query: "SELECT json_typeof('[1, 2]'::JSON)"},
268 {Name: "json_typeof_string", Query: "SELECT json_typeof('\"hello\"'::JSON)"},
269 {Name: "json_typeof_number", Query: "SELECT json_typeof('42'::JSON)"},
270 {Name: "json_array_length", Query: "SELECT json_array_length('[1, 2, 3]'::JSON)"},
271 {Name: "jsonb_array_length", Query: "SELECT jsonb_array_length('[1, 2, 3]'::JSONB)"},
272
273 // JSONB modification
274 {Name: "jsonb_set", Query: "SELECT jsonb_set('{\"a\": 1}'::JSONB, '{a}', '2')"},
275 // jsonb || semantics matrix (regression for #716: the old
276 // json_merge_patch rewrite replaced arrays instead of concatenating
277 // and deleted null-valued keys instead of keeping them). Each case is
278 // compared against real PostgreSQL output (jsonEqual is key-order
279 // insensitive; Postgres sorts jsonb keys, DuckDB does not).
280 {Name: "jsonb_concat", Query: "SELECT '{\"a\": 1}'::JSONB || '{\"b\": 2}'::JSONB"},
281 {Name: "jsonb_concat_null_value_kept", Query: "SELECT '{\"a\": 1}'::JSONB || '{\"a\": null}'::JSONB"},
282 {Name: "jsonb_concat_shallow_merge", Query: "SELECT '{\"a\": {\"x\": 1}}'::JSONB || '{\"a\": {\"y\": 2}}'::JSONB"},
283 {Name: "jsonb_concat_arrays", Query: "SELECT '[1, 2]'::JSONB || '[3, 4]'::JSONB"},
284 {Name: "jsonb_concat_array_append", Query: "SELECT '[1, 2]'::JSONB || '3'::JSONB"},
285 {Name: "jsonb_concat_array_prepend", Query: "SELECT '3'::JSONB || '[1, 2]'::JSONB"},
286 {Name: "jsonb_concat_scalars", Query: "SELECT '1'::JSONB || '2'::JSONB"},
287 {Name: "jsonb_concat_object_array", Query: "SELECT '{\"a\": 1}'::JSONB || '[1]'::JSONB"},
288 {Name: "jsonb_concat_json_null_element", Query: "SELECT 'null'::JSONB || '[1]'::JSONB"},
289 {Name: "jsonb_concat_sql_null", Query: "SELECT (NULL::JSONB || '[1]'::JSONB) IS NULL"},
290 {Name: "jsonb_delete_key", Query: "SELECT '{\"a\": 1, \"b\": 2}'::JSONB - 'a'"},
291
292 // Expansion functions
293 {Name: "json_each", Query: "SELECT * FROM json_each('{\"a\": 1, \"b\": 2}'::JSON)"},
294 {Name: "jsonb_each", Query: "SELECT * FROM jsonb_each('{\"a\": 1, \"b\": 2}'::JSONB)"},
295 {Name: "json_each_text", Query: "SELECT * FROM json_each_text('{\"a\": 1, \"b\": 2}'::JSON)"},
296 {Name: "json_array_elements", Query: "SELECT * FROM json_array_elements('[1, 2, 3]'::JSON)"},
297 {Name: "jsonb_array_elements", Query: "SELECT * FROM jsonb_array_elements('[1, 2, 3]'::JSONB)"},
298 {Name: "json_array_elements_text", Query: "SELECT * FROM json_array_elements_text('[\"a\", \"b\"]'::JSON)"},
299 }
300 runQueryTests(t, tests)
301}
302
303// TestFunctionsArray tests array functions
304func TestFunctionsArray(t *testing.T) {

Callers

nothing calls this directly

Calls 1

runQueryTestsFunction · 0.85

Tested by

no test coverage detected