#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test code — panics are acceptable failures"
)]
use chrono::Utc;
use cognee_database::ops::datasets::create_dataset;
use cognee_database::ops::graph_storage::{upsert_edges, upsert_nodes};
use cognee_database::{GraphEdge, GraphNode, connect, initialize};
use cognee_models::Dataset;
use serde_json::json;
use uuid::Uuid;
#[tokio::test]
async fn upserts_large_graph_without_variable_overflow() {
let db = connect("sqlite::memory:").await.expect("connect");
initialize(&db).await.expect("migrate");
let user = Uuid::new_v4();
let dataset = Uuid::new_v4();
create_dataset(&db, Dataset::new("war-peace".into(), user, None, dataset))
.await
.expect("seed dataset");
let n = 4000usize;
let data = Uuid::new_v4();
let nodes: Vec<GraphNode> = (0..n)
.map(|_| GraphNode {
id: Uuid::new_v4(),
slug: Uuid::new_v4(),
user_id: user,
data_id: data,
dataset_id: dataset,
label: Some("n".into()),
node_type: "Entity".into(),
indexed_fields: json!({ "index_fields": ["name"] }),
attributes: None,
created_at: Utc::now(),
})
.collect();
upsert_nodes(&db, &nodes)
.await
.expect("node upsert must chunk under the SQL-variable cap");
let edges: Vec<GraphEdge> = (0..n)
.map(|i| GraphEdge {
id: Uuid::new_v4(),
slug: Uuid::new_v4(),
user_id: user,
data_id: data,
dataset_id: dataset,
source_node_id: nodes[i].id,
destination_node_id: nodes[(i + 1) % n].id,
relationship_name: "rel".into(),
label: Some("e".into()),
attributes: None,
created_at: Utc::now(),
})
.collect();
upsert_edges(&db, &edges)
.await
.expect("edge upsert must chunk under the SQL-variable cap");
}
#[cfg(feature = "postgres")]
mod postgres {
use super::*;
use cognee_database::ops::graph_storage::{get_edges_by_data, get_nodes_by_data};
#[tokio::test]
async fn upsert_batch_with_duplicate_ids_dedups_keeping_last() {
let Some(base_url) = cognee_test_utils::test_postgres_url() else {
eprintln!(
"TEST_POSTGRES_URL not set — skipping upsert_batch_with_duplicate_ids_dedups_keeping_last"
);
return;
};
let tmp = cognee_test_utils::create_temp_postgres_db(&base_url)
.await
.expect("create temp Postgres database");
let db = connect(tmp.url()).await.expect("connect to temp Postgres");
initialize(&db).await.expect("migrate relational schema");
let user = Uuid::new_v4();
let dataset = Uuid::new_v4();
let data = Uuid::new_v4();
create_dataset(&db, Dataset::new("dup-upsert".into(), user, None, dataset))
.await
.expect("seed dataset");
let node_id = Uuid::new_v4();
let mk_node = |label: &str| GraphNode {
id: node_id,
slug: Uuid::new_v4(),
user_id: user,
data_id: data,
dataset_id: dataset,
label: Some(label.into()),
node_type: "Entity".into(),
indexed_fields: json!({ "index_fields": ["name"] }),
attributes: None,
created_at: Utc::now(),
};
upsert_nodes(&db, &[mk_node("first"), mk_node("last")])
.await
.expect("node upsert with a duplicate id in the batch must succeed on Postgres");
let stored = get_nodes_by_data(&db, data, dataset)
.await
.expect("read back nodes");
assert_eq!(
stored.len(),
1,
"duplicate id must collapse to a single node row"
);
assert_eq!(
stored[0].label.as_deref(),
Some("last"),
"the LAST occurrence in the batch must win the upsert"
);
let a_id = Uuid::new_v4();
let b_id = Uuid::new_v4();
let a = GraphNode {
id: a_id,
..mk_node("endpoint-a")
};
let b = GraphNode {
id: b_id,
..mk_node("endpoint-b")
};
upsert_nodes(&db, &[a, b])
.await
.expect("seed edge endpoints");
let edge_id = Uuid::new_v4();
let mk_edge = |rel: &str| GraphEdge {
id: edge_id,
slug: Uuid::new_v4(),
user_id: user,
data_id: data,
dataset_id: dataset,
source_node_id: a_id,
destination_node_id: b_id,
relationship_name: rel.into(),
label: Some(rel.into()),
attributes: None,
created_at: Utc::now(),
};
upsert_edges(&db, &[mk_edge("first_rel"), mk_edge("last_rel")])
.await
.expect("edge upsert with a duplicate id in the batch must succeed on Postgres");
let stored_edges = get_edges_by_data(&db, data, dataset)
.await
.expect("read back edges");
assert_eq!(
stored_edges.len(),
1,
"duplicate id must collapse to a single edge row"
);
assert_eq!(
stored_edges[0].relationship_name, "last_rel",
"the LAST occurrence in the batch must win the upsert"
);
drop(db);
tmp.cleanup().await;
}
#[tokio::test]
async fn temp_database_supports_back_to_back_initialize() {
let Some(base_url) = cognee_test_utils::test_postgres_url() else {
eprintln!(
"TEST_POSTGRES_URL not set — skipping temp_database_supports_back_to_back_initialize"
);
return;
};
for run in 0..2 {
let tmp = cognee_test_utils::create_temp_postgres_db(&base_url)
.await
.unwrap_or_else(|e| panic!("create temp DB (run {run}): {e}"));
let db = connect(tmp.url())
.await
.unwrap_or_else(|e| panic!("connect (run {run}): {e}"));
initialize(&db)
.await
.unwrap_or_else(|e| panic!("initialize on a fresh database (run {run}): {e}"));
create_dataset(
&db,
Dataset::new(format!("probe-{run}"), Uuid::new_v4(), None, Uuid::new_v4()),
)
.await
.unwrap_or_else(|e| panic!("seed (run {run}): {e}"));
drop(db);
tmp.cleanup().await;
}
}
}