TestCopyFromStdinMultilineJSON tests COPY with multi-line JSON Note: Due to using DuckDB's CSV parser (not PostgreSQL's text format parser), escape sequences in the data are stored literally. Real multi-line data that contains actual newline bytes works correctly.
(t *testing.T)
| 268 | // escape sequences in the data are stored literally. Real multi-line data |
| 269 | // that contains actual newline bytes works correctly. |
| 270 | func TestCopyFromStdinMultilineJSON(t *testing.T) { |
| 271 | mustExec(t, testHarness.DuckgresDB, "DROP TABLE IF EXISTS copy_json_test") |
| 272 | mustExec(t, testHarness.DuckgresDB, "CREATE TABLE copy_json_test (id INTEGER, metadata TEXT)") |
| 273 | |
| 274 | t.Run("copy_json_single_line", func(t *testing.T) { |
| 275 | // Test with single-line JSON (this should work correctly) |
| 276 | txn, err := testHarness.DuckgresDB.Begin() |
| 277 | if err != nil { |
| 278 | t.Fatalf("Failed to begin transaction: %v", err) |
| 279 | } |
| 280 | |
| 281 | stmt, err := txn.Prepare(pq.CopyIn("copy_json_test", "id", "metadata")) |
| 282 | if err != nil { |
| 283 | _ = txn.Rollback() |
| 284 | t.Fatalf("Failed to prepare COPY: %v", err) |
| 285 | } |
| 286 | |
| 287 | // Single-line JSON |
| 288 | jsonData := `{"subscription":"active","plan":"pro","features":["feature1","feature2"]}` |
| 289 | |
| 290 | _, err = stmt.Exec(1, jsonData) |
| 291 | if err != nil { |
| 292 | _ = stmt.Close() |
| 293 | _ = txn.Rollback() |
| 294 | t.Fatalf("Failed to exec COPY with JSON: %v", err) |
| 295 | } |
| 296 | |
| 297 | _, err = stmt.Exec() |
| 298 | if err != nil { |
| 299 | _ = stmt.Close() |
| 300 | _ = txn.Rollback() |
| 301 | t.Fatalf("Failed to flush COPY: %v", err) |
| 302 | } |
| 303 | |
| 304 | _ = stmt.Close() |
| 305 | _ = txn.Commit() |
| 306 | |
| 307 | // Verify the JSON was stored correctly |
| 308 | var stored string |
| 309 | err = testHarness.DuckgresDB.QueryRow( |
| 310 | "SELECT metadata FROM copy_json_test WHERE id = 1").Scan(&stored) |
| 311 | if err != nil { |
| 312 | t.Fatalf("Failed to query: %v", err) |
| 313 | } |
| 314 | if stored != jsonData { |
| 315 | t.Errorf("JSON not stored correctly.\nExpected: %s\nGot: %s", jsonData, stored) |
| 316 | } |
| 317 | }) |
| 318 | |
| 319 | // Cleanup |
| 320 | mustExec(t, testHarness.DuckgresDB, "DROP TABLE IF EXISTS copy_json_test") |
| 321 | } |
| 322 | |
| 323 | // TestCopyToStdout tests COPY TO STDOUT functionality |
| 324 | // Note: lib/pq's Query() doesn't support COPY TO STDOUT directly because |