()
| 1910 | |
| 1911 | #[test] |
| 1912 | fn test_jsonb_insert_function() { |
| 1913 | let conn = Connection::open_in_memory().unwrap(); |
| 1914 | register_json_functions(&conn).unwrap(); |
| 1915 | |
| 1916 | // Test inserting into object |
| 1917 | let test_json = r#"{"name": "John", "age": 30}"#; |
| 1918 | let result: Option<String> = conn.query_row( |
| 1919 | "SELECT jsonb_insert(?, ?, ?)", |
| 1920 | [test_json, "{email}", "\"john@example.com\""], |
| 1921 | |row| row.get(0) |
| 1922 | ).unwrap(); |
| 1923 | |
| 1924 | assert!(result.is_some()); |
| 1925 | let parsed: JsonValue = serde_json::from_str(&result.unwrap()).unwrap(); |
| 1926 | match parsed { |
| 1927 | JsonValue::Object(map) => { |
| 1928 | assert_eq!(map.get("name"), Some(&JsonValue::String("John".to_string()))); |
| 1929 | assert_eq!(map.get("age"), Some(&JsonValue::Number(serde_json::Number::from(30)))); |
| 1930 | assert_eq!(map.get("email"), Some(&JsonValue::String("john@example.com".to_string()))); |
| 1931 | } |
| 1932 | _ => panic!("Expected JSON object"), |
| 1933 | } |
| 1934 | |
| 1935 | // Test inserting into nested object |
| 1936 | let nested_json = r#"{"user": {"name": "Alice"}, "active": true}"#; |
| 1937 | let result: Option<String> = conn.query_row( |
| 1938 | "SELECT jsonb_insert(?, ?, ?)", |
| 1939 | [nested_json, "{user,email}", "\"alice@example.com\""], |
| 1940 | |row| row.get(0) |
| 1941 | ).unwrap(); |
| 1942 | |
| 1943 | assert!(result.is_some()); |
| 1944 | let parsed: JsonValue = serde_json::from_str(&result.unwrap()).unwrap(); |
| 1945 | match parsed { |
| 1946 | JsonValue::Object(map) => { |
| 1947 | if let Some(JsonValue::Object(user_map)) = map.get("user") { |
| 1948 | assert_eq!(user_map.get("name"), Some(&JsonValue::String("Alice".to_string()))); |
| 1949 | assert_eq!(user_map.get("email"), Some(&JsonValue::String("alice@example.com".to_string()))); |
| 1950 | } else { |
| 1951 | panic!("Expected nested user object"); |
| 1952 | } |
| 1953 | } |
| 1954 | _ => panic!("Expected JSON object"), |
| 1955 | } |
| 1956 | |
| 1957 | // Test inserting into array (before index) |
| 1958 | let array_json = r#"["apple", "banana", "cherry"]"#; |
| 1959 | let result: Option<String> = conn.query_row( |
| 1960 | "SELECT jsonb_insert(?, ?, ?)", |
| 1961 | [array_json, "{1}", "\"orange\""], |
| 1962 | |row| row.get(0) |
| 1963 | ).unwrap(); |
| 1964 | |
| 1965 | assert!(result.is_some()); |
| 1966 | let parsed: JsonValue = serde_json::from_str(&result.unwrap()).unwrap(); |
| 1967 | match parsed { |
| 1968 | JsonValue::Array(arr) => { |
| 1969 | assert_eq!(arr.len(), 4); |
nothing calls this directly
no test coverage detected