meilisearch/meilisearch-http/tests/documents/delete_documents.rs

148 lines
4.6 KiB
Rust
Raw Permalink Normal View History

2021-02-22 23:03:17 +08:00
use serde_json::json;
2021-03-16 01:11:10 +08:00
use crate::common::{GetAllDocumentsOptions, Server};
2021-02-22 23:03:17 +08:00
#[actix_rt::test]
async fn delete_one_document_unexisting_index() {
let server = Server::new().await;
let index = server.index("test");
let (_response, code) = index.delete_document(0).await;
assert_eq!(code, 202);
let response = index.wait_task(0).await;
assert_eq!(response["status"], "failed");
2021-02-22 23:03:17 +08:00
}
#[actix_rt::test]
async fn delete_one_unexisting_document() {
let server = Server::new().await;
let index = server.index("test");
index.create(None).await;
2021-06-24 22:25:27 +08:00
let (response, code) = index.delete_document(0).await;
assert_eq!(code, 202, "{}", response);
let update = index.wait_task(0).await;
assert_eq!(update["status"], "succeeded");
2021-02-22 23:03:17 +08:00
}
#[actix_rt::test]
async fn delete_one_document() {
let server = Server::new().await;
let index = server.index("test");
2021-03-16 01:11:10 +08:00
index
.add_documents(json!([{ "id": 0, "content": "foobar" }]), None)
.await;
index.wait_task(0).await;
2021-02-22 23:03:17 +08:00
let (_response, code) = server.index("test").delete_document(0).await;
assert_eq!(code, 202);
index.wait_task(1).await;
2021-02-22 23:03:17 +08:00
let (_response, code) = index.get_document(0, None).await;
2021-06-16 04:21:52 +08:00
assert_eq!(code, 404);
2021-02-22 23:03:17 +08:00
}
#[actix_rt::test]
async fn clear_all_documents_unexisting_index() {
let server = Server::new().await;
let index = server.index("test");
let (_response, code) = index.clear_all_documents().await;
assert_eq!(code, 202);
let response = index.wait_task(0).await;
assert_eq!(response["status"], "failed");
2021-02-22 23:03:17 +08:00
}
#[actix_rt::test]
async fn clear_all_documents() {
let server = Server::new().await;
let index = server.index("test");
2021-03-16 01:11:10 +08:00
index
.add_documents(
json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }]),
None,
)
.await;
index.wait_task(0).await;
2021-02-22 23:03:17 +08:00
let (_response, code) = index.clear_all_documents().await;
assert_eq!(code, 202);
2021-02-22 23:03:17 +08:00
let _update = index.wait_task(1).await;
2021-03-16 01:11:10 +08:00
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions::default())
.await;
2021-02-22 23:03:17 +08:00
assert_eq!(code, 200);
assert!(response.as_array().unwrap().is_empty());
}
#[actix_rt::test]
async fn clear_all_documents_empty_index() {
let server = Server::new().await;
let index = server.index("test");
index.create(None).await;
let (_response, code) = index.clear_all_documents().await;
assert_eq!(code, 202);
2021-02-22 23:03:17 +08:00
let _update = index.wait_task(0).await;
2021-03-16 01:11:10 +08:00
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions::default())
.await;
2021-02-22 23:03:17 +08:00
assert_eq!(code, 200);
assert!(response.as_array().unwrap().is_empty());
}
2021-02-23 21:13:43 +08:00
#[actix_rt::test]
2021-10-21 20:42:01 +08:00
async fn error_delete_batch_unexisting_index() {
2021-02-23 21:13:43 +08:00
let server = Server::new().await;
let index = server.index("test");
let (_, code) = index.delete_batch(vec![]).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!(code, 202);
let response = index.wait_task(0).await;
assert_eq!(response["status"], "failed");
assert_eq!(response["error"], expected_response);
2021-02-23 21:13:43 +08:00
}
#[actix_rt::test]
async fn delete_batch() {
let server = Server::new().await;
let index = server.index("test");
index.add_documents(json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }, { "id": 3, "content": "foobar" }]), Some("id")).await;
index.wait_task(0).await;
2021-02-23 21:13:43 +08:00
let (_response, code) = index.delete_batch(vec![1, 0]).await;
assert_eq!(code, 202);
2021-02-23 21:13:43 +08:00
let _update = index.wait_task(1).await;
2021-03-16 01:11:10 +08:00
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions::default())
.await;
2021-02-23 21:13:43 +08:00
assert_eq!(code, 200);
assert_eq!(response.as_array().unwrap().len(), 1);
assert_eq!(response.as_array().unwrap()[0]["id"], 3);
}
#[actix_rt::test]
async fn delete_no_document_batch() {
let server = Server::new().await;
let index = server.index("test");
index.add_documents(json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }, { "id": 3, "content": "foobar" }]), Some("id")).await;
index.wait_task(0).await;
2021-02-23 21:13:43 +08:00
let (_response, code) = index.delete_batch(vec![]).await;
2021-06-09 23:10:10 +08:00
assert_eq!(code, 202, "{}", _response);
2021-02-23 21:13:43 +08:00
let _update = index.wait_task(1).await;
2021-03-16 01:11:10 +08:00
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions::default())
.await;
2021-02-23 21:13:43 +08:00
assert_eq!(code, 200);
assert_eq!(response.as_array().unwrap().len(), 3);
}