MCPcopy Create free account
hub / github.com/erans/pgsqlite / test_json_functions

Function test_json_functions

src/functions/json_functions.rs:1642–1686  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1640
1641 #[test]
1642 fn test_json_functions() {
1643 let conn = Connection::open_in_memory().unwrap();
1644 register_json_functions(&conn).unwrap();
1645
1646 // Test json_valid
1647 let valid: bool = conn.query_row("SELECT json_valid(?)", ["{\"key\": \"value\"}"], |row| row.get(0)).unwrap();
1648 assert!(valid);
1649
1650 let invalid: bool = conn.query_row("SELECT json_valid(?)", ["{invalid}"], |row| row.get(0)).unwrap();
1651 assert!(!invalid);
1652
1653 // Test json_typeof
1654 let typ: Option<String> = conn.query_row("SELECT json_typeof(?)", ["[1,2,3]"], |row| row.get(0)).unwrap();
1655 assert_eq!(typ, Some("array".to_string()));
1656
1657 let typ: Option<String> = conn.query_row("SELECT json_typeof(?)", ["{\"a\": 1}"], |row| row.get(0)).unwrap();
1658 assert_eq!(typ, Some("object".to_string()));
1659
1660 // Test json_array_length
1661 let len: i64 = conn.query_row("SELECT json_array_length(?)", ["[1,2,3,4,5]"], |row| row.get(0)).unwrap();
1662 assert_eq!(len, 5);
1663
1664 // Test json_extract_scalar
1665 let value: Option<String> = conn.query_row(
1666 "SELECT json_extract_scalar(?, ?)",
1667 ["{\"name\": \"John\", \"age\": 30}", "name"],
1668 |row| row.get(0)
1669 ).unwrap();
1670 assert_eq!(value, Some("John".to_string()));
1671
1672 // Test jsonb_contains
1673 let contains: bool = conn.query_row(
1674 "SELECT jsonb_contains(?, ?)",
1675 ["{\"a\": 1, \"b\": 2}", "{\"a\": 1}"],
1676 |row| row.get(0)
1677 ).unwrap();
1678 assert!(contains);
1679
1680 let not_contains: bool = conn.query_row(
1681 "SELECT jsonb_contains(?, ?)",
1682 ["{\"a\": 1, \"b\": 2}", "{\"c\": 3}"],
1683 |row| row.get(0)
1684 ).unwrap();
1685 assert!(!not_contains);
1686 }
1687
1688 #[test]
1689 fn test_custom_json_path_functions() {

Callers

nothing calls this directly

Calls 2

register_json_functionsFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected