meilisearch/meilisearch-http/tests/index/update_index.rs

107 lines
2.8 KiB
Rust
Raw Permalink Normal View History

2021-02-20 02:26:42 +08:00
use crate::common::Server;
2021-10-21 20:42:01 +08:00
use serde_json::json;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
2021-02-20 02:26:42 +08:00
#[actix_rt::test]
async fn update_primary_key() {
let server = Server::new().await;
let index = server.index("test");
let (_, code) = index.create(None).await;
assert_eq!(code, 202);
2021-02-20 02:26:42 +08:00
index.update(Some("primary")).await;
let response = index.wait_task(1).await;
assert_eq!(response["status"], "succeeded");
let (response, code) = index.get().await;
2021-02-20 02:26:42 +08:00
assert_eq!(code, 200);
2021-02-20 02:26:42 +08:00
assert_eq!(response["uid"], "test");
2021-03-16 01:38:13 +08:00
assert_eq!(response["name"], "test");
2021-02-20 02:26:42 +08:00
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-20 02:26:42 +08:00
assert!(created_at < updated_at);
assert_eq!(response["primaryKey"], "primary");
2021-03-16 01:38:13 +08:00
assert_eq!(response.as_object().unwrap().len(), 5);
2021-02-20 02:26:42 +08:00
}
#[actix_rt::test]
async fn update_nothing() {
let server = Server::new().await;
let index = server.index("test");
let (_, code) = index.create(None).await;
2021-02-20 02:26:42 +08:00
assert_eq!(code, 202);
2021-02-20 02:26:42 +08:00
index.wait_task(0).await;
2021-02-20 02:26:42 +08:00
let (_, code) = index.update(None).await;
assert_eq!(code, 202);
let response = index.wait_task(1).await;
assert_eq!(response["status"], "succeeded");
2021-02-20 02:26:42 +08:00
}
#[actix_rt::test]
2021-10-21 20:42:01 +08:00
async fn error_update_existing_primary_key() {
2021-02-20 02:26:42 +08:00
let server = Server::new().await;
let index = server.index("test");
2021-10-27 01:36:48 +08:00
let (_response, code) = index.create(Some("id")).await;
2021-02-20 02:26:42 +08:00
assert_eq!(code, 202);
2021-02-20 02:26:42 +08:00
2021-10-27 01:36:48 +08:00
let documents = json!([
{
"id": "11",
"content": "foobar"
}
]);
index.add_documents(documents, None).await;
let (_, code) = index.update(Some("primary")).await;
assert_eq!(code, 202);
let response = index.wait_task(2).await;
2021-02-20 02:26:42 +08:00
2021-10-21 20:42:01 +08:00
let expected_response = json!({
2021-10-27 01:36:48 +08:00
"message": "Index already has a primary key: `id`.",
2021-10-21 20:42:01 +08:00
"code": "index_primary_key_already_exists",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#index_primary_key_already_exists"
});
assert_eq!(response["error"], expected_response);
2021-02-20 02:26:42 +08:00
}
#[actix_rt::test]
2021-10-21 20:42:01 +08:00
async fn error_update_unexisting_index() {
2021-02-20 02:26:42 +08:00
let server = Server::new().await;
let (_, code) = server.index("test").update(None).await;
assert_eq!(code, 202);
let response = server.index("test").wait_task(0).await;
2021-10-21 20:42:01 +08:00
let expected_response = json!({
2021-10-27 01:36:48 +08:00
"message": "Index `test` not found.",
2021-10-21 20:42:01 +08:00
"code": "index_not_found",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#index_not_found"
});
assert_eq!(response["error"], expected_response);
2021-02-20 02:26:42 +08:00
}