use serde_json::json;
use std::fs;
use veclite_core::VecLite;
#[test]
fn insert_and_search() {
let db_path = "test_insert.vec";
let _ = fs::remove_file(db_path);
let mut db = VecLite::open(db_path).unwrap();
db.insert("a", vec![1.0, 0.0], None).unwrap();
db.insert("b", vec![0.0, 1.0], None).unwrap();
let res = db.search(&[1.0, 0.0], 1).unwrap();
assert_eq!(res.len(), 1);
assert_eq!(res[0].id, "a");
let _ = fs::remove_file(db_path);
}
#[test]
fn test_persistence() {
let db_path = "test_persistence.vec";
let _ = fs::remove_file(db_path);
{
let mut db = VecLite::open(db_path).unwrap();
db.insert("persist", vec![0.5, 0.5], Some(json!({"persist": true})))
.unwrap();
}
{
let db = VecLite::open(db_path).unwrap();
let (count, _) = db.stats().unwrap();
assert_eq!(count, 1);
let res = db.search(&[0.5, 0.5], 1).unwrap();
assert_eq!(res[0].id, "persist");
assert_eq!(
res[0]
.metadata
.as_ref()
.unwrap()
.get("persist")
.unwrap()
.as_bool(),
Some(true)
);
}
let _ = fs::remove_file(db_path);
}
#[test]
fn test_metadata_filter() {
let db_path = "test_filter.vec";
let _ = fs::remove_file(db_path);
let mut db = VecLite::open(db_path).unwrap();
db.insert("a", vec![1.0, 1.0], Some(json!({"type": "note"})))
.unwrap();
db.insert("b", vec![1.0, 1.0], Some(json!({"type": "doc"})))
.unwrap();
let res = db
.build_search(&[1.0, 1.0])
.filter("type", "note")
.top_k(10)
.execute()
.unwrap();
assert_eq!(res.len(), 1);
assert_eq!(res[0].id, "a");
let _ = fs::remove_file(db_path);
}