From 097cae90a7455b486808cb808f0be5d0f0157949 Mon Sep 17 00:00:00 2001 From: mpostma Date: Mon, 22 Feb 2021 14:23:17 +0100 Subject: [PATCH] tests get documents limit, offset, attr to retrieve --- tests/common/index.rs | 22 +++++++++-- tests/common/mod.rs | 1 + tests/common/service.rs | 20 +++++++++- tests/documents/get_documents.rs | 66 ++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 5 deletions(-) diff --git a/tests/common/index.rs b/tests/common/index.rs index 6e3dc2603..ed5e533a5 100644 --- a/tests/common/index.rs +++ b/tests/common/index.rs @@ -100,8 +100,20 @@ impl Index<'_> { self.service.get(url).await } - pub async fn get_all_documents(&self, _options: GetAllDocumentsOptions) -> (Value, StatusCode) { - let url = format!("/indexes/{}/documents", self.uid); + pub async fn get_all_documents(&self, options: GetAllDocumentsOptions) -> (Value, StatusCode) { + let mut url = format!("/indexes/{}/documents?", self.uid); + 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 { + url.push_str(&format!("attributesToRetrieve={}&", attributes_to_retrieve.join(","))); + } + self.service.get(url).await } } @@ -109,4 +121,8 @@ impl Index<'_> { pub struct GetDocumentOptions; #[derive(Debug, Default)] -pub struct GetAllDocumentsOptions; +pub struct GetAllDocumentsOptions { + pub limit: Option, + pub offset: Option, + pub attributes_to_retrieve: Option>, +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 36c7e8190..fd461e61c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -3,6 +3,7 @@ mod server; mod service; pub use server::Server; +pub use index::{GetDocumentOptions, GetAllDocumentsOptions}; /// Performs a search test on both post and get routes #[macro_export] diff --git a/tests/common/service.rs b/tests/common/service.rs index 8d19c4a49..8e797c00d 100644 --- a/tests/common/service.rs +++ b/tests/common/service.rs @@ -23,6 +23,24 @@ impl Service { (response, status_code) } + /// Send a test post request from a text body, with a `content-type:application/json` header. + pub async fn post_str(&self, url: impl AsRef, body: impl AsRef) -> (Value, StatusCode) { + let mut app = + test::init_service(meilisearch_http::create_app(&self.0, true).wrap(NormalizePath)).await; + + let req = test::TestRequest::post() + .uri(url.as_ref()) + .set_payload(body.as_ref().to_string()) + .header("content-type", "application/json") + .to_request(); + let res = test::call_service(&mut app, req).await; + let status_code = res.status(); + + let body = test::read_body(res).await; + let response = serde_json::from_slice(&body).unwrap_or_default(); + (response, status_code) + } + pub async fn get(&self, url: impl AsRef) -> (Value, StatusCode) { let mut app = test::init_service(meilisearch_http::create_app(&self.0, true).wrap(NormalizePath)).await; @@ -64,6 +82,4 @@ impl Service { let response = serde_json::from_slice(&body).unwrap_or_default(); (response, status_code) } - } - diff --git a/tests/documents/get_documents.rs b/tests/documents/get_documents.rs index f5afc29e2..3b4581096 100644 --- a/tests/documents/get_documents.rs +++ b/tests/documents/get_documents.rs @@ -45,4 +45,70 @@ async fn get_all_documents_no_options() { assert_eq!(code, 200); let arr = response.as_array().unwrap(); assert_eq!(arr.len(), 20); + let first = serde_json::json!({ + "id":0, + "isActive":false, + "balance":"$2,668.55", + "picture":"http://placehold.it/32x32", + "age":36, + "color":"Green", + "name":"Lucas Hess", + "gender":"male", + "email":"lucashess@chorizon.com", + "phone":"+1 (998) 478-2597", + "address":"412 Losee Terrace, Blairstown, Georgia, 2825", + "about":"Mollit ad in exercitation quis. Anim est ut consequat fugiat duis magna aliquip velit nisi. Commodo eiusmod est consequat proident consectetur aliqua enim fugiat. Aliqua adipisicing laboris elit proident enim veniam laboris mollit. Incididunt fugiat minim ad nostrud deserunt tempor in. Id irure officia labore qui est labore nulla nisi. Magna sit quis tempor esse consectetur amet labore duis aliqua consequat.\r\n", + "registered":"2016-06-21T09:30:25 -02:00", + "latitude":-44.174957, + "longitude":-145.725388, + "tags":["bug" + ,"bug"]}); + assert_eq!(first, arr[0]); +} + +#[actix_rt::test] +async fn test_get_all_documents_limit() { + let server = Server::new().await; + let index = server.index("test"); + index.load_test_set().await; + + let (response, code) = index.get_all_documents(GetAllDocumentsOptions { limit: Some(5), ..Default::default() }).await; + assert_eq!(code, 200); + assert_eq!(response.as_array().unwrap().len(), 5); + assert_eq!(response.as_array().unwrap()[0]["id"], 0); +} + +#[actix_rt::test] +async fn test_get_all_documents_offset() { + let server = Server::new().await; + let index = server.index("test"); + index.load_test_set().await; + + let (response, code) = index.get_all_documents(GetAllDocumentsOptions { offset: Some(5), ..Default::default() }).await; + assert_eq!(code, 200); + assert_eq!(response.as_array().unwrap().len(), 20); + assert_eq!(response.as_array().unwrap()[0]["id"], 13); +} + +#[actix_rt::test] +async fn test_get_all_documents_attributes_to_retrieve() { + let server = Server::new().await; + let index = server.index("test"); + index.load_test_set().await; + + let (response, code) = index.get_all_documents(GetAllDocumentsOptions { attributes_to_retrieve: Some(vec!["name"]), ..Default::default() }).await; + assert_eq!(code, 200); + assert_eq!(response.as_array().unwrap().len(), 20); + assert_eq!(response.as_array().unwrap()[0].as_object().unwrap().keys().count(), 1); + assert!(response.as_array().unwrap()[0].as_object().unwrap().get("name").is_some()); + + let (response, code) = index.get_all_documents(GetAllDocumentsOptions { attributes_to_retrieve: Some(vec![]), ..Default::default() }).await; + assert_eq!(code, 200); + assert_eq!(response.as_array().unwrap().len(), 20); + assert_eq!(response.as_array().unwrap()[0].as_object().unwrap().keys().count(), 0); + + let (response, code) = index.get_all_documents(GetAllDocumentsOptions { attributes_to_retrieve: Some(vec!["name", "tags"]), ..Default::default() }).await; + assert_eq!(code, 200); + assert_eq!(response.as_array().unwrap().len(), 20); + assert_eq!(response.as_array().unwrap()[0].as_object().unwrap().keys().count(), 2); }