adding primary-key when adding documents does not work; fix #519

This commit is contained in:
qdequele 2020-03-11 14:12:38 +01:00
parent 4ccf1d10bd
commit ce0e8415d5
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
4 changed files with 54 additions and 24 deletions

View File

@ -118,9 +118,9 @@ fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
} }
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateDocumentsQuery { struct UpdateDocumentsQuery {
document_id: Option<String>, primary_key: Option<String>,
} }
async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) -> SResult<Response> { async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) -> SResult<Response> {
@ -141,7 +141,7 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
.ok_or(ResponseError::internal("schema not found"))?; .ok_or(ResponseError::internal("schema not found"))?;
if schema.primary_key().is_none() { if schema.primary_key().is_none() {
let id = match query.document_id { let id = match query.primary_key {
Some(id) => id, Some(id) => id,
None => match data.first().and_then(|docs| find_primary_key(docs)) { None => match data.first().and_then(|docs| find_primary_key(docs)) {
Some(id) => id, Some(id) => id,

View File

@ -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 { loop {
let req = http::Request::get(format!("/indexes/{}/updates/{}", self.uid, update_id)) let (response, status_code) = self.get_update_status(update_id);
.body(Body::empty()) assert_eq!(status_code, 200);
.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();
if response["status"] == "processed" || response["status"] == "error" { if response["status"] == "processed" || response["status"] == "error" {
eprintln!("{:#?}", response); eprintln!("{:#?}", response);
return; return;
} }
block_on(sleep(Duration::from_secs(1))); 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); eprintln!("get_request: {}", url);
let req = http::Request::get(url).body(Body::empty()).unwrap(); let req = http::Request::get(url).body(Body::empty()).unwrap();
let res = self.mock.simulate(req).unwrap(); let res = self.mock.simulate(req).unwrap();
@ -79,7 +72,7 @@ impl Server {
(response, status_code) (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); eprintln!("post_request: {}", url);
let body_bytes = body.to_string().into_bytes(); let body_bytes = body.to_string().into_bytes();
@ -95,7 +88,7 @@ impl Server {
(response, status_code) (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); eprintln!("post_request_async: {}", url);
let (response, status_code) = self.post_request(url, body); let (response, status_code) = self.post_request(url, body);
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
@ -104,7 +97,7 @@ impl Server {
(response, status_code) (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); eprintln!("put_request: {}", url);
let body_bytes = body.to_string().into_bytes(); let body_bytes = body.to_string().into_bytes();
@ -120,7 +113,7 @@ impl Server {
(response, status_code) (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); eprintln!("put_request_async: {}", url);
let (response, status_code) = self.put_request(url, body); let (response, status_code) = self.put_request(url, body);
assert!(response["updateId"].as_u64().is_some()); assert!(response["updateId"].as_u64().is_some());
@ -129,7 +122,7 @@ impl Server {
(response, status_code) (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); eprintln!("delete_request: {}", url);
let req = http::Request::delete(url).body(Body::empty()).unwrap(); let req = http::Request::delete(url).body(Body::empty()).unwrap();
let res = self.mock.simulate(req).unwrap(); let res = self.mock.simulate(req).unwrap();
@ -141,7 +134,7 @@ impl Server {
(response, status_code) (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); eprintln!("delete_request_async: {}", url);
let (response, status_code) = self.delete_request(url); let (response, status_code) = self.delete_request(url);
assert!(response["updateId"].as_u64().is_some()); assert!(response["updateId"].as_u64().is_some());
@ -150,7 +143,7 @@ impl Server {
(response, status_code) (response, status_code)
} }
// // All Routes // All Routes
pub fn list_indexes(&mut self) -> (Value, StatusCode) { pub fn list_indexes(&mut self) -> (Value, StatusCode) {
self.get_request("/indexes") self.get_request("/indexes")

View File

@ -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");
}

View File

@ -657,4 +657,3 @@ fn check_add_documents_without_primary_key() {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_json_eq!(response, expected, ordered: false); assert_json_eq!(response, expected, ordered: false);
} }