MeiliSearch/meilisearch-http/tests/common/index.rs

245 lines
7.9 KiB
Rust
Raw Normal View History

2021-07-06 11:54:37 +02:00
use std::{
panic::{catch_unwind, resume_unwind, UnwindSafe},
time::Duration,
};
2021-02-18 22:10:50 +01:00
2021-02-18 19:50:52 +01:00
use actix_web::http::StatusCode;
2021-04-20 12:07:22 +02:00
use paste::paste;
2021-02-18 19:50:52 +01:00
use serde_json::{json, Value};
2021-03-10 14:47:04 +01:00
use tokio::time::sleep;
2021-11-03 14:25:49 +01:00
use urlencoding::encode;
2021-02-18 19:50:52 +01:00
use super::service::Service;
2021-04-20 12:07:22 +02:00
macro_rules! make_settings_test_routes {
($($name:ident),+) => {
$(paste! {
pub async fn [<update_$name>](&self, value: Value) -> (Value, StatusCode) {
2021-11-03 14:25:49 +01:00
let url = format!("/indexes/{}/settings/{}", encode(self.uid.as_ref()).to_string(), stringify!($name).replace("_", "-"));
2021-04-20 12:07:22 +02:00
self.service.post(url, value).await
}
pub async fn [<get_$name>](&self) -> (Value, StatusCode) {
2021-11-03 14:25:49 +01:00
let url = format!("/indexes/{}/settings/{}", encode(self.uid.as_ref()).to_string(), stringify!($name).replace("_", "-"));
2021-04-20 12:07:22 +02:00
self.service.get(url).await
}
})*
};
}
2021-02-18 19:50:52 +01:00
pub struct Index<'a> {
pub uid: String,
pub service: &'a Service,
}
2021-04-20 12:07:22 +02:00
#[allow(dead_code)]
2021-02-18 19:50:52 +01:00
impl Index<'_> {
pub async fn get(&self) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}", encode(self.uid.as_ref()));
2021-02-18 19:50:52 +01:00
self.service.get(url).await
}
2021-02-19 19:55:00 +01:00
pub async fn load_test_set(&self) -> u64 {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/documents", encode(self.uid.as_ref()));
2021-03-15 18:11:10 +01:00
let (response, code) = self
.service
.post_str(url, include_str!("../assets/test_set.json"))
.await;
assert_eq!(code, 202);
let update_id = response["uid"].as_i64().unwrap();
self.wait_task(update_id as u64).await;
2021-02-19 19:55:00 +01:00
update_id as u64
}
2021-05-31 16:03:39 +02:00
pub async fn create(&self, primary_key: Option<&str>) -> (Value, StatusCode) {
2021-02-18 19:50:52 +01:00
let body = json!({
"uid": self.uid,
"primaryKey": primary_key,
});
self.service.post("/indexes", body).await
}
2021-02-18 20:28:10 +01:00
pub async fn update(&self, primary_key: Option<&str>) -> (Value, StatusCode) {
let body = json!({
"primaryKey": primary_key,
});
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}", encode(self.uid.as_ref()));
2021-02-18 20:28:10 +01:00
self.service.put(url, body).await
}
2021-02-18 20:44:33 +01:00
pub async fn delete(&self) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}", encode(self.uid.as_ref()));
2021-02-18 20:44:33 +01:00
self.service.delete(url).await
}
2021-02-18 22:10:50 +01:00
pub async fn add_documents(
&self,
documents: Value,
primary_key: Option<&str>,
) -> (Value, StatusCode) {
let url = match primary_key {
2021-11-03 14:25:49 +01:00
Some(key) => format!(
"/indexes/{}/documents?primaryKey={}",
2022-01-12 15:57:31 +01:00
encode(self.uid.as_ref()),
2021-11-03 14:25:49 +01:00
key
),
2022-01-12 15:57:31 +01:00
None => format!("/indexes/{}/documents", encode(self.uid.as_ref())),
2021-02-18 22:10:50 +01:00
};
self.service.post(url, documents).await
}
2021-03-15 18:11:10 +01:00
pub async fn update_documents(
&self,
documents: Value,
primary_key: Option<&str>,
) -> (Value, StatusCode) {
2021-02-19 19:55:00 +01:00
let url = match primary_key {
2021-11-03 14:25:49 +01:00
Some(key) => format!(
"/indexes/{}/documents?primaryKey={}",
2022-01-12 15:57:31 +01:00
encode(self.uid.as_ref()),
2021-11-03 14:25:49 +01:00
key
),
2022-01-12 15:57:31 +01:00
None => format!("/indexes/{}/documents", encode(self.uid.as_ref())),
2021-02-19 19:55:00 +01:00
};
self.service.put(url, documents).await
}
pub async fn wait_task(&self, update_id: u64) -> Value {
2021-02-18 22:10:50 +01:00
// try 10 times to get status, or panic to not wait forever
let url = format!("/tasks/{}", update_id);
2021-02-18 22:10:50 +01:00
for _ in 0..10 {
let (response, status_code) = self.service.get(&url).await;
2021-03-10 18:04:20 +01:00
assert_eq!(status_code, 200, "response: {}", response);
2021-02-18 22:10:50 +01:00
if response["status"] == "succeeded" || response["status"] == "failed" {
2021-02-22 16:03:17 +01:00
return response;
2021-02-18 22:10:50 +01:00
}
2021-03-10 14:47:04 +01:00
sleep(Duration::from_secs(1)).await;
2021-02-18 22:10:50 +01:00
}
panic!("Timeout waiting for update id");
}
pub async fn get_task(&self, update_id: u64) -> (Value, StatusCode) {
let url = format!("/indexes/{}/tasks/{}", self.uid, update_id);
2021-02-18 22:10:50 +01:00
self.service.get(url).await
}
pub async fn list_tasks(&self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/tasks", self.uid);
2021-02-18 22:10:50 +01:00
self.service.get(url).await
}
2021-02-19 19:55:00 +01:00
2021-03-15 18:11:10 +01:00
pub async fn get_document(
&self,
id: u64,
_options: Option<GetDocumentOptions>,
) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/documents/{}", encode(self.uid.as_ref()), id);
2021-02-19 19:55:00 +01:00
self.service.get(url).await
}
pub async fn get_all_documents(&self, options: GetAllDocumentsOptions) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let mut url = format!("/indexes/{}/documents?", encode(self.uid.as_ref()));
if let Some(limit) = options.limit {
url.push_str(&format!("limit={}&", limit));
}
if let Some(offset) = options.offset {
url.push_str(&format!("offset={}&", offset));
}
if let Some(attributes_to_retrieve) = options.attributes_to_retrieve {
2021-03-15 18:11:10 +01:00
url.push_str(&format!(
"attributesToRetrieve={}&",
attributes_to_retrieve.join(",")
));
}
2021-02-19 19:55:00 +01:00
self.service.get(url).await
}
2021-02-22 16:03:17 +01:00
pub async fn delete_document(&self, id: u64) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/documents/{}", encode(self.uid.as_ref()), id);
2021-02-22 16:03:17 +01:00
self.service.delete(url).await
}
pub async fn clear_all_documents(&self) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/documents", encode(self.uid.as_ref()));
2021-02-22 16:03:17 +01:00
self.service.delete(url).await
}
2021-02-23 14:13:43 +01:00
pub async fn delete_batch(&self, ids: Vec<u64>) -> (Value, StatusCode) {
2021-11-03 14:25:49 +01:00
let url = format!(
"/indexes/{}/documents/delete-batch",
2022-01-12 15:57:31 +01:00
encode(self.uid.as_ref())
2021-11-03 14:25:49 +01:00
);
2021-03-15 18:11:10 +01:00
self.service
.post(url, serde_json::to_value(&ids).unwrap())
.await
2021-02-23 14:13:43 +01:00
}
2021-02-24 09:30:51 +01:00
pub async fn settings(&self) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/settings", encode(self.uid.as_ref()));
2021-02-24 09:30:51 +01:00
self.service.get(url).await
}
pub async fn update_settings(&self, settings: Value) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/settings", encode(self.uid.as_ref()));
2021-02-24 09:30:51 +01:00
self.service.post(url, settings).await
}
2021-02-24 10:14:36 +01:00
pub async fn delete_settings(&self) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/settings", encode(self.uid.as_ref()));
2021-02-24 10:14:36 +01:00
self.service.delete(url).await
}
2021-04-01 20:54:37 +02:00
pub async fn stats(&self) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/stats", encode(self.uid.as_ref()));
2021-04-01 20:54:37 +02:00
self.service.get(url).await
}
2021-04-20 12:07:22 +02:00
2021-07-06 11:54:37 +02:00
/// Performs both GET and POST search queries
pub async fn search(
&self,
query: Value,
test: impl Fn(Value, StatusCode) + UnwindSafe + Clone,
) {
2021-07-06 11:54:09 +02:00
let (response, code) = self.search_post(query.clone()).await;
let t = test.clone();
if let Err(e) = catch_unwind(move || t(response, code)) {
eprintln!("Error with post search");
resume_unwind(e);
}
let (response, code) = self.search_get(query).await;
if let Err(e) = catch_unwind(move || test(response, code)) {
eprintln!("Error with get search");
resume_unwind(e);
}
}
pub async fn search_post(&self, query: Value) -> (Value, StatusCode) {
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/search", encode(self.uid.as_ref()));
2021-07-06 11:54:09 +02:00
self.service.post(url, query).await
}
pub async fn search_get(&self, query: Value) -> (Value, StatusCode) {
let params = serde_url_params::to_string(&query).unwrap();
2022-01-12 15:57:31 +01:00
let url = format!("/indexes/{}/search?{}", encode(self.uid.as_ref()), params);
2021-07-06 11:54:09 +02:00
self.service.get(url).await
}
2021-04-22 10:14:29 +02:00
make_settings_test_routes!(distinct_attribute);
2021-02-18 19:50:52 +01:00
}
2021-02-19 19:55:00 +01:00
pub struct GetDocumentOptions;
#[derive(Debug, Default)]
pub struct GetAllDocumentsOptions {
pub limit: Option<usize>,
pub offset: Option<usize>,
pub attributes_to_retrieve: Option<Vec<&'static str>>,
}