From a4476c20f88bbba66eaa8ed782e80b829a99055b Mon Sep 17 00:00:00 2001 From: Tamo Date: Wed, 18 Jan 2023 15:28:02 +0100 Subject: [PATCH] fix a wrong error code and add tests on the document resource --- meilisearch/src/routes/indexes/documents.rs | 4 +- meilisearch/tests/common/index.rs | 10 +++ meilisearch/tests/documents/errors.rs | 99 +++++++++++++++++++++ meilisearch/tests/documents/mod.rs | 1 + 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 meilisearch/tests/documents/errors.rs diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index 2b36ba834..0ec1057ae 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -128,11 +128,11 @@ pub async fn delete_document( #[derive(Debug, DeserializeFromValue)] #[deserr(error = DeserrQueryParamError, rename_all = camelCase, deny_unknown_fields)] pub struct BrowseQuery { - #[deserr(default, error = DeserrQueryParamError)] + #[deserr(default, error = DeserrQueryParamError)] offset: Param, #[deserr(default = Param(PAGINATION_DEFAULT_LIMIT), error = DeserrQueryParamError)] limit: Param, - #[deserr(default, error = DeserrQueryParamError)] + #[deserr(default, error = DeserrQueryParamError)] fields: OptionStarOrList, } diff --git a/meilisearch/tests/common/index.rs b/meilisearch/tests/common/index.rs index b98ed9827..454c84565 100644 --- a/meilisearch/tests/common/index.rs +++ b/meilisearch/tests/common/index.rs @@ -155,6 +155,11 @@ impl Index<'_> { self.service.get(url).await } + pub async fn get_all_documents_raw(&self, options: &str) -> (Value, StatusCode) { + let url = format!("/indexes/{}/documents{}", urlencode(self.uid.as_ref()), options); + self.service.get(url).await + } + pub async fn get_all_documents(&self, options: GetAllDocumentsOptions) -> (Value, StatusCode) { let mut url = format!("/indexes/{}/documents?", urlencode(self.uid.as_ref())); if let Some(limit) = options.limit { @@ -187,6 +192,11 @@ impl Index<'_> { self.service.post_encoded(url, serde_json::to_value(&ids).unwrap(), self.encoder).await } + pub async fn delete_batch_raw(&self, body: Value) -> (Value, StatusCode) { + let url = format!("/indexes/{}/documents/delete-batch", urlencode(self.uid.as_ref())); + self.service.post_encoded(url, body, self.encoder).await + } + pub async fn settings(&self) -> (Value, StatusCode) { let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref())); self.service.get(url).await diff --git a/meilisearch/tests/documents/errors.rs b/meilisearch/tests/documents/errors.rs new file mode 100644 index 000000000..4c50a8e02 --- /dev/null +++ b/meilisearch/tests/documents/errors.rs @@ -0,0 +1,99 @@ +use meili_snap::*; +use serde_json::json; + +use crate::common::Server; + +#[actix_rt::test] +async fn get_all_documents_bad_offset() { + let server = Server::new().await; + let index = server.index("test"); + + let (response, code) = index.get_all_documents_raw("?offset").await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Invalid value in parameter `offset`: could not parse `` as a positive integer", + "code": "invalid_document_offset", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid-document-offset" + } + "###); + + let (response, code) = index.get_all_documents_raw("?offset=doggo").await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Invalid value in parameter `offset`: could not parse `doggo` as a positive integer", + "code": "invalid_document_offset", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid-document-offset" + } + "###); + + let (response, code) = index.get_all_documents_raw("?offset=-1").await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Invalid value in parameter `offset`: could not parse `-1` as a positive integer", + "code": "invalid_document_offset", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid-document-offset" + } + "###); +} + +#[actix_rt::test] +async fn get_all_documents_bad_limit() { + let server = Server::new().await; + let index = server.index("test"); + + let (response, code) = index.get_all_documents_raw("?limit").await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Invalid value in parameter `limit`: could not parse `` as a positive integer", + "code": "invalid_document_limit", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid-document-limit" + } + "###); + + let (response, code) = index.get_all_documents_raw("?limit=doggo").await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Invalid value in parameter `limit`: could not parse `doggo` as a positive integer", + "code": "invalid_document_limit", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid-document-limit" + } + "###); + + let (response, code) = index.get_all_documents_raw("?limit=-1").await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Invalid value in parameter `limit`: could not parse `-1` as a positive integer", + "code": "invalid_document_limit", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid-document-limit" + } + "###); +} + +#[actix_rt::test] +async fn delete_documents_batch() { + let server = Server::new().await; + let index = server.index("test"); + + let (response, code) = index.delete_batch_raw(json!("doggo")).await; + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Json deserialize error: invalid type: string \"doggo\", expected a sequence at line 1 column 7", + "code": "bad_request", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#bad-request" + } + "###); +} diff --git a/meilisearch/tests/documents/mod.rs b/meilisearch/tests/documents/mod.rs index 794b57c3a..f6430b108 100644 --- a/meilisearch/tests/documents/mod.rs +++ b/meilisearch/tests/documents/mod.rs @@ -1,4 +1,5 @@ mod add_documents; mod delete_documents; +mod errors; mod get_documents; mod update_documents;