b8d198f150
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
71 lines
2.1 KiB
Rust
71 lines
2.1 KiB
Rust
use search::{SearchClient, SearchDocument};
|
|
|
|
fn meili() -> (String, String) {
|
|
(
|
|
std::env::var("MEILI_URL").expect("MEILI_URL must be set"),
|
|
std::env::var("MEILI_MASTER_KEY").expect("MEILI_MASTER_KEY must be set"),
|
|
)
|
|
}
|
|
|
|
fn unique_index() -> String {
|
|
format!("objects_test_{}", uuid::Uuid::new_v4().simple())
|
|
}
|
|
|
|
fn doc(id: &str, object_name: &str, fields_text: &[&str]) -> SearchDocument {
|
|
SearchDocument {
|
|
id: id.to_string(),
|
|
object_number: format!("N-{id}"),
|
|
object_name: object_name.to_string(),
|
|
brief_description: None,
|
|
current_owner: None,
|
|
recorder: None,
|
|
visibility: "draft".to_string(),
|
|
fields_text: fields_text.iter().map(|s| s.to_string()).collect(),
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn index_search_and_remove() {
|
|
let (url, key) = meili();
|
|
let client = SearchClient::connect(&url, &key, &unique_index()).unwrap();
|
|
client.ensure_index().await.unwrap();
|
|
|
|
let vase = domain::ObjectId::new();
|
|
let chair = domain::ObjectId::new();
|
|
client
|
|
.index_object(&doc(&vase.to_string(), "vase", &["wood", "trä"]))
|
|
.await
|
|
.unwrap();
|
|
client
|
|
.index_object(&doc(&chair.to_string(), "chair", &["oak"]))
|
|
.await
|
|
.unwrap();
|
|
|
|
let hits = client.search("wood").await.unwrap();
|
|
assert_eq!(hits, vec![vase]);
|
|
|
|
let hits = client.search("chair").await.unwrap();
|
|
assert_eq!(hits, vec![chair]);
|
|
|
|
client.remove_object(vase).await.unwrap();
|
|
assert!(client.search("wood").await.unwrap().is_empty());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn ensure_index_is_idempotent() {
|
|
let (url, key) = meili();
|
|
let index = unique_index();
|
|
let client = SearchClient::connect(&url, &key, &index).unwrap();
|
|
client.ensure_index().await.unwrap();
|
|
// second call against the now-existing index must succeed
|
|
client.ensure_index().await.unwrap();
|
|
|
|
// and the client still works
|
|
let id = domain::ObjectId::new();
|
|
client
|
|
.index_object(&doc(&id.to_string(), "lamp", &[]))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(client.search("lamp").await.unwrap(), vec![id]);
|
|
}
|