()
| 2326 | |
| 2327 | #[test] |
| 2328 | fn test_json_to_record_function() { |
| 2329 | let conn = Connection::open_in_memory().unwrap(); |
| 2330 | register_json_functions(&conn).unwrap(); |
| 2331 | |
| 2332 | // Test with simple JSON object |
| 2333 | let json_data = r#"{"name": "Alice", "age": 25, "active": true}"#; |
| 2334 | let result: Option<String> = conn.query_row( |
| 2335 | "SELECT json_to_record(?)", |
| 2336 | [json_data], |
| 2337 | |row| row.get(0) |
| 2338 | ).unwrap(); |
| 2339 | |
| 2340 | assert!(result.is_some()); |
| 2341 | let result_str = result.unwrap(); |
| 2342 | assert!(result_str.starts_with('(')); |
| 2343 | assert!(result_str.ends_with(')')); |
| 2344 | assert!(result_str.contains("name:Alice")); |
| 2345 | assert!(result_str.contains("age:25")); |
| 2346 | assert!(result_str.contains("active:true")); |
| 2347 | |
| 2348 | // Test with object containing different data types |
| 2349 | let complex_json = r#"{"id": 123, "title": "Test", "enabled": false, "data": null}"#; |
| 2350 | let result: Option<String> = conn.query_row( |
| 2351 | "SELECT json_to_record(?)", |
| 2352 | [complex_json], |
| 2353 | |row| row.get(0) |
| 2354 | ).unwrap(); |
| 2355 | |
| 2356 | assert!(result.is_some()); |
| 2357 | let result_str = result.unwrap(); |
| 2358 | assert!(result_str.contains("id:123")); |
| 2359 | assert!(result_str.contains("title:Test")); |
| 2360 | assert!(result_str.contains("enabled:false")); |
| 2361 | assert!(result_str.contains("data:null")); |
| 2362 | |
| 2363 | // Test with empty object |
| 2364 | let empty_obj = "{}"; |
| 2365 | let result: Option<String> = conn.query_row( |
| 2366 | "SELECT json_to_record(?)", |
| 2367 | [empty_obj], |
| 2368 | |row| row.get(0) |
| 2369 | ).unwrap(); |
| 2370 | |
| 2371 | assert_eq!(result, Some("()".to_string())); |
| 2372 | |
| 2373 | // Test with array (should return error message) |
| 2374 | let array_json = r#"[{"name": "test"}]"#; |
| 2375 | let result: Option<String> = conn.query_row( |
| 2376 | "SELECT json_to_record(?)", |
| 2377 | [array_json], |
| 2378 | |row| row.get(0) |
| 2379 | ).unwrap(); |
| 2380 | |
| 2381 | assert!(result.is_some()); |
| 2382 | let result_str = result.unwrap(); |
| 2383 | assert!(result_str.contains("input must be a JSON object")); |
| 2384 | |
| 2385 | // Test with invalid JSON |
nothing calls this directly
no test coverage detected