use radixtree::RadixTree; use std::collections::HashMap; use tempfile::tempdir; #[test] fn test_getall() -> Result<(), radixtree::Error> { // Create a temporary directory for the test let temp_dir = tempdir().expect("Failed to create temp directory"); let db_path = temp_dir.path().to_str().unwrap(); // Create a new radix tree let mut tree = RadixTree::new(db_path, true)?; // Set up test data with common prefixes let test_data: HashMap<&str, &str> = [ ("user_1", "data1"), ("user_2", "data2"), ("user_3", "data3"), ("admin_1", "admin_data1"), ("admin_2", "admin_data2"), ("guest", "guest_data"), ].iter().cloned().collect(); // Set all test data for (key, value) in &test_data { tree.set(key, value.as_bytes().to_vec())?; } // Test getall with 'user_' prefix let user_values = tree.getall("user_")?; // Should return 3 values assert_eq!(user_values.len(), 3); // Convert byte arrays to strings for easier comparison let user_value_strings: Vec = user_values .iter() .map(|v| String::from_utf8_lossy(v).to_string()) .collect(); // Check all expected values are present assert!(user_value_strings.contains(&"data1".to_string())); assert!(user_value_strings.contains(&"data2".to_string())); assert!(user_value_strings.contains(&"data3".to_string())); // Test getall with 'admin_' prefix let admin_values = tree.getall("admin_")?; // Should return 2 values assert_eq!(admin_values.len(), 2); // Convert byte arrays to strings for easier comparison let admin_value_strings: Vec = admin_values .iter() .map(|v| String::from_utf8_lossy(v).to_string()) .collect(); // Check all expected values are present assert!(admin_value_strings.contains(&"admin_data1".to_string())); assert!(admin_value_strings.contains(&"admin_data2".to_string())); // Test getall with empty prefix (should return all values) let all_values = tree.getall("")?; // Should return all 6 values assert_eq!(all_values.len(), test_data.len()); // Test getall with non-existent prefix let non_existent_values = tree.getall("xyz")?; // Should return empty array assert_eq!(non_existent_values.len(), 0); Ok(()) } #[test] fn test_getall_with_updates() -> Result<(), radixtree::Error> { // Create a temporary directory for the test let temp_dir = tempdir().expect("Failed to create temp directory"); let db_path = temp_dir.path().to_str().unwrap(); // Create a new radix tree let mut tree = RadixTree::new(db_path, true)?; // Set initial values tree.set("key1", b"value1".to_vec())?; tree.set("key2", b"value2".to_vec())?; tree.set("key3", b"value3".to_vec())?; // Get initial values let initial_values = tree.getall("key")?; assert_eq!(initial_values.len(), 3); // Update a value tree.update("key2", b"updated_value2".to_vec())?; // Get values after update let updated_values = tree.getall("key")?; assert_eq!(updated_values.len(), 3); // Convert to strings for easier comparison let updated_value_strings: Vec = updated_values .iter() .map(|v| String::from_utf8_lossy(v).to_string()) .collect(); // Check the updated value is present assert!(updated_value_strings.contains(&"value1".to_string())); assert!(updated_value_strings.contains(&"updated_value2".to_string())); assert!(updated_value_strings.contains(&"value3".to_string())); Ok(()) } #[test] fn test_getall_with_deletions() -> Result<(), radixtree::Error> { // Create a temporary directory for the test let temp_dir = tempdir().expect("Failed to create temp directory"); let db_path = temp_dir.path().to_str().unwrap(); // Create a new radix tree let mut tree = RadixTree::new(db_path, true)?; // Set initial values tree.set("prefix_1", b"value1".to_vec())?; tree.set("prefix_2", b"value2".to_vec())?; tree.set("prefix_3", b"value3".to_vec())?; tree.set("other", b"other_value".to_vec())?; // Get initial values let initial_values = tree.getall("prefix_")?; assert_eq!(initial_values.len(), 3); // Delete a key tree.delete("prefix_2")?; // Get values after deletion let after_delete_values = tree.getall("prefix_")?; assert_eq!(after_delete_values.len(), 2); // Convert to strings for easier comparison let after_delete_strings: Vec = after_delete_values .iter() .map(|v| String::from_utf8_lossy(v).to_string()) .collect(); // Check the remaining values assert!(after_delete_strings.contains(&"value1".to_string())); assert!(after_delete_strings.contains(&"value3".to_string())); Ok(()) }