()
| 1687 | |
| 1688 | #[test] |
| 1689 | fn test_custom_json_path_functions() { |
| 1690 | let conn = Connection::open_in_memory().unwrap(); |
| 1691 | register_json_functions(&conn).unwrap(); |
| 1692 | |
| 1693 | let test_json = r#"{"name": "John", "age": 30, "items": ["item1", "item2"], "address": {"city": "NYC", "zip": "10001"}}"#; |
| 1694 | |
| 1695 | // Test pgsqlite_json_get_text (string key) |
| 1696 | let name: Option<String> = conn.query_row( |
| 1697 | "SELECT pgsqlite_json_get_text(?, ?)", |
| 1698 | [test_json, "name"], |
| 1699 | |row| row.get(0) |
| 1700 | ).unwrap(); |
| 1701 | assert_eq!(name, Some("John".to_string())); |
| 1702 | |
| 1703 | // Test pgsqlite_json_get_json (string key) |
| 1704 | let address: Option<String> = conn.query_row( |
| 1705 | "SELECT pgsqlite_json_get_json(?, ?)", |
| 1706 | [test_json, "address"], |
| 1707 | |row| row.get(0) |
| 1708 | ).unwrap(); |
| 1709 | assert!(address.is_some()); |
| 1710 | assert!(address.unwrap().contains("NYC")); |
| 1711 | |
| 1712 | // Test pgsqlite_json_get_array_text (array index) |
| 1713 | let first_item: Option<String> = conn.query_row( |
| 1714 | "SELECT pgsqlite_json_get_array_text(?, ?)", |
| 1715 | (r#"["item1", "item2", "item3"]"#, 0i64), |
| 1716 | |row| row.get(0) |
| 1717 | ).unwrap(); |
| 1718 | assert_eq!(first_item, Some("item1".to_string())); |
| 1719 | |
| 1720 | // Test pgsqlite_json_get_array_json (array index) |
| 1721 | let second_item: Option<String> = conn.query_row( |
| 1722 | "SELECT pgsqlite_json_get_array_json(?, ?)", |
| 1723 | (r#"["item1", {"nested": "value"}, "item3"]"#, 1i64), |
| 1724 | |row| row.get(0) |
| 1725 | ).unwrap(); |
| 1726 | assert!(second_item.is_some()); |
| 1727 | assert!(second_item.unwrap().contains("nested")); |
| 1728 | |
| 1729 | // Test pgsqlite_json_path_text (path navigation) |
| 1730 | let city: Option<String> = conn.query_row( |
| 1731 | "SELECT pgsqlite_json_path_text(?, ?)", |
| 1732 | [test_json, "address,city"], |
| 1733 | |row| row.get(0) |
| 1734 | ).unwrap(); |
| 1735 | assert_eq!(city, Some("NYC".to_string())); |
| 1736 | |
| 1737 | // Test pgsqlite_json_path_json (path navigation) |
| 1738 | let address_json: Option<String> = conn.query_row( |
| 1739 | "SELECT pgsqlite_json_path_json(?, ?)", |
| 1740 | [test_json, "address"], |
| 1741 | |row| row.get(0) |
| 1742 | ).unwrap(); |
| 1743 | assert!(address_json.is_some()); |
| 1744 | assert!(address_json.unwrap().contains("NYC")); |
| 1745 | |
| 1746 | // Test array access via path |
nothing calls this directly
no test coverage detected