implement index search methods

This commit is contained in:
mpostma 2021-07-06 11:54:09 +02:00
parent 487d82773a
commit 520d37983c
2 changed files with 29 additions and 2 deletions

View File

@ -97,7 +97,7 @@ actix-rt = "2.1.0"
assert-json-diff = { branch = "master", git = "https://github.com/qdequele/assert-json-diff" } assert-json-diff = { branch = "master", git = "https://github.com/qdequele/assert-json-diff" }
mockall = "0.9.1" mockall = "0.9.1"
paste = "1.0.5" paste = "1.0.5"
serde_url_params = "0.2.0" serde_url_params = "0.2.1"
tempdir = "0.3.7" tempdir = "0.3.7"
urlencoding = "1.1.1" urlencoding = "1.1.1"

View File

@ -1,4 +1,4 @@
use std::time::Duration; use std::{panic::{UnwindSafe, catch_unwind, resume_unwind}, time::Duration};
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use paste::paste; use paste::paste;
@ -185,6 +185,33 @@ impl Index<'_> {
self.service.get(url).await self.service.get(url).await
} }
pub async fn search(&self, query: Value, test: impl Fn(Value, StatusCode) + UnwindSafe + Clone) {
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) {
let url = format!("/indexes/{}/search", self.uid);
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();
let url = format!("/indexes/{}/search?{}", self.uid, params);
self.service.get(url).await
}
make_settings_test_routes!(distinct_attribute); make_settings_test_routes!(distinct_attribute);
} }