MeiliSearch/meilisearch/tests/index/update_index.rs

126 lines
3.5 KiB
Rust
Raw Normal View History

2022-10-20 18:00:07 +02:00
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use crate::common::encoder::Encoder;
2021-02-19 19:26:42 +01:00
use crate::common::Server;
2023-09-11 16:50:53 +02:00
use crate::json;
2021-02-19 19:26:42 +01:00
#[actix_rt::test]
async fn update_primary_key() {
let server = Server::new_shared().await;
let index = server.unique_index();
let (task, code) = index.create(None).await;
2021-02-19 19:26:42 +01:00
assert_eq!(code, 202);
2021-02-19 19:26:42 +01:00
index.update(Some("primary")).await;
let response = index.wait_task(task.uid()).await.succeeded();
assert_eq!(response["status"], "succeeded");
let (response, code) = index.get().await;
2021-02-19 19:26:42 +01:00
assert_eq!(code, 200);
2021-02-19 19:26:42 +01:00
assert_eq!(response["uid"], "test");
assert!(response.get("createdAt").is_some());
assert!(response.get("updatedAt").is_some());
let created_at =
OffsetDateTime::parse(response["createdAt"].as_str().unwrap(), &Rfc3339).unwrap();
let updated_at =
OffsetDateTime::parse(response["updatedAt"].as_str().unwrap(), &Rfc3339).unwrap();
2021-02-19 19:26:42 +01:00
assert!(created_at < updated_at);
assert_eq!(response["primaryKey"], "primary");
assert_eq!(response.as_object().unwrap().len(), 4);
2021-02-19 19:26:42 +01:00
}
#[actix_rt::test]
async fn create_and_update_with_different_encoding() {
let server = Server::new_shared().await;
let index = server.index_with_encoder("test", Encoder::Gzip);
let (task, code) = index.create(None).await;
assert_eq!(code, 202);
let index = server.index_with_encoder("test", Encoder::Brotli);
index.update(Some("primary")).await;
let response = index.wait_task(task.uid()).await.succeeded();
assert_eq!(response["status"], "succeeded");
}
2021-02-19 19:26:42 +01:00
#[actix_rt::test]
async fn update_nothing() {
let server = Server::new_shared().await;
let index = server.unique_index();
let (task, code) = index.create(None).await;
2021-02-19 19:26:42 +01:00
assert_eq!(code, 202);
2021-02-19 19:26:42 +01:00
index.wait_task(task.uid()).await.succeeded();
2021-02-19 19:26:42 +01:00
let (task, code) = index.update(None).await;
assert_eq!(code, 202);
let response = index.wait_task(task.uid()).await.succeeded();
assert_eq!(response["status"], "succeeded");
2021-02-19 19:26:42 +01:00
}
#[actix_rt::test]
2021-10-21 14:42:01 +02:00
async fn error_update_existing_primary_key() {
let server = Server::new_shared().await;
let index = server.unique_index();
let (task, code) = index.create(Some("id")).await;
2021-02-19 19:26:42 +01:00
assert_eq!(code, 202);
2021-02-19 19:26:42 +01:00
2021-10-26 19:36:48 +02:00
let documents = json!([
{
"id": "11",
"content": "foobar"
}
]);
index.add_documents(documents, None).await;
let (task, code) = index.update(Some("primary")).await;
assert_eq!(code, 202);
let response = index.wait_task(task.uid()).await.succeeded();
2021-02-19 19:26:42 +01:00
2021-10-21 14:42:01 +02:00
let expected_response = json!({
2021-10-26 19:36:48 +02:00
"message": "Index already has a primary key: `id`.",
2021-10-21 14:42:01 +02:00
"code": "index_primary_key_already_exists",
"type": "invalid_request",
2023-01-19 16:10:05 +01:00
"link": "https://docs.meilisearch.com/errors#index_primary_key_already_exists"
2021-10-21 14:42:01 +02:00
});
assert_eq!(response["error"], expected_response);
2021-02-19 19:26:42 +01:00
}
#[actix_rt::test]
2021-10-21 14:42:01 +02:00
async fn error_update_unexisting_index() {
let server = Server::new_shared().await;
let index = shared_does_not_exists_index();
let (task, code) = index.update(None).await;
assert_eq!(code, 202);
let response = index.wait_task(task.uid()).await.succeeded();
2021-10-21 14:42:01 +02:00
let expected_response = json!({
"message": "Index `DOES_NOT_EXISTS` not found.",
2021-10-21 14:42:01 +02:00
"code": "index_not_found",
"type": "invalid_request",
2023-01-19 16:10:05 +01:00
"link": "https://docs.meilisearch.com/errors#index_not_found"
2021-10-21 14:42:01 +02:00
});
assert_eq!(response["error"], expected_response);
2021-02-19 19:26:42 +01:00
}