292 lines
8.9 KiB
Rust
Raw Normal View History

use actix_web::http::header::{ContentType, ACCEPT_ENCODING};
use actix_web::test;
use meili_snap::{json_string, snapshot};
use meilisearch::Opt;
2021-02-18 19:50:52 +01:00
2022-10-20 18:00:07 +02:00
use crate::common::encoder::Encoder;
use crate::common::{default_settings, Server, Value};
2023-09-11 16:50:53 +02:00
use crate::json;
2022-10-20 18:00:07 +02:00
2021-02-18 19:50:52 +01:00
#[actix_rt::test]
async fn create_index_no_primary_key() {
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
let index = server.unique_index();
2021-02-18 19:50:52 +01:00
let (response, code) = index.create(None).await;
assert_eq!(code, 202);
assert_eq!(response["status"], "enqueued");
let response = index.wait_task(response.uid()).await;
assert_eq!(response["status"], "succeeded");
assert_eq!(response["type"], "indexCreation");
2023-09-11 16:50:53 +02:00
assert_eq!(response["details"]["primaryKey"], json!(null));
}
#[actix_rt::test]
async fn create_index_with_gzip_encoded_request() {
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
let index = server.unique_index_with_encoder(Encoder::Gzip);
let (response, code) = index.create(None).await;
assert_eq!(code, 202);
assert_eq!(response["status"], "enqueued");
let response = index.wait_task(response.uid()).await;
assert_eq!(response["status"], "succeeded");
assert_eq!(response["type"], "indexCreation");
2023-09-11 16:50:53 +02:00
assert_eq!(response["details"]["primaryKey"], json!(null));
}
#[actix_rt::test]
async fn create_index_with_gzip_encoded_request_and_receiving_brotli_encoded_response() {
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
2022-10-20 17:08:52 +02:00
let app = server.init_web_app().await;
let body = serde_json::to_string(&json!({
"uid": "test",
"primaryKey": None::<&str>,
}))
.unwrap();
let req = test::TestRequest::post()
.uri("/indexes")
.insert_header(Encoder::Gzip.header().unwrap())
.insert_header((ACCEPT_ENCODING, "br"))
.insert_header(ContentType::json())
.set_payload(Encoder::Gzip.encode(body))
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), 202);
let bytes = test::read_body(res).await;
let decoded = Encoder::Brotli.decode(bytes);
let parsed_response =
2022-10-13 15:35:31 +02:00
serde_json::from_slice::<Value>(decoded.into().as_ref()).expect("Expecting valid json");
assert_eq!(parsed_response["indexUid"], "test");
}
#[actix_rt::test]
async fn create_index_with_zlib_encoded_request() {
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
let index = server.unique_index_with_encoder(Encoder::Deflate);
let (response, code) = index.create(None).await;
assert_eq!(code, 202);
assert_eq!(response["status"], "enqueued");
let response = index.wait_task(response.uid()).await;
assert_eq!(response["status"], "succeeded");
assert_eq!(response["type"], "indexCreation");
2023-09-11 16:50:53 +02:00
assert_eq!(response["details"]["primaryKey"], json!(null));
}
#[actix_rt::test]
async fn create_index_with_brotli_encoded_request() {
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
let index = server.unique_index_with_encoder(Encoder::Brotli);
let (response, code) = index.create(None).await;
assert_eq!(code, 202);
assert_eq!(response["status"], "enqueued");
let response = index.wait_task(response.uid()).await;
assert_eq!(response["status"], "succeeded");
assert_eq!(response["type"], "indexCreation");
2023-09-11 16:50:53 +02:00
assert_eq!(response["details"]["primaryKey"], json!(null));
2021-02-18 19:50:52 +01:00
}
#[actix_rt::test]
async fn create_index_with_primary_key() {
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
let index = server.unique_index();
2021-02-18 19:50:52 +01:00
let (response, code) = index.create(Some("primary")).await;
assert_eq!(code, 202);
assert_eq!(response["status"], "enqueued");
let response = index.wait_task(response.uid()).await;
assert_eq!(response["status"], "succeeded");
assert_eq!(response["type"], "indexCreation");
assert_eq!(response["details"]["primaryKey"], "primary");
2021-02-18 19:50:52 +01:00
}
#[actix_rt::test]
async fn create_index_with_invalid_primary_key() {
2024-09-16 23:53:34 +03:00
let documents = json!([ { "id": 2, "title": "Pride and Prejudice" } ]);
2024-10-01 17:27:27 +02:00
let server = Server::new_shared();
let index = server.unique_index();
let (response, code) = index.add_documents(documents, Some("title")).await;
assert_eq!(code, 202);
index.wait_task(response.uid()).await;
let (response, code) = index.get().await;
assert_eq!(code, 200);
2023-09-11 16:50:53 +02:00
assert_eq!(response["primaryKey"], json!(null));
2024-09-16 23:53:34 +03:00
let documents = json!([ { "id": "e".repeat(513) } ]);
let (response, code) = index.add_documents(documents, Some("id")).await;
assert_eq!(code, 202);
index.wait_task(response.uid()).await;
let (response, code) = index.get().await;
assert_eq!(code, 200);
2023-09-11 16:50:53 +02:00
assert_eq!(response["primaryKey"], json!(null));
}
2021-02-18 20:32:34 +01:00
#[actix_rt::test]
async fn test_create_multiple_indexes() {
2024-10-07 16:29:47 +02:00
let server = Server::new_shared();
2024-10-01 17:27:27 +02:00
let index1 = server.unique_index();
let index2 = server.unique_index();
let index3 = server.unique_index();
let index4 = server.unique_index();
2021-02-18 20:32:34 +01:00
let (task1, _) = index1.create(None).await;
let (task2, _) = index2.create(None).await;
let (task3, _) = index3.create(None).await;
2021-02-18 20:32:34 +01:00
index1.wait_task(task1.uid()).await.succeeded();
index2.wait_task(task2.uid()).await.succeeded();
index3.wait_task(task3.uid()).await.succeeded();
2021-02-18 20:32:34 +01:00
assert_eq!(index1.get().await.1, 200);
assert_eq!(index2.get().await.1, 200);
assert_eq!(index3.get().await.1, 200);
2021-06-15 22:21:52 +02:00
assert_eq!(index4.get().await.1, 404);
2021-02-18 20:32:34 +01:00
}
2021-10-21 14:42:01 +02:00
#[actix_rt::test]
async fn error_create_existing_index() {
2024-10-07 16:29:47 +02:00
let server = Server::new_shared();
2024-10-01 17:27:27 +02:00
let index = server.unique_index();
2021-10-21 14:42:01 +02:00
let (_, code) = index.create(Some("primary")).await;
assert_eq!(code, 202);
2021-10-21 14:42:01 +02:00
let (task, _) = index.create(Some("primary")).await;
let response = index.wait_task(task.uid()).await;
2024-10-01 17:27:27 +02:00
let msg = format!(
"Index `{}` already exists.",
task["indexUid"].as_str().expect("indexUid should exist").trim_matches('"')
);
2021-10-21 14:42:01 +02:00
let expected_response = json!({
2024-10-01 17:27:27 +02:00
"message": msg,
2021-10-21 14:42:01 +02:00
"code": "index_already_exists",
"type": "invalid_request",
2023-01-19 16:10:05 +01:00
"link":"https://docs.meilisearch.com/errors#index_already_exists"
2021-10-21 14:42:01 +02:00
});
assert_eq!(response["error"], expected_response);
2021-10-21 14:42:01 +02:00
}
#[actix_rt::test]
async fn error_create_with_invalid_index_uid() {
let server = Server::new().await;
let index = server.index("test test#!");
let (response, code) = index.create(None).await;
snapshot!(code, @"400 Bad Request");
snapshot!(json_string!(response), @r###"
{
2024-09-16 22:22:24 +03:00
"message": "Invalid value at `.uid`: `test test#!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_), and can not be more than 512 bytes.",
"code": "invalid_index_uid",
"type": "invalid_request",
2023-01-19 15:48:20 +01:00
"link": "https://docs.meilisearch.com/errors#invalid_index_uid"
}
"###);
2021-10-21 14:42:01 +02:00
}
2023-09-07 11:16:51 +02:00
#[actix_rt::test]
async fn send_task_id() {
let temp = tempfile::tempdir().unwrap();
2024-02-21 14:33:40 +01:00
let options =
Opt { experimental_replication_parameters: true, ..default_settings(temp.path()) };
let server = Server::new_with_options(options).await.unwrap();
2023-09-07 11:16:51 +02:00
let app = server.init_web_app().await;
let index = server.index("catto");
let (response, code) = index.create(None).await;
snapshot!(code, @"202 Accepted");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{
"taskUid": 0,
"indexUid": "catto",
"status": "enqueued",
"type": "indexCreation",
"enqueuedAt": "[date]"
}
"###);
let body = serde_json::to_string(&json!({
"uid": "doggo",
"primaryKey": None::<&str>,
}))
.unwrap();
let req = test::TestRequest::post()
.uri("/indexes")
.insert_header(("TaskId", "25"))
.insert_header(ContentType::json())
.set_payload(body)
.to_request();
let res = test::call_service(&app, req).await;
snapshot!(res.status(), @"202 Accepted");
let bytes = test::read_body(res).await;
let response = serde_json::from_slice::<Value>(&bytes).expect("Expecting valid json");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{
"taskUid": 25,
"indexUid": "doggo",
"status": "enqueued",
"type": "indexCreation",
"enqueuedAt": "[date]"
}
"###);
let body = serde_json::to_string(&json!({
"uid": "girafo",
"primaryKey": None::<&str>,
}))
.unwrap();
let req = test::TestRequest::post()
.uri("/indexes")
.insert_header(("TaskId", "12"))
.insert_header(ContentType::json())
.set_payload(body)
.to_request();
let res = test::call_service(&app, req).await;
snapshot!(res.status(), @"400 Bad Request");
let bytes = test::read_body(res).await;
let response = serde_json::from_slice::<Value>(&bytes).expect("Expecting valid json");
snapshot!(json_string!(response), @r###"
{
"message": "Received bad task id: 12 should be >= to 26.",
"code": "bad_request",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#bad_request"
}
"###);
}