erpc_analysis/graph/
projections.rs
use std::collections::HashMap;
pub fn build_gds_drop_cypher(projection_name: &str) -> String {
format!(
"CALL gds.graph.drop('{}', false) YIELD graphName",
projection_name
)
}
pub fn build_gds_project_cypher(
projection_name: &str,
node_label: &str,
relationship_types: &HashMap<String, String>,
_relationship_properties_to_project: Option<&[String]>,
) -> String {
let relationships_cypher_map_entries: Vec<String> = relationship_types
.iter()
.map(|(rel_type, orientation)| {
format!("{}: {{ orientation: '{}' }}", rel_type, orientation)
})
.collect();
let relationships_cypher_map =
format!("{{ {} }}", relationships_cypher_map_entries.join(", "));
let relationship_properties_cypher_map = "{}";
format!(
"CALL gds.graph.project('{}', '{}', {}, {}) YIELD graphName, \
nodeCount, relationshipCount, projectMillis",
projection_name,
node_label,
relationships_cypher_map,
relationship_properties_cypher_map
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_gds_drop_cypher() {
let projection_name = "test_projection";
let result = build_gds_drop_cypher(projection_name);
let expected =
"CALL gds.graph.drop('test_projection', false) YIELD graphName";
assert_eq!(result, expected);
}
#[test]
fn test_build_gds_drop_cypher_with_special_characters() {
let projection_name = "test-projection_123";
let result = build_gds_drop_cypher(projection_name);
let expected = "CALL gds.graph.drop('test-projection_123', false) \
YIELD graphName";
assert_eq!(result, expected);
}
#[test]
fn test_build_gds_project_cypher_single_relationship() {
let projection_name = "tor_network";
let node_label = "Relay";
let mut relationship_types = HashMap::new();
relationship_types
.insert("CIRCUIT_SUCCESS".to_string(), "NATURAL".to_string());
let result = build_gds_project_cypher(
projection_name,
node_label,
&relationship_types,
None,
);
let expected = "CALL gds.graph.project('tor_network', 'Relay', \
{ CIRCUIT_SUCCESS: { orientation: 'NATURAL' } }, {}) \
YIELD graphName, nodeCount, relationshipCount, \
projectMillis";
assert_eq!(result, expected);
}
#[test]
fn test_build_gds_project_cypher_multiple_relationships() {
let projection_name = "complex_network";
let node_label = "Node";
let mut relationship_types = HashMap::new();
relationship_types
.insert("SUCCESS".to_string(), "NATURAL".to_string());
relationship_types
.insert("FAILURE".to_string(), "REVERSE".to_string());
relationship_types
.insert("BIDIRECTIONAL".to_string(), "UNDIRECTED".to_string());
let result = build_gds_project_cypher(
projection_name,
node_label,
&relationship_types,
None,
);
assert!(result
.contains("CALL gds.graph.project('complex_network', 'Node'"));
assert!(result.contains("SUCCESS: { orientation: 'NATURAL' }"));
assert!(result.contains("FAILURE: { orientation: 'REVERSE' }"));
assert!(
result.contains("BIDIRECTIONAL: { orientation: 'UNDIRECTED' }")
);
assert!(result.contains(
"YIELD graphName, nodeCount, relationshipCount, projectMillis"
));
}
#[test]
fn test_build_gds_project_cypher_empty_relationships() {
let projection_name = "empty_rels";
let node_label = "EmptyNode";
let relationship_types = HashMap::new();
let result = build_gds_project_cypher(
projection_name,
node_label,
&relationship_types,
None,
);
let expected = "CALL gds.graph.project('empty_rels', 'EmptyNode', \
{ }, {}) YIELD graphName, nodeCount, \
relationshipCount, projectMillis";
assert_eq!(result, expected);
}
}