From ce0e8415d5c68fef1026b12d06d984c922e9bfc3 Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 11 Mar 2020 14:12:38 +0100 Subject: [PATCH] adding primary-key when adding documents does not work; fix #519 --- meilisearch-http/src/routes/document.rs | 6 ++-- meilisearch-http/tests/common.rs | 33 +++++++++------------ meilisearch-http/tests/documents_add.rs | 38 +++++++++++++++++++++++++ meilisearch-http/tests/index.rs | 1 - 4 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 meilisearch-http/tests/documents_add.rs diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 3669e3899..4babe530f 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -118,9 +118,9 @@ fn find_primary_key(document: &IndexMap) -> Option { } #[derive(Default, Deserialize)] -#[serde(deny_unknown_fields)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] struct UpdateDocumentsQuery { - document_id: Option, + primary_key: Option, } async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> SResult { @@ -141,7 +141,7 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> .ok_or(ResponseError::internal("schema not found"))?; if schema.primary_key().is_none() { - let id = match query.document_id { + let id = match query.primary_key { Some(id) => id, None => match data.first().and_then(|docs| find_primary_key(docs)) { Some(id) => id, diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index 3814db6de..42a2ce556 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -44,30 +44,23 @@ impl Server { } } - fn wait_update_id(&mut self, update_id: u64) { + pub fn wait_update_id(&mut self, update_id: u64) { loop { - let req = http::Request::get(format!("/indexes/{}/updates/{}", self.uid, update_id)) - .body(Body::empty()) - .unwrap(); - - let res = self.mock.simulate(req).unwrap(); - assert_eq!(res.status(), 200); - - let mut buf = Vec::new(); - block_on(res.into_body().read_to_end(&mut buf)).unwrap(); - let response: Value = serde_json::from_slice(&buf).unwrap(); + let (response, status_code) = self.get_update_status(update_id); + assert_eq!(status_code, 200); if response["status"] == "processed" || response["status"] == "error" { eprintln!("{:#?}", response); return; } + block_on(sleep(Duration::from_secs(1))); } } - // // Global Http request GET/POST/DELETE async or sync + // Global Http request GET/POST/DELETE async or sync - fn get_request(&mut self, url: &str) -> (Value, StatusCode) { + pub fn get_request(&mut self, url: &str) -> (Value, StatusCode) { eprintln!("get_request: {}", url); let req = http::Request::get(url).body(Body::empty()).unwrap(); let res = self.mock.simulate(req).unwrap(); @@ -79,7 +72,7 @@ impl Server { (response, status_code) } - fn post_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) { + pub fn post_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) { eprintln!("post_request: {}", url); let body_bytes = body.to_string().into_bytes(); @@ -95,7 +88,7 @@ impl Server { (response, status_code) } - fn post_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) { + pub fn post_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) { eprintln!("post_request_async: {}", url); let (response, status_code) = self.post_request(url, body); assert_eq!(status_code, 202); @@ -104,7 +97,7 @@ impl Server { (response, status_code) } - fn put_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) { + pub fn put_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) { eprintln!("put_request: {}", url); let body_bytes = body.to_string().into_bytes(); @@ -120,7 +113,7 @@ impl Server { (response, status_code) } - fn put_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) { + pub fn put_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) { eprintln!("put_request_async: {}", url); let (response, status_code) = self.put_request(url, body); assert!(response["updateId"].as_u64().is_some()); @@ -129,7 +122,7 @@ impl Server { (response, status_code) } - fn delete_request(&mut self, url: &str) -> (Value, StatusCode) { + pub fn delete_request(&mut self, url: &str) -> (Value, StatusCode) { eprintln!("delete_request: {}", url); let req = http::Request::delete(url).body(Body::empty()).unwrap(); let res = self.mock.simulate(req).unwrap(); @@ -141,7 +134,7 @@ impl Server { (response, status_code) } - fn delete_request_async(&mut self, url: &str) -> (Value, StatusCode) { + pub fn delete_request_async(&mut self, url: &str) -> (Value, StatusCode) { eprintln!("delete_request_async: {}", url); let (response, status_code) = self.delete_request(url); assert!(response["updateId"].as_u64().is_some()); @@ -150,7 +143,7 @@ impl Server { (response, status_code) } - // // All Routes + // All Routes pub fn list_indexes(&mut self) -> (Value, StatusCode) { self.get_request("/indexes") diff --git a/meilisearch-http/tests/documents_add.rs b/meilisearch-http/tests/documents_add.rs new file mode 100644 index 000000000..7ba726810 --- /dev/null +++ b/meilisearch-http/tests/documents_add.rs @@ -0,0 +1,38 @@ +use serde_json::json; + +mod common; + +// Test issue https://github.com/meilisearch/MeiliSearch/issues/519 +#[test] +fn check_add_documents_with_primary_key_param() { + let mut server = common::Server::with_uid("movies"); + + // 1 - Create the index with no primary_key + + let body = json!({ + "uid": "movies", + }); + let (response, status_code) = server.create_index(body); + assert_eq!(status_code, 201); + assert_eq!(response["primaryKey"], json!(null)); + + // 2 - Add documents + + let body = json!([{ + "title": "Test", + "comment": "comment test" + }]); + + let url = "/indexes/movies/documents?primaryKey=title"; + let (response, status_code) = server.post_request(&url, body); + eprintln!("{:#?}", response); + assert_eq!(status_code, 202); + let update_id = response["updateId"].as_u64().unwrap(); + server.wait_update_id(update_id); + + // 3 - Check update sucess + + let (response, status_code) = server.get_update_status(update_id); + assert_eq!(status_code, 200); + assert_eq!(response["status"], "processed"); +} diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index a16c10575..ab10cf35b 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -657,4 +657,3 @@ fn check_add_documents_without_primary_key() { assert_eq!(status_code, 400); assert_json_eq!(response, expected, ordered: false); } -