()
| 1817 | |
| 1818 | #[test] |
| 1819 | fn test_json_agg_functions() { |
| 1820 | let conn = Connection::open_in_memory().unwrap(); |
| 1821 | register_json_functions(&conn).unwrap(); |
| 1822 | |
| 1823 | // Create test data |
| 1824 | conn.execute_batch(r#" |
| 1825 | CREATE TABLE test_agg (id INTEGER, name TEXT, score INTEGER); |
| 1826 | INSERT INTO test_agg VALUES (1, 'Alice', 95); |
| 1827 | INSERT INTO test_agg VALUES (2, 'Bob', 87); |
| 1828 | INSERT INTO test_agg VALUES (3, 'Charlie', 92); |
| 1829 | "#).unwrap(); |
| 1830 | |
| 1831 | // Test json_agg with simple values |
| 1832 | let result: String = conn.query_row( |
| 1833 | "SELECT json_agg(name) FROM test_agg ORDER BY id", |
| 1834 | [], |
| 1835 | |row| row.get(0) |
| 1836 | ).unwrap(); |
| 1837 | |
| 1838 | let parsed: JsonValue = serde_json::from_str(&result).unwrap(); |
| 1839 | match parsed { |
| 1840 | JsonValue::Array(arr) => { |
| 1841 | assert_eq!(arr.len(), 3); |
| 1842 | assert_eq!(arr[0], JsonValue::String("Alice".to_string())); |
| 1843 | assert_eq!(arr[1], JsonValue::String("Bob".to_string())); |
| 1844 | assert_eq!(arr[2], JsonValue::String("Charlie".to_string())); |
| 1845 | } |
| 1846 | _ => panic!("Expected JSON array"), |
| 1847 | } |
| 1848 | |
| 1849 | // Test json_agg with numbers |
| 1850 | let result: String = conn.query_row( |
| 1851 | "SELECT json_agg(score) FROM test_agg ORDER BY id", |
| 1852 | [], |
| 1853 | |row| row.get(0) |
| 1854 | ).unwrap(); |
| 1855 | |
| 1856 | let parsed: JsonValue = serde_json::from_str(&result).unwrap(); |
| 1857 | match parsed { |
| 1858 | JsonValue::Array(arr) => { |
| 1859 | assert_eq!(arr.len(), 3); |
| 1860 | assert_eq!(arr[0], JsonValue::Number(serde_json::Number::from(95))); |
| 1861 | assert_eq!(arr[1], JsonValue::Number(serde_json::Number::from(87))); |
| 1862 | assert_eq!(arr[2], JsonValue::Number(serde_json::Number::from(92))); |
| 1863 | } |
| 1864 | _ => panic!("Expected JSON array"), |
| 1865 | } |
| 1866 | |
| 1867 | // Test jsonb_agg (should behave identically) |
| 1868 | let result: String = conn.query_row( |
| 1869 | "SELECT jsonb_agg(name) FROM test_agg ORDER BY id", |
| 1870 | [], |
| 1871 | |row| row.get(0) |
| 1872 | ).unwrap(); |
| 1873 | |
| 1874 | let parsed: JsonValue = serde_json::from_str(&result).unwrap(); |
| 1875 | match parsed { |
| 1876 | JsonValue::Array(arr) => { |
nothing calls this directly
no test coverage detected