Skip to content

Commit

Permalink
Add test for passing midnight.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurdok committed Jan 25, 2025
1 parent b860de8 commit 2d24691
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/spanreed/plugins/habit_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,4 @@ async def run_for_user(self, user: User) -> None:
await asyncio.sleep(
datetime.timedelta(hours=4).total_seconds()
)
self._logger.info("Passed midnight, re-asking")
50 changes: 48 additions & 2 deletions src/spanreed/plugins/habit_tracker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
from unittest.mock import MagicMock, patch, AsyncMock, call


@patch.object(asyncio, "timeout")
@patch.object(HabitTrackerPlugin, "get_config")
@patch_obsidian("spanreed.plugins.habit_tracker")
@patch_telegram_bot("spanreed.plugins.habit_tracker")
def test_run_for_user(
mock_bot: AsyncMock,
mock_obsidian: AsyncMock,
mock_get_config: AsyncMock,
mock_timeout: AsyncContextManager,
) -> None:
Plugin.reset_registry()
plugin = HabitTrackerPlugin()
Expand Down Expand Up @@ -61,3 +59,51 @@ def fake_user_choice(prompt: str, choices: list[str], columns: int) -> int:
property_name="habits",
value="habit2",
)


@patch.object(asyncio, "timeout")
@patch.object(HabitTrackerPlugin, "get_config")
@patch_obsidian("spanreed.plugins.habit_tracker")
@patch_telegram_bot("spanreed.plugins.habit_tracker")
def test_run_for_user_passed_midnight(
mock_bot: AsyncMock,
mock_obsidian: AsyncMock,
mock_get_config: AsyncMock,
mock_timeout: MagicMock,
) -> None:
Plugin.reset_registry()
plugin = HabitTrackerPlugin()

mock_user = mock_user_find_by_id(4)
mock_get_config.return_value = UserConfig(
daily_note_path="myDaily",
habit_tracker_property_name="habits",
habits=[
Habit("habit1", "my habit 1"),
Habit("habit2", "my habit 2"),
],
)
mock_obsidian.get_daily_note.return_value = "daily/2024-01-01.md"

def fake_user_choice(prompt: str, choices: list[str], columns: int) -> int:
if "Did you do any of these habits today?" in prompt:
assert "Habit1" in choices
assert "Cancel" in choices
if "Habit2" in choices:
return 1
raise asyncio.CancelledError

assert False, f"Unexpected prompt: {prompt}"

mock_bot.request_user_choice.side_effect = fake_user_choice

mock_timeout.side_effect = [TimeoutError, mock_timeout]

with pytest.raises(asyncio.CancelledError):
asyncio.run(plugin.run_for_user(user=mock_user))

mock_obsidian.add_value_to_list_property.assert_called_once_with(
filepath="daily/2024-01-01.md",
property_name="habits",
value="habit2",
)

0 comments on commit 2d24691

Please sign in to comment.