Fix some errors

This commit is contained in:
many
2021-10-26 19:36:48 +02:00
parent 61c15b69fb
commit 7464720426
27 changed files with 347 additions and 234 deletions

View File

@ -80,7 +80,7 @@ async fn error_add_documents_test_bad_content_types() {
assert_eq!(
response["message"],
json!(
r#"The Content-Type "text/plain" is invalid. Accepted values for the Content-Type header are: "application/json", "application/x-ndjson", "text/csv""#
r#"The Content-Type `text/plain` is invalid. Accepted values for the Content-Type header are: `application/json`, `application/x-ndjson`, `text/csv`"#
)
);
assert_eq!(response["code"], "invalid_content_type");
@ -104,7 +104,7 @@ async fn error_add_documents_test_bad_content_types() {
assert_eq!(
response["message"],
json!(
r#"The Content-Type "text/plain" is invalid. Accepted values for the Content-Type header are: "application/json", "application/x-ndjson", "text/csv""#
r#"The Content-Type `text/plain` is invalid. Accepted values for the Content-Type header are: `application/json`, `application/x-ndjson`, `text/csv`"#
)
);
assert_eq!(response["code"], "invalid_content_type");
@ -145,7 +145,7 @@ async fn error_add_documents_test_no_content_type() {
assert_eq!(
response["message"],
json!(
r#"A Content-Type header is missing. Accepted values for the Content-Type header are: "application/json", "application/x-ndjson", "text/csv""#
r#"A Content-Type header is missing. Accepted values for the Content-Type header are: `application/json`, `application/x-ndjson`, `text/csv`"#
)
);
assert_eq!(response["code"], "missing_content_type");
@ -168,7 +168,7 @@ async fn error_add_documents_test_no_content_type() {
assert_eq!(
response["message"],
json!(
r#"A Content-Type header is missing. Accepted values for the Content-Type header are: "application/json", "application/x-ndjson", "text/csv""#
r#"A Content-Type header is missing. Accepted values for the Content-Type header are: `application/json`, `application/x-ndjson`, `text/csv`"#
)
);
assert_eq!(response["code"], "missing_content_type");
@ -204,7 +204,7 @@ async fn error_add_malformed_csv_documents() {
assert_eq!(
response["message"],
json!(
r#"The csv payload provided is malformed. CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields."#
r#"The `csv` payload provided is malformed. `CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields`."#
)
);
assert_eq!(response["code"], json!("malformed_payload"));
@ -228,7 +228,7 @@ async fn error_add_malformed_csv_documents() {
assert_eq!(
response["message"],
json!(
r#"The csv payload provided is malformed. CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields."#
r#"The `csv` payload provided is malformed. `CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields`."#
)
);
assert_eq!(response["code"], json!("malformed_payload"));
@ -264,7 +264,7 @@ async fn error_add_malformed_json_documents() {
assert_eq!(
response["message"],
json!(
r#"The json payload provided is malformed. key must be a string at line 1 column 14."#
r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."#
)
);
assert_eq!(response["code"], json!("malformed_payload"));
@ -288,7 +288,7 @@ async fn error_add_malformed_json_documents() {
assert_eq!(
response["message"],
json!(
r#"The json payload provided is malformed. key must be a string at line 1 column 14."#
r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."#
)
);
assert_eq!(response["code"], json!("malformed_payload"));
@ -324,7 +324,7 @@ async fn error_add_malformed_ndjson_documents() {
assert_eq!(
response["message"],
json!(
r#"The ndjson payload provided is malformed. key must be a string at line 2 column 2."#
r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 2`."#
)
);
assert_eq!(response["code"], json!("malformed_payload"));
@ -348,7 +348,7 @@ async fn error_add_malformed_ndjson_documents() {
assert_eq!(
response["message"],
json!(
r#"The ndjson payload provided is malformed. key must be a string at line 2 column 2."#
r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 2`."#
)
);
assert_eq!(response["code"], json!("malformed_payload"));
@ -359,6 +359,162 @@ async fn error_add_malformed_ndjson_documents() {
);
}
#[actix_rt::test]
async fn error_add_missing_payload_csv_documents() {
let document = "";
let server = Server::new().await;
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
))
.await;
// post
let req = test::TestRequest::post()
.uri("/indexes/dog/documents")
.set_payload(document.to_string())
.insert_header(("content-type", "text/csv"))
.to_request();
let res = test::call_service(&app, req).await;
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 400);
assert_eq!(response["message"], json!(r#"A csv payload is missing."#));
assert_eq!(response["code"], json!("missing_payload"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
json!("https://docs.meilisearch.com/errors#missing_payload")
);
// put
let req = test::TestRequest::put()
.uri("/indexes/dog/documents")
.set_payload(document.to_string())
.insert_header(("content-type", "text/csv"))
.to_request();
let res = test::call_service(&app, req).await;
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 400);
assert_eq!(response["message"], json!(r#"A csv payload is missing."#));
assert_eq!(response["code"], json!("missing_payload"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
json!("https://docs.meilisearch.com/errors#missing_payload")
);
}
#[actix_rt::test]
async fn error_add_missing_payload_json_documents() {
let document = "";
let server = Server::new().await;
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
))
.await;
// post
let req = test::TestRequest::post()
.uri("/indexes/dog/documents")
.set_payload(document.to_string())
.insert_header(("content-type", "application/json"))
.to_request();
let res = test::call_service(&app, req).await;
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 400);
assert_eq!(response["message"], json!(r#"A json payload is missing."#));
assert_eq!(response["code"], json!("missing_payload"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
json!("https://docs.meilisearch.com/errors#missing_payload")
);
// put
let req = test::TestRequest::put()
.uri("/indexes/dog/documents")
.set_payload(document.to_string())
.insert_header(("content-type", "application/json"))
.to_request();
let res = test::call_service(&app, req).await;
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 400);
assert_eq!(response["message"], json!(r#"A json payload is missing."#));
assert_eq!(response["code"], json!("missing_payload"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
json!("https://docs.meilisearch.com/errors#missing_payload")
);
}
#[actix_rt::test]
async fn error_add_missing_payload_ndjson_documents() {
let document = "";
let server = Server::new().await;
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
))
.await;
// post
let req = test::TestRequest::post()
.uri("/indexes/dog/documents")
.set_payload(document.to_string())
.insert_header(("content-type", "application/x-ndjson"))
.to_request();
let res = test::call_service(&app, req).await;
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 400);
assert_eq!(
response["message"],
json!(r#"A ndjson payload is missing."#)
);
assert_eq!(response["code"], json!("missing_payload"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
json!("https://docs.meilisearch.com/errors#missing_payload")
);
// put
let req = test::TestRequest::put()
.uri("/indexes/dog/documents")
.set_payload(document.to_string())
.insert_header(("content-type", "application/x-ndjson"))
.to_request();
let res = test::call_service(&app, req).await;
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 400);
assert_eq!(
response["message"],
json!(r#"A ndjson payload is missing."#)
);
assert_eq!(response["code"], json!("missing_payload"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
json!("https://docs.meilisearch.com/errors#missing_payload")
);
}
#[actix_rt::test]
async fn add_documents_no_index_creation() {
let server = Server::new().await;
@ -411,7 +567,7 @@ async fn error_document_add_create_index_bad_uid() {
let (response, code) = index.add_documents(json!([]), None).await;
let expected_response = json!({
"message": "883 fj! is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"message": "`883 fj!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"code": "invalid_index_uid",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_index_uid"
@ -428,7 +584,7 @@ async fn error_document_update_create_index_bad_uid() {
let (response, code) = index.update_documents(json!([]), None).await;
let expected_response = json!({
"message": "883 fj! is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"message": "`883 fj!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"code": "invalid_index_uid",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_index_uid"
@ -646,13 +802,13 @@ async fn error_add_documents_bad_document_id() {
index.wait_update_id(0).await;
let (response, code) = index.get_update(0).await;
assert_eq!(code, 200);
assert_eq!(response["status"], "failed");
assert_eq!(response["message"], "Document identifier foo & bar is invalid. A document identifier can be of type integer or string, only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and underscores (_).");
assert_eq!(response["code"], "invalid_document_id");
assert_eq!(response["type"], "invalid_request");
assert_eq!(response["status"], json!("failed"));
assert_eq!(response["message"], json!("Document identifier `foo & bar` is invalid. A document identifier can be of type integer or string, only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and underscores (_)."));
assert_eq!(response["code"], json!("invalid_document_id"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
"https://docs.meilisearch.com/errors#invalid_document_id"
json!("https://docs.meilisearch.com/errors#invalid_document_id")
);
}
@ -671,13 +827,13 @@ async fn error_update_documents_bad_document_id() {
index.wait_update_id(0).await;
let (response, code) = index.get_update(0).await;
assert_eq!(code, 200);
assert_eq!(response["status"], "failed");
assert_eq!(response["message"], "Document identifier foo & bar is invalid. A document identifier can be of type integer or string, only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and underscores (_).");
assert_eq!(response["code"], "invalid_document_id");
assert_eq!(response["type"], "invalid_request");
assert_eq!(response["status"], json!("failed"));
assert_eq!(response["message"], json!("Document identifier `foo & bar` is invalid. A document identifier can be of type integer or string, only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and underscores (_)."));
assert_eq!(response["code"], json!("invalid_document_id"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
"https://docs.meilisearch.com/errors#invalid_document_id"
json!("https://docs.meilisearch.com/errors#invalid_document_id")
);
}
@ -699,13 +855,13 @@ async fn error_add_documents_missing_document_id() {
assert_eq!(response["status"], "failed");
assert_eq!(
response["message"],
r#"Document doesn't have a docid attribute: {"id":"11","content":"foobar"}."#
json!(r#"Document doesn't have a `docid` attribute: `{"id":"11","content":"foobar"}`."#)
);
assert_eq!(response["code"], "missing_document_id");
assert_eq!(response["type"], "invalid_request");
assert_eq!(response["code"], json!("missing_document_id"));
assert_eq!(response["type"], json!("invalid_request"));
assert_eq!(
response["link"],
"https://docs.meilisearch.com/errors#missing_document_id"
json!("https://docs.meilisearch.com/errors#missing_document_id")
);
}
@ -727,7 +883,7 @@ async fn error_update_documents_missing_document_id() {
assert_eq!(response["status"], "failed");
assert_eq!(
response["message"],
r#"Document doesn't have a docid attribute: {"id":"11","content":"foobar"}."#
r#"Document doesn't have a `docid` attribute: `{"id":"11","content":"foobar"}`."#
);
assert_eq!(response["code"], "missing_document_id");
assert_eq!(response["type"], "invalid_request");
@ -737,109 +893,41 @@ async fn error_update_documents_missing_document_id() {
);
}
#[actix_rt::test]
async fn error_add_documents_with_primary_key_and_primary_key_already_exists() {
let server = Server::new().await;
let index = server.index("test");
// #[actix_rt::test]
// async fn error_document_field_limit_reached() {
// let server = Server::new().await;
// let index = server.index("test");
index.create(Some("primary")).await;
let documents = json!([
{
"id": 1,
"content": "foo",
}
]);
// index.create(Some("id")).await;
let (_response, code) = index.add_documents(documents, Some("id")).await;
assert_eq!(code, 202);
// let mut big_object = std::collections::HashMap::new();
// big_object.insert("id".to_owned(), "wow");
// for i in 0..65535 {
// let key = i.to_string();
// big_object.insert(key, "I am a text!");
// }
index.wait_update_id(0).await;
// let documents = json!([big_object]);
let (response, code) = index.get_update(0).await;
assert_eq!(code, 200);
assert_eq!(response["status"], "failed");
assert_eq!(response["message"], "Index test already has a primary key.");
assert_eq!(response["code"], "index_primary_key_already_exists");
assert_eq!(response["type"], "invalid_request");
assert_eq!(
response["link"],
"https://docs.meilisearch.com/errors#index_primary_key_already_exists"
);
// let (_response, code) = index.update_documents(documents, Some("id")).await;
// assert_eq!(code, 202);
let (response, code) = index.get().await;
assert_eq!(code, 200);
assert_eq!(response["primaryKey"], "primary");
}
#[actix_rt::test]
async fn error_update_documents_with_primary_key_and_primary_key_already_exists() {
let server = Server::new().await;
let index = server.index("test");
index.create(Some("primary")).await;
let documents = json!([
{
"id": 1,
"content": "foo",
}
]);
let (_response, code) = index.update_documents(documents, Some("id")).await;
assert_eq!(code, 202);
index.wait_update_id(0).await;
let (response, code) = index.get_update(0).await;
assert_eq!(code, 200);
// Documents without a primary key are not accepted.
assert_eq!(response["status"], "failed");
assert_eq!(response["message"], "Index test already has a primary key.");
assert_eq!(response["code"], "index_primary_key_already_exists");
assert_eq!(response["type"], "invalid_request");
assert_eq!(
response["link"],
"https://docs.meilisearch.com/errors#index_primary_key_already_exists"
);
let (response, code) = index.get().await;
assert_eq!(code, 200);
assert_eq!(response["primaryKey"], "primary");
}
#[actix_rt::test]
async fn error_document_field_limit_reached() {
let server = Server::new().await;
let index = server.index("test");
index.create(Some("id")).await;
let mut big_object = std::collections::HashMap::new();
big_object.insert("id".to_owned(), "wow");
for i in 0..65535 {
let key = i.to_string();
big_object.insert(key, "I am a text!");
}
let documents = json!([big_object]);
let (_response, code) = index.update_documents(documents, Some("id")).await;
assert_eq!(code, 202);
index.wait_update_id(0).await;
let (response, code) = index.get_update(0).await;
assert_eq!(code, 200);
// Documents without a primary key are not accepted.
assert_eq!(response["status"], "failed");
assert_eq!(
response["message"],
"A document cannot contain more than 65,535 fields."
);
assert_eq!(response["code"], "document_fields_limit_reached");
assert_eq!(response["type"], "invalid_request");
assert_eq!(
response["link"],
"https://docs.meilisearch.com/errors#document_fields_limit_reached"
);
}
// index.wait_update_id(0).await;
// let (response, code) = index.get_update(0).await;
// assert_eq!(code, 200);
// // Documents without a primary key are not accepted.
// assert_eq!(response["status"], "failed");
// assert_eq!(
// response["message"],
// "A document cannot contain more than 65,535 fields."
// );
// assert_eq!(response["code"], "document_fields_limit_reached");
// assert_eq!(response["type"], "invalid_request");
// assert_eq!(
// response["link"],
// "https://docs.meilisearch.com/errors#document_fields_limit_reached"
// );
// }
#[actix_rt::test]
async fn error_add_documents_invalid_geo_field() {
@ -885,7 +973,7 @@ async fn error_add_documents_payload_size() {
let (response, code) = index.add_documents(documents, None).await;
let expected_response = json!({
"message": "The payload cannot exceed 10MB.",
"message": "The provided payload reached the size limit.",
"code": "payload_too_large",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#payload_too_large"