#![cfg(feature = "real-es")]
use serde_json::json;
use sz_orm_es::real_es::RealEsClient;
use sz_orm_es::{EsDocument, EsError, EsQuery, EsSearchRequest, EsSortOrder, EsSync};
#[test]
fn test_client_new_succeeds() {
let client = RealEsClient::new("http://localhost:9200");
assert!(client.is_ok(), "构造客户端应成功,实际: {:?}", client.err());
}
#[test]
fn test_client_with_auth_succeeds() {
let client = RealEsClient::with_auth("http://localhost:9200", "elastic", "password");
assert!(
client.is_ok(),
"构造带认证客户端应成功,实际: {:?}",
client.err()
);
}
#[test]
fn test_client_invalid_url_fails() {
let result = RealEsClient::new("");
assert!(
matches!(result, Err(EsError::ConnectionFailed(_))),
"空 URL 应返回 ConnectionFailed 错误,实际: {}",
result.err().map(|e| e.to_string()).unwrap_or_default()
);
}
#[test]
fn test_client_as_es_sync_trait_object() {
let client = RealEsClient::new("http://localhost:9200").unwrap();
let _boxed: Box<dyn EsSync> = Box::new(client);
}
#[test]
fn test_es_document_serde_roundtrip() {
let doc = EsDocument::new("test-index", json!({"name": "test", "value": 42})).with_id("doc1");
let serialized = serde_json::to_string(&doc).expect("序列化 EsDocument 失败");
let deserialized: EsDocument =
serde_json::from_str(&serialized).expect("反序列化 EsDocument 失败");
assert_eq!(deserialized.index, "test-index");
assert_eq!(deserialized.id, Some("doc1".to_string()));
assert_eq!(deserialized.source["name"], "test");
assert_eq!(deserialized.source["value"], 42);
}
#[test]
fn test_es_search_request_serde_roundtrip() {
let query = EsQuery::must(vec![
EsQuery::term("status", json!("active")),
EsQuery::range("age", sz_orm_es::EsRangeQuery::new().gte(json!(18))),
]);
let request = EsSearchRequest::new("users", query)
.with_pagination(10, 20)
.with_sort("name", EsSortOrder::Asc);
let serialized = serde_json::to_string(&request).expect("序列化 EsSearchRequest 失败");
let deserialized: EsSearchRequest =
serde_json::from_str(&serialized).expect("反序列化 EsSearchRequest 失败");
assert_eq!(deserialized.index, "users");
assert_eq!(deserialized.from, 10);
assert_eq!(deserialized.size, 20);
assert_eq!(deserialized.sort.len(), 1);
assert_eq!(deserialized.sort[0].field, "name");
assert_eq!(deserialized.sort[0].order, EsSortOrder::Asc);
}
#[test]
fn test_es_query_variants_serde() {
let q = EsQuery::match_all();
let s = serde_json::to_string(&q).unwrap();
let d: EsQuery = serde_json::from_str(&s).unwrap();
assert_eq!(q, d);
let q = EsQuery::term("status", json!("active"));
let s = serde_json::to_string(&q).unwrap();
let d: EsQuery = serde_json::from_str(&s).unwrap();
assert_eq!(q, d);
let q = EsQuery::should(vec![
EsQuery::term("tag", json!("a")),
EsQuery::term("tag", json!("b")),
]);
let s = serde_json::to_string(&q).unwrap();
let _: EsQuery = serde_json::from_str(&s).unwrap();
assert!(!s.is_empty());
}