#![cfg(feature = "text-index")]
use grafeo_common::types::Value;
use grafeo_engine::GrafeoDB;
fn setup_db_with_text_index() -> GrafeoDB {
let db = GrafeoDB::new_in_memory();
db.create_text_index("Doc", "content").unwrap();
db
}
#[test]
fn test_text_index_auto_insert_via_create_node() {
let db = setup_db_with_text_index();
let id = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("The quick brown fox jumps over the lazy dog".into()),
)],
);
let results = db
.text_search("Doc", "content", "quick brown fox", 10)
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, id);
}
#[test]
fn test_text_index_auto_update_via_set_property() {
let db = setup_db_with_text_index();
let id = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("original text about databases".into()),
)],
);
let results = db.text_search("Doc", "content", "databases", 10).unwrap();
assert_eq!(results.len(), 1);
db.set_node_property(
id,
"content",
Value::String("updated text about graph theory".into()),
);
let results = db.text_search("Doc", "content", "databases", 10).unwrap();
assert!(
results.is_empty(),
"Old text should not be found after update"
);
let results = db
.text_search("Doc", "content", "graph theory", 10)
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, id);
}
#[test]
fn test_text_index_auto_delete() {
let db = setup_db_with_text_index();
let id = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("searchable document content".into()),
)],
);
let results = db.text_search("Doc", "content", "searchable", 10).unwrap();
assert_eq!(results.len(), 1);
let deleted = db.delete_node(id);
assert!(deleted);
let results = db.text_search("Doc", "content", "searchable", 10).unwrap();
assert!(
results.is_empty(),
"Deleted node should not appear in text search"
);
}
#[test]
fn test_text_index_add_label() {
let db = GrafeoDB::new_in_memory();
db.create_text_index("Article", "content").unwrap();
let id = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("important article about Rust programming".into()),
)],
);
let results = db.text_search("Article", "content", "Rust", 10).unwrap();
assert!(results.is_empty());
let added = db.add_node_label(id, "Article");
assert!(added);
let results = db
.text_search("Article", "content", "Rust programming", 10)
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, id);
}
#[test]
fn test_text_index_non_string_property_ignored() {
let db = setup_db_with_text_index();
let id = db.create_node_with_props(&["Doc"], [("content", Value::Int64(42))]);
let results = db.text_search("Doc", "content", "42", 10).unwrap();
assert!(
results.is_empty(),
"Non-string values should not be indexed"
);
db.set_node_property(
id,
"content",
Value::String("now it is a string value".into()),
);
let results = db
.text_search("Doc", "content", "string value", 10)
.unwrap();
assert_eq!(results.len(), 1);
}
#[test]
fn test_text_index_multiple_nodes() {
let db = setup_db_with_text_index();
let _id1 = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("machine learning with neural networks".into()),
)],
);
let _id2 = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("graph databases and knowledge graphs".into()),
)],
);
let id3 = db.create_node_with_props(
&["Doc"],
[(
"content",
Value::String("machine learning on graphs".into()),
)],
);
let results = db
.text_search("Doc", "content", "machine learning", 10)
.unwrap();
assert_eq!(results.len(), 2);
db.delete_node(id3);
let results = db
.text_search("Doc", "content", "machine learning", 10)
.unwrap();
assert_eq!(results.len(), 1);
}