#[cfg(test)]
mod tests {
use crate::{ApplicationSource, ApplicationSourceConfig, PropertyMapBuilder};
use std::collections::HashMap;
async fn create_test_application_source(
id: &str,
) -> (ApplicationSource, crate::ApplicationSourceHandle) {
let config = ApplicationSourceConfig {
properties: HashMap::new(),
};
ApplicationSource::new(id, config).expect("Failed to create application source")
}
#[tokio::test]
async fn test_application_source_creation() {
let (_source, handle) = create_test_application_source("test-source").await;
assert_eq!(handle.source_id(), "test-source");
}
#[tokio::test]
async fn test_send_node_insert() {
let (_source, handle) = create_test_application_source("test-source").await;
let props = PropertyMapBuilder::new()
.with_string("name", "John")
.with_integer("age", 30)
.build();
let result = handle
.send_node_insert("node-1", vec!["Person"], props)
.await;
assert!(result.is_ok(), "Should send node insert successfully");
}
#[tokio::test]
async fn test_send_node_update() {
let (_source, handle) = create_test_application_source("test-source").await;
let props = PropertyMapBuilder::new()
.with_string("name", "Jane")
.with_integer("age", 25)
.build();
let result = handle
.send_node_update("node-1", vec!["Person"], props)
.await;
assert!(result.is_ok(), "Should send node update successfully");
}
#[tokio::test]
async fn test_send_delete() {
let (_source, handle) = create_test_application_source("test-source").await;
let result = handle.send_delete("node-1", vec!["Person"]).await;
assert!(result.is_ok(), "Should send delete successfully");
}
#[tokio::test]
async fn test_send_relation_insert() {
let (_source, handle) = create_test_application_source("test-source").await;
let props = PropertyMapBuilder::new()
.with_string("since", "2020")
.build();
let result = handle
.send_relation_insert("rel-1", vec!["KNOWS"], props, "node-1", "node-2")
.await;
assert!(result.is_ok(), "Should send relation insert successfully");
}
#[tokio::test]
async fn test_send_batch() {
let (_source, handle) = create_test_application_source("test-source").await;
let changes = vec![];
let result = handle.send_batch(changes).await;
assert!(result.is_ok(), "Should send empty batch successfully");
}
#[tokio::test]
async fn test_property_map_builder_string() {
let props = PropertyMapBuilder::new()
.with_string("key", "value")
.build();
assert!(props.get("key").is_some());
}
#[tokio::test]
async fn test_property_map_builder_integer() {
let props = PropertyMapBuilder::new().with_integer("age", 42).build();
assert!(props.get("age").is_some());
}
#[tokio::test]
async fn test_property_map_builder_bool() {
let props = PropertyMapBuilder::new().with_bool("active", true).build();
assert!(props.get("active").is_some());
}
#[tokio::test]
async fn test_property_map_builder_float() {
let props = PropertyMapBuilder::new().with_float("price", 19.99).build();
assert!(props.get("price").is_some());
}
#[tokio::test]
async fn test_property_map_builder_multiple_types() {
let props = PropertyMapBuilder::new()
.with_string("name", "Product")
.with_integer("quantity", 100)
.with_bool("available", true)
.with_float("price", 29.99)
.build();
assert!(props.get("name").is_some());
assert!(props.get("quantity").is_some());
assert!(props.get("available").is_some());
assert!(props.get("price").is_some());
}
#[tokio::test]
async fn test_property_map_builder_empty() {
let _props = PropertyMapBuilder::new().build();
}
#[tokio::test]
async fn test_property_map_builder_chaining() {
let props = PropertyMapBuilder::new()
.with_string("a", "1")
.with_string("b", "2")
.with_string("c", "3")
.build();
assert!(props.get("a").is_some());
assert!(props.get("b").is_some());
assert!(props.get("c").is_some());
}
#[tokio::test]
async fn test_multiple_node_inserts() {
let (_source, handle) = create_test_application_source("test-source").await;
for i in 1..=5 {
let props = PropertyMapBuilder::new()
.with_string("name", format!("Person-{i}"))
.with_integer("id", i)
.build();
let result = handle
.send_node_insert(format!("node-{i}"), vec!["Person"], props)
.await;
assert!(result.is_ok());
}
}
#[tokio::test]
async fn test_send_with_multiple_labels() {
let (_source, handle) = create_test_application_source("test-source").await;
let props = PropertyMapBuilder::new()
.with_string("name", "MultiLabel")
.build();
let result = handle
.send_node_insert("node-1", vec!["Person", "User", "Admin"], props)
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_property_map_overwrite() {
let props = PropertyMapBuilder::new()
.with_string("key", "value1")
.with_string("key", "value2")
.build();
assert!(props.get("key").is_some());
}
#[tokio::test]
async fn test_handle_clone() {
let (_source, handle) = create_test_application_source("test-source").await;
let handle2 = handle.clone();
assert_eq!(handle.source_id(), handle2.source_id());
let props = PropertyMapBuilder::new()
.with_string("name", "Test")
.build();
assert!(handle
.send_node_insert("node-1", vec!["Person"], props.clone())
.await
.is_ok());
assert!(handle2
.send_node_insert("node-2", vec!["Person"], props)
.await
.is_ok());
}
}