From 7f1e3f66df4bd3badb9eb1f23f436ce707bdabcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Mus=C3=ADlek?= Date: Tue, 19 Mar 2024 16:42:03 +0100 Subject: [PATCH] Fix parsing of defer CLI command --at and --in options --- procrastinate/cli.py | 3 +- tests/integration/test_cli.py | 62 +++++++++++++++++++++++++++++++++++ tests/unit/test_cli.py | 6 ++-- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/procrastinate/cli.py b/procrastinate/cli.py index f4c3b5d2d..431370f46 100644 --- a/procrastinate/cli.py +++ b/procrastinate/cli.py @@ -393,6 +393,7 @@ def configure_defer_parser(subparsers: argparse._SubParsersAction): time_group, "--at", default=argparse.SUPPRESS, + dest="schedule_at", type=utils.parse_datetime, help="ISO-8601 localized datetime after which to launch the job", envvar="DEFER_AT", @@ -401,7 +402,7 @@ def configure_defer_parser(subparsers: argparse._SubParsersAction): time_group, "--in", default=argparse.SUPPRESS, - dest="in_", + dest="schedule_in", type=lambda s: {"seconds": int(s)}, help="Number of seconds after which to launch the job", envvar="DEFER_IN", diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index c0c4f4629..ae1b0f400 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -2,6 +2,7 @@ import asyncio import dataclasses +import datetime import logging import os @@ -169,6 +170,67 @@ def mytask(a): } +async def test_defer_at(entrypoint, cli_app, connector): + @cli_app.task(name="hello") + def mytask(a): + pass + + # No space in the json helps entrypoint() to simply split args + result = await entrypoint( + """defer --lock=sherlock --at=2020-01-01T12:00:00Z hello {"a":1}""" + ) + + assert "Launching a job: hello(a=1)\n" in result.stderr + assert result.exit_code == 0 + assert connector.jobs == { + 1: { + "args": {"a": 1}, + "attempts": 0, + "id": 1, + "lock": "sherlock", + "queueing_lock": None, + "queue_name": "default", + "scheduled_at": datetime.datetime( + 2020, 1, 1, 12, tzinfo=datetime.timezone.utc + ), + "status": "todo", + "task_name": "hello", + } + } + + +async def test_defer_in(entrypoint, cli_app, connector): + @cli_app.task(name="hello") + def mytask(a): + pass + + now = datetime.datetime.now(datetime.timezone.utc) + + # No space in the json helps entrypoint() to simply split args + result = await entrypoint("""defer --lock=sherlock --in=10 hello {"a":1}""") + + assert "Launching a job: hello(a=1)\n" in result.stderr + assert result.exit_code == 0 + assert len(connector.jobs) == 1 + job = connector.jobs[1] + scheduled_at = job.pop("scheduled_at") + assert job == { + "args": {"a": 1}, + "attempts": 0, + "id": 1, + "lock": "sherlock", + "queueing_lock": None, + "queue_name": "default", + "status": "todo", + "task_name": "hello", + } + assert ( + now + datetime.timedelta(seconds=9) + < scheduled_at + < now + datetime.timedelta(seconds=11) + ) + + async def test_defer_queueing_lock(entrypoint, cli_app, connector): @cli_app.task(name="hello") def mytask(a): diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 6a09e91f9..02443af70 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -60,14 +60,16 @@ def test_main(mocker): ["defer", "x", "--at", "2023-01-01T00:00:00"], { "command": "defer", - "at": datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc), + "schedule_at": datetime.datetime( + 2023, 1, 1, tzinfo=datetime.timezone.utc + ), }, ), ( ["defer", "x", "--in", "3600"], { "command": "defer", - "in_": {"seconds": 3600}, + "schedule_in": {"seconds": 3600}, }, ), (