Skip to content

Commit

Permalink
Fix the display of timer schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Dec 12, 2023
1 parent a2aa6c0 commit 405e1ee
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Bugfixes

* Fix the display of timer schedules.
14 changes: 9 additions & 5 deletions src/globus_cli/commands/timer/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,23 @@ def render(self, value: dict[str, t.Any]) -> str:
if value.get("type") == "once":
when = value.get("datetime")
if when:
return f"once at {when}"
timestamp = formatters.Date.render(formatters.Date.parse(when))
return f"once at {timestamp}"
else: # should be unreachable
return "once"
elif value.get("type") == "recurring":
interval = value.get("interval_seconds")
start = value.get("start")
if start:
start = formatters.Date.render(formatters.Date.parse(start))
end = value.get("end", {})

ret = f"every {interval} seconds, starting {start}"
if end.get("condition") == "time":
ret += f" and running until {end['datetime']}"
elif end.get("condition") == "iterations":
ret += f" and running for {end['iterations']} iterations"
if end.get("datetime"):
stop = formatters.Date.render(formatters.Date.parse(end["datetime"]))
ret += f" and running until {stop}"
elif end.get("count"):
ret += f" and running for {end['count']} iterations"
return ret
else: # should be unreachable
return f"unrecognized schedule type: {value}"
Expand Down
59 changes: 58 additions & 1 deletion tests/unit/formatters/test_timer_formatters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import datetime

import pytest

from globus_cli.commands.timer._common import CallbackActionTypeFormatter
from globus_cli.commands.timer._common import (
CallbackActionTypeFormatter,
ScheduleFormatter,
)


@pytest.mark.parametrize(
Expand All @@ -14,3 +19,55 @@
)
def test_action_formatter(value, expected):
assert CallbackActionTypeFormatter().render(value) == expected


start_string = "2023-12-19T21:00:00+03:00"
end_string = "2024-01-01T15:16:17-14:00"
start_rendered = (
datetime.datetime.fromisoformat(start_string)
.astimezone()
.strftime("%Y-%m-%d %H:%M:%S")
)
end_rendered = (
datetime.datetime.fromisoformat(end_string)
.astimezone()
.strftime("%Y-%m-%d %H:%M:%S")
)


@pytest.mark.parametrize(
"value, expected_template",
(
pytest.param(
{
"end": {"count": 3},
"interval_seconds": 86400,
"start": start_string,
"type": "recurring",
},
"every 86400 seconds, starting {start} and running for 3 iterations",
id="start-end-count",
),
pytest.param(
{
"end": {"datetime": end_string},
"interval_seconds": 86400,
"start": start_string,
"type": "recurring",
},
"every 86400 seconds, starting {start} and running until {end}",
id="start-end-datetime",
),
pytest.param(
{
"datetime": start_string,
"type": "once",
},
"once at {start}",
id="start-once",
),
),
)
def test_schedule_formatter(value, expected_template):
expected = expected_template.format(start=start_rendered, end=end_rendered)
assert ScheduleFormatter().render(value) == expected

0 comments on commit 405e1ee

Please sign in to comment.