MeiliSearch/meilisearch/tests/documents/delete_documents.rs

427 lines
12 KiB
Rust
Raw Normal View History

2023-04-11 19:34:25 +02:00
use meili_snap::{json_string, snapshot};
2021-02-22 16:03:17 +01:00
use serde_json::json;
2021-03-15 18:11:10 +01:00
use crate::common::{GetAllDocumentsOptions, Server};
2021-02-22 16:03:17 +01: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 16:03:17 +01: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 16:25:27 +02: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 16:03:17 +01:00
}
#[actix_rt::test]
async fn delete_one_document() {
let server = Server::new().await;
let index = server.index("test");
2022-10-20 18:00:07 +02:00
index.add_documents(json!([{ "id": 0, "content": "foobar" }]), None).await;
index.wait_task(0).await;
2021-02-22 16:03:17 +01:00
let (_response, code) = server.index("test").delete_document(0).await;
assert_eq!(code, 202);
index.wait_task(1).await;
2021-02-22 16:03:17 +01:00
let (_response, code) = index.get_document(0, None).await;
2021-06-15 22:21:52 +02:00
assert_eq!(code, 404);
2021-02-22 16:03:17 +01: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 16:03:17 +01:00
}
#[actix_rt::test]
async fn clear_all_documents() {
let server = Server::new().await;
let index = server.index("test");
2021-03-15 18:11:10 +01:00
index
.add_documents(
json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }]),
None,
)
.await;
index.wait_task(0).await;
2021-02-22 16:03:17 +01:00
let (_response, code) = index.clear_all_documents().await;
assert_eq!(code, 202);
2021-02-22 16:03:17 +01:00
let _update = index.wait_task(1).await;
2022-10-20 18:00:07 +02:00
let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
2021-02-22 16:03:17 +01:00
assert_eq!(code, 200);
assert!(response["results"].as_array().unwrap().is_empty());
2021-02-22 16:03:17 +01:00
}
#[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 16:03:17 +01:00
let _update = index.wait_task(0).await;
2022-10-20 18:00:07 +02:00
let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
2021-02-22 16:03:17 +01:00
assert_eq!(code, 200);
assert!(response["results"].as_array().unwrap().is_empty());
2021-02-22 16:03:17 +01:00
}
2021-02-23 14:13:43 +01:00
#[actix_rt::test]
2021-10-21 14:42:01 +02:00
async fn error_delete_batch_unexisting_index() {
2021-02-23 14:13:43 +01:00
let server = Server::new().await;
let index = server.index("test");
let (_, code) = index.delete_batch(vec![]).await;
2021-10-21 14:42:01 +02:00
let expected_response = json!({
2021-10-26 19:36:48 +02:00
"message": "Index `test` 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!(code, 202);
let response = index.wait_task(0).await;
assert_eq!(response["status"], "failed");
assert_eq!(response["error"], expected_response);
2021-02-23 14:13:43 +01: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 14:13:43 +01:00
let (_response, code) = index.delete_batch(vec![1, 0]).await;
assert_eq!(code, 202);
2021-02-23 14:13:43 +01:00
let _update = index.wait_task(1).await;
2022-10-20 18:00:07 +02:00
let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
2021-02-23 14:13:43 +01:00
assert_eq!(code, 200);
assert_eq!(response["results"].as_array().unwrap().len(), 1);
assert_eq!(response["results"][0]["id"], json!(3));
2021-02-23 14:13:43 +01:00
}
#[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 14:13:43 +01:00
let (_response, code) = index.delete_batch(vec![]).await;
2021-06-09 17:10:10 +02:00
assert_eq!(code, 202, "{}", _response);
2021-02-23 14:13:43 +01:00
let _update = index.wait_task(1).await;
2022-10-20 18:00:07 +02:00
let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
2021-02-23 14:13:43 +01:00
assert_eq!(code, 200);
assert_eq!(response["results"].as_array().unwrap().len(), 3);
2021-02-23 14:13:43 +01:00
}
2023-04-11 19:34:25 +02:00
#[actix_rt::test]
async fn delete_document_by_filter() {
let server = Server::new().await;
let index = server.index("doggo");
index.update_settings_filterable_attributes(json!(["color"])).await;
index
.add_documents(
json!([
{ "id": 0, "color": "red" },
{ "id": 1, "color": "blue" },
{ "id": 2, "color": "blue" },
{ "id": 3 },
]),
Some("id"),
)
.await;
index.wait_task(1).await;
let (stats, _) = index.stats().await;
snapshot!(json_string!(stats), @r###"
{
"numberOfDocuments": 4,
"isIndexing": false,
"fieldDistribution": {
"color": 3,
"id": 4
}
}
"###);
2023-04-11 19:34:25 +02:00
let (response, code) =
index.delete_document_by_filter(json!({ "filter": "color = blue"})).await;
2023-05-02 17:46:04 +02:00
snapshot!(code, @"202 Accepted");
2023-04-11 19:34:25 +02:00
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{
2023-04-27 19:28:05 +02:00
"taskUid": 2,
"indexUid": "doggo",
"status": "enqueued",
"type": "documentDeletion",
"enqueuedAt": "[date]"
2023-04-11 19:34:25 +02:00
}
"###);
let response = index.wait_task(2).await;
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###"
{
"uid": 2,
"indexUid": "doggo",
"status": "succeeded",
"type": "documentDeletion",
"canceledBy": null,
"details": {
2023-05-02 22:53:16 +02:00
"providedIds": 0,
2023-04-11 19:34:25 +02:00
"deletedDocuments": 2,
"originalFilter": "\"color = blue\""
},
"error": null,
"duration": "[duration]",
"enqueuedAt": "[date]",
"startedAt": "[date]",
"finishedAt": "[date]"
}
"###);
let (stats, _) = index.stats().await;
snapshot!(json_string!(stats), @r###"
{
"numberOfDocuments": 2,
"isIndexing": false,
"fieldDistribution": {
"color": 1,
"id": 2
}
}
"###);
2023-04-11 19:34:25 +02:00
let (documents, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
snapshot!(code, @"200 OK");
snapshot!(json_string!(documents), @r###"
{
"results": [
{
"id": 0,
"color": "red"
},
{
"id": 3
}
],
"offset": 0,
"limit": 20,
"total": 2
}
"###);
let (response, code) =
index.delete_document_by_filter(json!({ "filter": "color NOT EXISTS"})).await;
snapshot!(code, @"202 Accepted");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###"
{
"taskUid": 3,
"indexUid": "doggo",
"status": "enqueued",
"type": "documentDeletion",
"enqueuedAt": "[date]"
}
"###);
2023-05-03 17:41:35 +02:00
let response = index.wait_task(3).await;
2023-04-11 19:34:25 +02:00
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###"
{
2023-05-03 17:41:35 +02:00
"uid": 3,
2023-04-11 19:34:25 +02:00
"indexUid": "doggo",
"status": "succeeded",
"type": "documentDeletion",
"canceledBy": null,
"details": {
2023-05-02 22:53:16 +02:00
"providedIds": 0,
2023-05-03 17:41:35 +02:00
"deletedDocuments": 1,
"originalFilter": "\"color NOT EXISTS\""
2023-04-11 19:34:25 +02:00
},
"error": null,
"duration": "[duration]",
"enqueuedAt": "[date]",
"startedAt": "[date]",
"finishedAt": "[date]"
}
"###);
let (stats, _) = index.stats().await;
snapshot!(json_string!(stats), @r###"
{
"numberOfDocuments": 1,
"isIndexing": false,
"fieldDistribution": {
"color": 1,
"id": 1
}
}
"###);
2023-04-11 19:34:25 +02:00
let (documents, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
snapshot!(code, @"200 OK");
snapshot!(json_string!(documents), @r###"
{
"results": [
{
"id": 0,
"color": "red"
}
],
"offset": 0,
"limit": 20,
2023-05-02 17:46:04 +02:00
"total": 1
2023-04-11 19:34:25 +02:00
}
"###);
}
2023-05-03 20:07:08 +02:00
#[actix_rt::test]
async fn delete_document_by_complex_filter() {
let server = Server::new().await;
let index = server.index("doggo");
index.update_settings_filterable_attributes(json!(["color"])).await;
index
.add_documents(
json!([
{ "id": 0, "color": "red" },
{ "id": 1, "color": "blue" },
{ "id": 2, "color": "blue" },
{ "id": 3, "color": "green" },
{ "id": 4 },
]),
Some("id"),
)
.await;
index.wait_task(1).await;
let (response, code) = index
.delete_document_by_filter(
json!({ "filter": ["color != red", "color != green", "color EXISTS"] }),
)
.await;
snapshot!(code, @"202 Accepted");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{
"taskUid": 2,
"indexUid": "doggo",
"status": "enqueued",
"type": "documentDeletion",
"enqueuedAt": "[date]"
}
"###);
let response = index.wait_task(2).await;
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###"
{
"uid": 2,
"indexUid": "doggo",
"status": "succeeded",
"type": "documentDeletion",
"canceledBy": null,
"details": {
"providedIds": 0,
"deletedDocuments": 2,
"originalFilter": "[\"color != red\",\"color != green\",\"color EXISTS\"]"
},
"error": null,
"duration": "[duration]",
"enqueuedAt": "[date]",
"startedAt": "[date]",
"finishedAt": "[date]"
}
"###);
let (documents, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
snapshot!(code, @"200 OK");
snapshot!(json_string!(documents), @r###"
{
"results": [
{
"id": 0,
"color": "red"
},
{
"id": 3,
"color": "green"
},
{
"id": 4
}
],
"offset": 0,
"limit": 20,
"total": 3
}
"###);
let (response, code) = index
.delete_document_by_filter(json!({ "filter": [["color = green", "color NOT EXISTS"]] }))
.await;
snapshot!(code, @"202 Accepted");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###"
{
"taskUid": 3,
"indexUid": "doggo",
"status": "enqueued",
"type": "documentDeletion",
"enqueuedAt": "[date]"
}
"###);
let response = index.wait_task(3).await;
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###"
{
"uid": 3,
"indexUid": "doggo",
"status": "succeeded",
"type": "documentDeletion",
"canceledBy": null,
"details": {
"providedIds": 0,
"deletedDocuments": 4,
"originalFilter": "[[\"color = green\",\"color NOT EXISTS\"]]"
},
"error": null,
"duration": "[duration]",
"enqueuedAt": "[date]",
"startedAt": "[date]",
"finishedAt": "[date]"
}
"###);
let (documents, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await;
snapshot!(code, @"200 OK");
snapshot!(json_string!(documents), @r###"
{
"results": [
{
"id": 0,
"color": "red"
}
],
"offset": 0,
"limit": 20,
"total": 1
}
"###);
}