From 73e82d67d775ce298843e06d877c98a0221722ae Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Wed, 6 Aug 2025 11:14:07 +0200 Subject: [PATCH] Add test --- crates/meilisearch/tests/common/mod.rs | 16 ++++ crates/meilisearch/tests/common/server.rs | 4 +- crates/meilisearch/tests/tasks/deletion.rs | 97 ++++++++++++++++++++++ crates/meilisearch/tests/tasks/mod.rs | 1 + 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 crates/meilisearch/tests/tasks/deletion.rs diff --git a/crates/meilisearch/tests/common/mod.rs b/crates/meilisearch/tests/common/mod.rs index 03b1271f1..2f9d5021a 100644 --- a/crates/meilisearch/tests/common/mod.rs +++ b/crates/meilisearch/tests/common/mod.rs @@ -154,6 +154,22 @@ impl From> for Value { } } +pub trait IntoTaskUid { + fn uid(&self) -> u64; +} + +impl IntoTaskUid for Value { + fn uid(&self) -> u64 { + self["taskUid"].as_u64().expect("Value must contain a taskUid") + } +} + +impl IntoTaskUid for u64 { + fn uid(&self) -> u64 { + *self + } +} + #[macro_export] macro_rules! json { ($($json:tt)+) => { diff --git a/crates/meilisearch/tests/common/server.rs b/crates/meilisearch/tests/common/server.rs index 291356bf8..3e3ea79f2 100644 --- a/crates/meilisearch/tests/common/server.rs +++ b/crates/meilisearch/tests/common/server.rs @@ -426,7 +426,9 @@ impl Server { self.service.delete(format!("/tasks?{}", value)).await } - pub async fn wait_task(&self, update_id: u64) -> Value { + pub async fn wait_task(&self, update_id: impl super::IntoTaskUid) -> Value { + let update_id = update_id.uid(); + // try several times to get status, or panic to not wait forever let url = format!("/tasks/{update_id}"); let max_attempts = 400; // 200 seconds in total, 0.5secs per attempt diff --git a/crates/meilisearch/tests/tasks/deletion.rs b/crates/meilisearch/tests/tasks/deletion.rs new file mode 100644 index 000000000..a982f2285 --- /dev/null +++ b/crates/meilisearch/tests/tasks/deletion.rs @@ -0,0 +1,97 @@ +use crate::common::Server; +use crate::json; +use meili_snap::snapshot; + +// pub struct Query { +// /// The maximum number of tasks to be matched +// pub limit: Option, +// /// The minimum [task id](`meilisearch_types::tasks::Task::uid`) to be matched +// pub from: Option, +// /// The order used to return the tasks. By default the newest tasks are returned first and the boolean is `false`. +// pub reverse: Option, +// /// The [task ids](`meilisearch_types::tasks::Task::uid`) to be matched +// pub uids: Option>, +// /// The [batch ids](`meilisearch_types::batches::Batch::uid`) to be matched +// pub batch_uids: Option>, +// /// The allowed [statuses](`meilisearch_types::tasks::Task::status`) of the matched tasls +// pub statuses: Option>, +// /// The allowed [kinds](meilisearch_types::tasks::Kind) of the matched tasks. +// /// +// /// The kind of a task is given by: +// /// ``` +// /// # use meilisearch_types::tasks::{Task, Kind}; +// /// # fn doc_func(task: Task) -> Kind { +// /// task.kind.as_kind() +// /// # } +// /// ``` +// pub types: Option>, +// /// The allowed [index ids](meilisearch_types::tasks::Task::index_uid) of the matched tasks +// pub index_uids: Option>, +// /// The [task ids](`meilisearch_types::tasks::Task::uid`) of the [`TaskCancelation`](meilisearch_types::tasks::Task::Kind::TaskCancelation) tasks +// /// that canceled the matched tasks. +// pub canceled_by: Option>, +// /// Exclusive upper bound of the matched tasks' [`enqueued_at`](meilisearch_types::tasks::Task::enqueued_at) field. +// pub before_enqueued_at: Option, +// /// Exclusive lower bound of the matched tasks' [`enqueued_at`](meilisearch_types::tasks::Task::enqueued_at) field. +// pub after_enqueued_at: Option, +// /// Exclusive upper bound of the matched tasks' [`started_at`](meilisearch_types::tasks::Task::started_at) field. +// pub before_started_at: Option, +// /// Exclusive lower bound of the matched tasks' [`started_at`](meilisearch_types::tasks::Task::started_at) field. +// pub after_started_at: Option, +// /// Exclusive upper bound of the matched tasks' [`finished_at`](meilisearch_types::tasks::Task::finished_at) field. +// pub before_finished_at: Option, +// /// Exclusive lower bound of the matched tasks' [`finished_at`](meilisearch_types::tasks::Task::finished_at) field. +// pub after_finished_at: Option, +// } + +#[actix_rt::test] +async fn delete_task() { + let server = Server::new_shared(); + let index = server.unique_index(); + + // Add a document + let (task, code) = index + .add_documents(json!([{"id": 1, "free": "palestine", "asc_desc_rank": 1}]), Some("id")) + .await; + snapshot!(code, @r#"202 Accepted"#); + let task_uid = task["taskUid"].as_u64().unwrap(); + server.wait_task(task).await.succeeded(); + + // Delete tasks + let (task, code) = index.delete_tasks(format!("uids={task_uid}")).await; + snapshot!(code, @"200 OK"); + let value = server.wait_task(task).await.succeeded(); + snapshot!(value, @r#" + { + "uid": "[uid]", + "batchUid": "[batch_uid]", + "indexUid": null, + "status": "succeeded", + "type": "taskDeletion", + "canceledBy": null, + "details": { + "matchedTasks": 1, + "deletedTasks": 1, + "originalFilter": "?uids=0" + }, + "error": null, + "duration": "[duration]", + "enqueuedAt": "[date]", + "startedAt": "[date]", + "finishedAt": "[date]" + } + "#); + + // Check that the task is deleted + let (value, code) = index.list_tasks().await; + snapshot!(code, @r#"200 OK"#); + snapshot!(value, @r#" + { + "results": [], + "total": 0, + "limit": 20, + "from": null, + "next": null + } + "#); +} diff --git a/crates/meilisearch/tests/tasks/mod.rs b/crates/meilisearch/tests/tasks/mod.rs index 09700d3c5..45ee18f7f 100644 --- a/crates/meilisearch/tests/tasks/mod.rs +++ b/crates/meilisearch/tests/tasks/mod.rs @@ -1,3 +1,4 @@ +mod deletion; mod errors; mod webhook;