From 8af8aa5a334d26c5dfd06f09f34863de5b52bb89 Mon Sep 17 00:00:00 2001 From: Tamo Date: Tue, 11 Apr 2023 19:34:25 +0200 Subject: [PATCH] add a test --- meilisearch/tests/common/index.rs | 5 + .../tests/documents/delete_documents.rs | 120 ++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/meilisearch/tests/common/index.rs b/meilisearch/tests/common/index.rs index 96ac5ce2e..5d38b84af 100644 --- a/meilisearch/tests/common/index.rs +++ b/meilisearch/tests/common/index.rs @@ -225,6 +225,11 @@ impl Index<'_> { self.service.delete(url).await } + pub async fn delete_document_by_filter(&self, body: Value) -> (Value, StatusCode) { + let url = format!("/indexes/{}/documents/delete-batch", urlencode(self.uid.as_ref())); + self.service.post_encoded(url, body, self.encoder).await + } + pub async fn clear_all_documents(&self) -> (Value, StatusCode) { let url = format!("/indexes/{}/documents", urlencode(self.uid.as_ref())); self.service.delete(url).await diff --git a/meilisearch/tests/documents/delete_documents.rs b/meilisearch/tests/documents/delete_documents.rs index e36e2f033..a34bcfa95 100644 --- a/meilisearch/tests/documents/delete_documents.rs +++ b/meilisearch/tests/documents/delete_documents.rs @@ -1,3 +1,4 @@ +use meili_snap::{json_string, snapshot}; use serde_json::json; use crate::common::{GetAllDocumentsOptions, Server}; @@ -135,3 +136,122 @@ async fn delete_no_document_batch() { assert_eq!(code, 200); assert_eq!(response["results"].as_array().unwrap().len(), 3); } + +#[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 (response, code) = + index.delete_document_by_filter(json!({ "filter": "color = blue"})).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": { + "deletedDocuments": 2, + "originalFilter": "\"color = blue\"" + }, + "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 + } + ], + "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]" + } + "###); + + 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": { + "deletedDocuments": 2, + "originalFilter": "\"color = blue\"" + }, + "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 + } + "###); +}