use langchainrust::{
ChromaDBVectorStore, ChromaDBConfig,
VectorStoreError,
Document,
};
#[test]
fn test_chromadb_config_default() {
let config = ChromaDBConfig::default();
assert_eq!(config.host, "http://localhost:8000");
assert_eq!(config.collection_name, "langchainrust");
assert_eq!(config.vector_size, 1536);
}
#[test]
fn test_chromadb_config_new() {
let config = ChromaDBConfig::new("http://10.0.0.1:8000", "test_coll", 768);
assert_eq!(config.host, "http://10.0.0.1:8000");
assert_eq!(config.collection_name, "test_coll");
assert_eq!(config.vector_size, 768);
assert!(config.metadata.is_none());
}
#[test]
fn test_chromadb_config_with_metadata() {
let mut config = ChromaDBConfig::new("http://localhost:8000", "my_coll", 384);
let mut meta = std::collections::HashMap::new();
meta.insert("description".to_string(), "test collection".to_string());
config.metadata = Some(meta);
assert!(config.metadata.is_some());
assert_eq!(
config.metadata.as_ref().unwrap().get("description").unwrap(),
"test collection"
);
}
#[tokio::test]
async fn test_chromadb_connection_refused() {
let config = ChromaDBConfig::new("http://127.0.0.1:19999", "test", 384);
let result = ChromaDBVectorStore::new(config).await;
assert!(result.is_err(), "连接不存在的服务应当返回错误");
}
#[test]
fn test_chromadb_error_display() {
let err = VectorStoreError::ConnectionError("connection refused".to_string());
assert!(err.to_string().contains("connection refused"));
let err = VectorStoreError::StorageError("storage failed".to_string());
assert!(err.to_string().contains("storage failed"));
}
#[test]
fn test_chromadb_document_serde() {
let doc = Document::new("测试内容")
.with_metadata("source", "test")
.with_id("doc-1");
let json = serde_json::to_value(&doc).unwrap();
assert_eq!(json["content"], "测试内容");
assert_eq!(json["metadata"]["source"], "test");
assert_eq!(json["id"], "doc-1");
}
#[test]
fn test_chromadb_distance_to_similarity() {
let sim0: f64 = 1.0 / (1.0 + 0.0);
assert!((sim0 - 1.0).abs() < 1e-6);
let sim1: f64 = 1.0 / (1.0 + 1.0);
assert!((sim1 - 0.5).abs() < 1e-6);
let sim_large: f64 = 1.0 / (1.0 + 1000.0);
assert!(sim_large < 0.01);
}
#[tokio::test]
async fn test_chromadb_add_empty_docs() {
let _config = ChromaDBConfig::new("http://127.0.0.1:19999", "test", 384);
let docs: Vec<Document> = vec![];
let embeddings: Vec<Vec<f32>> = vec![];
assert!(docs.is_empty());
assert!(embeddings.is_empty());
}