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

Function TestEncodeJSON

server/types_test.go:1396–1445  ·  view source on GitHub ↗

TestEncodeJSON verifies that encodeJSON re-serializes Go native values back to valid JSON text. The Go DuckDB driver's getJSON() deserializes JSON via json.Unmarshal (e.g., JSON string "hello" becomes Go string hello without quotes). Without re-serialization, bare strings like hello are sent on the

(t *testing.T)

Source from the content-addressed store, hash-verified

1394// quotes). Without re-serialization, bare strings like hello are sent on the
1395// wire and CAST(x AS JSON) fails because hello is not valid JSON.
1396func TestEncodeJSON(t *testing.T) {
1397 tests := []struct {
1398 name string
1399 input interface{} // Go native value (as returned by DuckDB driver's getJSON)
1400 expected string // Expected JSON text on the wire
1401 }{
1402 {"string", "hello", `"hello"`},
1403 {"empty string", "", `""`},
1404 {"string with quotes", `say "hi"`, `"say \"hi\""`},
1405 {"integer", float64(42), "42"},
1406 {"float", float64(3.14), "3.14"},
1407 {"boolean true", true, "true"},
1408 {"boolean false", false, "false"},
1409 {"null", nil, "null"},
1410 {"object", map[string]interface{}{"name": "Alice", "age": float64(30)}, ""}, // checked separately
1411 {"array", []interface{}{float64(1), float64(2), float64(3)}, "[1,2,3]"},
1412 }
1413
1414 for _, tt := range tests {
1415 t.Run(tt.name, func(t *testing.T) {
1416 result := encodeJSON(tt.input)
1417 if result == nil {
1418 t.Fatal("encodeJSON returned nil")
1419 }
1420 got := string(result)
1421
1422 if tt.name == "object" {
1423 // Object key order is non-deterministic; verify it's valid JSON with expected keys
1424 var parsed map[string]interface{}
1425 if err := json.Unmarshal(result, &parsed); err != nil {
1426 t.Fatalf("encodeJSON produced invalid JSON: %s", got)
1427 }
1428 if parsed["name"] != "Alice" || parsed["age"] != float64(30) {
1429 t.Errorf("encodeJSON object content wrong: %s", got)
1430 }
1431 return
1432 }
1433
1434 if got != tt.expected {
1435 t.Errorf("encodeJSON(%v) = %s, want %s", tt.input, got, tt.expected)
1436 }
1437
1438 // Verify the output is valid JSON (the whole point of this function)
1439 var v interface{}
1440 if err := json.Unmarshal(result, &v); err != nil {
1441 t.Errorf("encodeJSON(%v) produced invalid JSON %q: %v", tt.input, got, err)
1442 }
1443 })
1444 }
1445}
1446
1447// TestEncodeBinaryJSON verifies that encodeBinary routes JSON/JSONB OIDs
1448// through encodeJSON rather than the default text encoder.

Callers

nothing calls this directly

Calls 2

encodeJSONFunction · 0.85
RunMethod · 0.65

Tested by

no test coverage detected