Skip to content

Commit

Permalink
test: stablize tests running near 00:00 (#785)
Browse files Browse the repository at this point in the history
* fix: stablize tests running near 00:00

* fix linting
  • Loading branch information
YogevBokobza authored Aug 5, 2024
1 parent 645eb62 commit f5792ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pytest-mockservers = "^0.6.0"
pytest-sugar = "^0.9.4"
time-machine = "^2.7.0"
yamllint = "^1.26.3"
freezegun = ">=1.5.1"

[tool.poetry.group.docs.dependencies]
mkdocs = "^1.3.0"
Expand Down
7 changes: 6 additions & 1 deletion src/aioswitcher/schedule/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ def pretty_next_run(start_time: str, days: Set[Days] = set()) -> str:
current_datetime.time().strftime("%H:%M"), "%H:%M"
).time()
schedule_time = datetime.strptime(start_time, "%H:%M").time()
current_time_plus_one_hour = (
datetime.combine(datetime.today(), current_time) + timedelta(hours=1)
).time()

execution_days = [d.weekday for d in days]
# if scheduled for later on today, return "due today"
if current_weekday in execution_days and current_time < schedule_time:
if current_weekday in execution_days and (
current_time < schedule_time or current_time_plus_one_hour >= schedule_time
):
return f"Due today at {start_time}"

execution_days.sort()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_schedule_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import time_machine
from assertpy import assert_that
from freezegun import freeze_time
from pytest import fixture, mark

from aioswitcher.schedule import Days, tools
Expand Down Expand Up @@ -52,6 +53,13 @@ def test_pretty_next_run_with_todays_day_should_return_due_today(todays_day, one
assert_that(tools.pretty_next_run(one_hour_from_now, {todays_day})).is_equal_to(expected_return)


@freeze_time("23:00:00")
def test_pretty_next_run_with_todays_day_end_of_day_should_return_due_today(todays_day):
one_hour_from_now = "00:00"
expected_return = f"Due today at {one_hour_from_now}"
assert_that(tools.pretty_next_run(one_hour_from_now, {todays_day})).is_equal_to(expected_return)


def test_pretty_next_run_with_multiple_days_should_return_due_the_closest_day(today):
two_days_from_now = today + timedelta(days=2)
four_days_from_now = today + timedelta(days=4)
Expand Down

0 comments on commit f5792ef

Please sign in to comment.