use cognee_models::{Entity, EntityType};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GraphNodePair {
pub entity: Entity,
pub entity_type: EntityType,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GraphEdgePair {
pub source_entity_id: Uuid,
pub target_entity_id: Uuid,
pub relationship_name: String,
pub properties: HashMap<String, String>,
}
impl GraphEdgePair {
pub fn new(
source_entity_id: Uuid,
target_entity_id: Uuid,
relationship_name: impl Into<String>,
) -> Self {
Self {
source_entity_id,
target_entity_id,
relationship_name: relationship_name.into(),
properties: HashMap::new(),
}
}
pub fn with_properties(
source_entity_id: Uuid,
target_entity_id: Uuid,
relationship_name: impl Into<String>,
properties: HashMap<String, String>,
) -> Self {
Self {
source_entity_id,
target_entity_id,
relationship_name: relationship_name.into(),
properties,
}
}
pub fn dedup_key(&self) -> (Uuid, Uuid, String) {
(
self.source_entity_id,
self.target_entity_id,
self.relationship_name.clone(),
)
}
pub fn add_property(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.properties.insert(key.into(), value.into());
}
}
#[cfg(test)]
mod tests {
use super::*;
use cognee_models::{Entity, EntityType};
#[test]
fn test_graph_edge_pair_creation() {
let source_id = Uuid::new_v4();
let target_id = Uuid::new_v4();
let edge = GraphEdgePair::new(source_id, target_id, "works_at");
assert_eq!(edge.source_entity_id, source_id);
assert_eq!(edge.target_entity_id, target_id);
assert_eq!(edge.relationship_name, "works_at");
assert!(edge.properties.is_empty());
}
#[test]
fn test_graph_edge_pair_with_properties() {
let source_id = Uuid::new_v4();
let target_id = Uuid::new_v4();
let mut props = HashMap::new();
props.insert("since".to_string(), "2020".to_string());
let edge = GraphEdgePair::with_properties(source_id, target_id, "works_at", props);
assert_eq!(edge.properties.get("since"), Some(&"2020".to_string()));
}
#[test]
fn test_edge_add_property() {
let source_id = Uuid::new_v4();
let target_id = Uuid::new_v4();
let mut edge = GraphEdgePair::new(source_id, target_id, "works_at");
edge.add_property("since", "2020");
assert_eq!(edge.properties.get("since"), Some(&"2020".to_string()));
}
#[test]
fn test_graph_node_pair_structure() {
let entity = Entity::new("TechCorp", None, "A technology company", None);
let entity_type = EntityType::new("Organization", "", None);
let node_pair = GraphNodePair {
entity,
entity_type,
};
assert_eq!(node_pair.entity.name, "TechCorp");
assert_eq!(node_pair.entity_type.name, "Organization");
}
}