Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add facetstats in the searchResults #443

Merged
merged 5 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ isahc-static-curl = ["isahc/static-curl"]
env_logger = "0.10"
futures-await-test = "0.3"
futures = "0.3"
mockito = "0.32.3"
mockito = "1.0.0"
meilisearch-test-macro = { path = "meilisearch-test-macro" }
tokio = { version = "1", features = ["rt", "macros"] }

Expand Down
4 changes: 1 addition & 3 deletions meilisearch-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ pub fn meilisearch_test(params: TokenStream, input: TokenStream) -> TokenStream

outer_fn.sig.inputs.clear();
outer_fn.sig.asyncness = inner_fn.sig.asyncness.clone();
outer_fn
.attrs
.push(parse_quote!(#[futures_await_test::async_test]));
outer_fn.attrs.push(parse_quote!(#[tokio::test]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, what is tokio test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the library used to handle async testing. We had to change it to be compatible with the new version of Mockito

outer_fn.block.stmts = outer_block;
} else {
panic!("#[meilisearch_test] can only be applied to async functions")
Expand Down
34 changes: 18 additions & 16 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,10 +1039,7 @@ pub struct Version {

#[cfg(test)]
mod tests {
use std::mem;

use big_s::S;
use mockito::mock;
use time::OffsetDateTime;

use meilisearch_test_macro::meilisearch_test;
Expand Down Expand Up @@ -1108,22 +1105,25 @@ mod tests {

#[meilisearch_test]
async fn test_methods_has_qualified_version_as_header() {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let path = "/hello";
let address = &format!("{}{}", mock_server_url, path);
let user_agent = &*qualified_version();

let assertions = vec![
(
mock("GET", path)
s.mock("GET", path)
.match_header("User-Agent", user_agent)
.create(),
.create_async()
.await,
request::<(), (), ()>(address, "", Method::Get { query: () }, 200),
),
(
mock("POST", path)
s.mock("POST", path)
.match_header("User-Agent", user_agent)
.create(),
.create_async()
.await,
request::<(), (), ()>(
address,
"",
Expand All @@ -1135,15 +1135,17 @@ mod tests {
),
),
(
mock("DELETE", path)
s.mock("DELETE", path)
.match_header("User-Agent", user_agent)
.create(),
.create_async()
.await,
request::<(), (), ()>(address, "", Method::Delete { query: () }, 200),
),
(
mock("PUT", path)
s.mock("PUT", path)
.match_header("User-Agent", user_agent)
.create(),
.create_async()
.await,
request::<(), (), ()>(
address,
"",
Expand All @@ -1155,9 +1157,10 @@ mod tests {
),
),
(
mock("PATCH", path)
s.mock("PATCH", path)
.match_header("User-Agent", user_agent)
.create(),
.create_async()
.await,
request::<(), (), ()>(
address,
"",
Expand All @@ -1173,8 +1176,7 @@ mod tests {
for (m, req) in assertions {
let _ = req.await;

m.assert();
mem::drop(m);
m.assert_async().await;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub struct SearchResult<T> {
pub matches_position: Option<HashMap<String, Vec<MatchRange>>>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct FacetStats {
pub min: u32,
pub max: u32,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
/// A struct containing search results and other information about the search.
Expand All @@ -68,6 +74,8 @@ pub struct SearchResults<T> {
pub total_pages: Option<usize>,
/// Distribution of the given facets
pub facet_distribution: Option<HashMap<String, HashMap<String, usize>>>,
/// facet stats of the numerical facets requested in the `facet` search parameter.
pub facet_stats: Option<HashMap<String, FacetStats>>,
/// Processing time of the query
pub processing_time_ms: usize,
/// Query originating the response
Expand Down
64 changes: 39 additions & 25 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ mod test {
};
use big_s::S;
use meilisearch_test_macro::meilisearch_test;
use mockito::mock;
use serde::{Deserialize, Serialize};
use std::time::Duration;

Expand Down Expand Up @@ -846,25 +845,27 @@ mod test {

#[meilisearch_test]
async fn test_get_tasks_no_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path = "/tasks";

let mock_res = mock("GET", path).with_status(200).create();
let mock_res = s.mock("GET", path).with_status(200).create_async().await;
let _ = client.get_tasks().await;
mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

#[meilisearch_test]
async fn test_get_tasks_with_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path =
"/tasks?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1&limit=0&from=1";

let mock_res = mock("GET", path).with_status(200).create();
let mock_res = s.mock("GET", path).with_status(200).create_async().await;

let mut query = TasksSearchQuery::new(&client);
query
Expand All @@ -877,13 +878,15 @@ mod test {

let _ = client.get_tasks_with(&query).await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

#[meilisearch_test]
async fn test_get_tasks_with_date_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path = "/tasks?\
beforeEnqueuedAt=2022-02-03T13%3A02%3A38.369634Z\
Expand All @@ -893,7 +896,7 @@ mod test {
&beforeFinishedAt=2026-02-03T13%3A02%3A38.369634Z\
&afterFinishedAt=2027-02-03T13%3A02%3A38.369634Z";

let mock_res = mock("GET", path).with_status(200).create();
let mock_res = s.mock("GET", path).with_status(200).create_async().await;

let before_enqueued_at = OffsetDateTime::parse(
"2022-02-03T13:02:38.369634Z",
Expand Down Expand Up @@ -940,18 +943,20 @@ mod test {

let _ = client.get_tasks_with(&query).await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

#[meilisearch_test]
async fn test_get_tasks_on_struct_with_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path =
"/tasks?indexUids=movies,test&statuses=equeued&types=documentDeletion&canceledBy=9";

let mock_res = mock("GET", path).with_status(200).create();
let mock_res = s.mock("GET", path).with_status(200).create_async().await;

let mut query = TasksSearchQuery::new(&client);
let _ = query
Expand All @@ -962,7 +967,8 @@ mod test {
.execute()
.await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

Expand Down Expand Up @@ -1005,12 +1011,13 @@ mod test {

#[meilisearch_test]
async fn test_cancel_tasks_with_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path =
"/tasks/cancel?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1";

let mock_res = mock("POST", path).with_status(200).create();
let mock_res = s.mock("POST", path).with_status(200).create_async().await;

let mut query = TasksCancelQuery::new(&client);
query
Expand All @@ -1021,18 +1028,20 @@ mod test {

let _ = client.cancel_tasks_with(&query).await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

#[meilisearch_test]
async fn test_cancel_tasks_with_params_execute() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path =
"/tasks/cancel?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1";

let mock_res = mock("POST", path).with_status(200).create();
let mock_res = s.mock("POST", path).with_status(200).create_async().await;

let mut query = TasksCancelQuery::new(&client);
let _ = query
Expand All @@ -1043,17 +1052,19 @@ mod test {
.execute()
.await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

#[meilisearch_test]
async fn test_delete_tasks_with_params() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path = "/tasks?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1";

let mock_res = mock("DELETE", path).with_status(200).create();
let mock_res = s.mock("DELETE", path).with_status(200).create_async().await;

let mut query = TasksDeleteQuery::new(&client);
query
Expand All @@ -1064,17 +1075,19 @@ mod test {

let _ = client.delete_tasks_with(&query).await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}

#[meilisearch_test]
async fn test_delete_tasks_with_params_execute() -> Result<(), Error> {
let mock_server_url = &mockito::server_url();
let mut s = mockito::Server::new_async().await;
let mock_server_url = s.url();
let client = Client::new(mock_server_url, "masterKey");
let path = "/tasks?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1";

let mock_res = mock("DELETE", path).with_status(200).create();
let mock_res = s.mock("DELETE", path).with_status(200).create_async().await;

let mut query = TasksDeleteQuery::new(&client);
let _ = query
Expand All @@ -1085,7 +1098,8 @@ mod test {
.execute()
.await;

mock_res.assert();
mock_res.assert_async().await;

Ok(())
}
}