Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default cancel parameters #328

Merged
merged 1 commit into from Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions meilisearch_python_async/task.py
Expand Up @@ -25,6 +25,8 @@ async def cancel_tasks(
) -> TaskInfo:
"""Cancel a list of enqueued or processing tasks.

Defaults to cancelling all tasks.

Args:

uids: A list of task UIDs to cancel.
Expand Down Expand Up @@ -72,6 +74,11 @@ async def cancel_tasks(
parameters["beforeStartedAt"] = str(before_started_at)
if after_finished_at:
parameters["afterFinishedAt"] = str(after_finished_at)

if not parameters:
# Cancel all tasks if no parmaeters provided
parameters["statuses"] = "enqueued,processing"

url = f"tasks/cancel?{urlencode(parameters)}"
response = await http_client.post(url)

Expand Down
20 changes: 18 additions & 2 deletions tests/test_task.py
Expand Up @@ -4,6 +4,18 @@
from meilisearch_python_async.task import cancel_tasks, get_task, get_tasks, wait_for_task


async def test_cancel_every_task(test_client):
task = await cancel_tasks(test_client.http_client, statuses=["enqueued", "processing"])
tasks = await get_tasks(test_client.http_client, types="taskCancelation")

assert task.task_uid is not None
assert task.index_uids is None
assert task.status in {"enqueued", "processing", "succeeded"}
assert task.task_type == "taskCancelation"
assert tasks[0].details is not None
assert "statuses=enqueued%2Cprocessing" in tasks[0].details["originalFilter"]


async def test_cancel_tasks(test_client):
task = await cancel_tasks(test_client.http_client, uids=["1", "2"])
tasks = await get_tasks(test_client.http_client, types=["taskCancelation"])
Expand All @@ -16,12 +28,16 @@ async def test_cancel_tasks(test_client):
assert "uids=1%2C2" in tasks[0].details["originalFilter"]


async def test_cancel_every_task(test_client):
task = await cancel_tasks(test_client.http_client, statuses=["enqueued", "processing"])
async def test_cancel_task_no_params(test_client):
task = await cancel_tasks(test_client.http_client)
tasks = await get_tasks(test_client.http_client, types="taskCancelation")

assert task.task_uid is not None
assert task.index_uids is None
assert task.status in {"enqueued", "processing", "succeeded"}
assert task.task_type == "taskCancelation"
assert tasks[0].details is not None
assert "statuses=enqueued%2Cprocessing" in tasks[0].details["originalFilter"]


async def test_get_tasks(empty_index, small_movies):
Expand Down