Refactor /remind list to use a normal embed and fix calculate() function

This commit is contained in:
2025-02-13 03:07:27 +01:00
parent f82c410097
commit 176a1a058c
5 changed files with 129 additions and 588 deletions

@ -3,8 +3,11 @@ from __future__ import annotations
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING
from apscheduler.job import Job
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.date import DateTrigger
from apscheduler.triggers.interval import IntervalTrigger
from discord_reminder_bot.misc import calc_time, calculate, get_human_time
@ -63,6 +66,10 @@ def test_get_human_time() -> None:
assert get_human_time(test_timedelta) == expected_output, assert_msg
def dummy_job() -> None:
"""Dummy job function for testing."""
def test_calculate() -> None:
"""Test the calculate function with various job inputs."""
scheduler = BackgroundScheduler()
@ -70,7 +77,7 @@ def test_calculate() -> None:
# Create a job with a DateTrigger
run_date = datetime(2270, 10, 1, 12, 0, 0, tzinfo=timezone.utc)
job: Job = scheduler.add_job(lambda: None, trigger=DateTrigger(run_date=run_date), id="test_job", name="Test Job")
job: Job = scheduler.add_job(dummy_job, trigger=DateTrigger(run_date=run_date), id="test_job", name="Test Job")
expected_output = "<t:9490737600:R>"
assert_msg: str = f"Expected {expected_output}, got {calculate(job)}"
@ -78,10 +85,60 @@ def test_calculate() -> None:
# Modify the job to have a next_run_time
job.modify(next_run_time=run_date)
assert_msg: str = f"Expected {expected_output}, got {calculate(job)}"
assert calculate(job) == expected_output, assert_msg
# Paused job should still return the same output
job.pause()
assert calculate(job) == expected_output, assert_msg
assert_msg: str = f"Expected None, got {calculate(job)}"
assert not calculate(job), assert_msg
scheduler.shutdown()
def test_calculate_cronjob() -> None:
"""Test the calculate function with a CronTrigger job."""
scheduler = BackgroundScheduler()
scheduler.start()
run_date = datetime(2270, 10, 1, 12, 0, 0, tzinfo=timezone.utc)
job: Job = scheduler.add_job(
dummy_job,
trigger=CronTrigger(
second=run_date.second,
minute=run_date.minute,
hour=run_date.hour,
day=run_date.day,
month=run_date.month,
year=run_date.year,
),
)
# Force next_run_time to expected value for testing
job.modify(next_run_time=run_date)
expected_output: str = f"<t:{int(run_date.timestamp())}:R>"
assert calculate(job) == expected_output, f"Expected {expected_output}, got {calculate(job)}"
# You can't pause a CronTrigger job so this should return the same output
job.pause()
assert calculate(job) == expected_output, f"Expected {expected_output}, got {calculate(job)}"
scheduler.shutdown()
def test_calculate_intervaljob() -> None:
"""Test the calculate function with an IntervalTrigger job."""
scheduler = BackgroundScheduler()
scheduler.start()
run_date = datetime(2270, 12, 31, 23, 59, 59, tzinfo=timezone.utc)
job = scheduler.add_job(dummy_job, trigger=IntervalTrigger(seconds=3600), id="test_interval_job", name="Test Interval Job")
# Force next_run_time to expected value for testing
job.modify(next_run_time=run_date)
expected_output = f"<t:{int(run_date.timestamp())}:R>"
assert calculate(job) == expected_output, f"Expected {expected_output}, got {calculate(job)}"
# Paused job should return False
job.pause()
assert not calculate(job), f"Expected None, got {calculate(job)}"
scheduler.shutdown()

@ -1,111 +0,0 @@
from __future__ import annotations
import unittest
from unittest.mock import Mock
import discord
from apscheduler.triggers.interval import IntervalTrigger
from discord_reminder_bot.ui import create_job_embed
class TestCreateJobEmbed(unittest.TestCase):
"""Test the `create_job_embed` function in the `discord_reminder_bot.ui` module."""
def setUp(self) -> None:
"""Set up the mock job for testing."""
self.job = Mock()
self.job.id = "12345"
self.job.kwargs = {"channel_id": 67890, "message": "Test message", "author_id": 54321}
self.job.next_run_time = None
self.job.trigger = Mock(spec=IntervalTrigger)
self.job.trigger.interval = "1 day"
def test_create_job_embed_with_next_run_time(self) -> None:
"""Test the `create_job_embed` function to ensure it correctly creates a Discord embed for a job with the next run time."""
self.job.next_run_time = Mock()
self.job.next_run_time.strftime.return_value = "2023-10-10 10:00:00"
embed: discord.Embed = create_job_embed(self.job)
assert_msg: str = f"Expected discord.Embed, got {type(embed)}"
assert isinstance(embed, discord.Embed), assert_msg
assert_msg = f"Expected Test message, got {embed.title}"
assert embed.title == "Test message", assert_msg
assert_msg = "Expected embed description to not be None"
assert embed.description is not None, assert_msg
assert_msg = f"Expected 12345 in embed footer, got {embed.footer}"
assert "12345" in embed.footer.text if embed.footer.text else None, assert_msg
assert_msg = f"Expected Next run: 2023-10-10 10:00:00 in embed description, got {embed.description}"
assert "Next run: 2023-10-10 10:00:00" in embed.description, assert_msg
assert_msg = f"Expected Interval: 1 day in embed description, got {embed.description}"
assert "Interval: 1 day" in embed.description, assert_msg
assert_msg = f"Expected Channel: <#67890> in embed description, got {embed.description}"
assert "Channel: <#67890>" in embed.description, assert_msg
assert_msg = f"Expected Created by: <@54321> in embed description, got {embed.description}"
assert "Created by: <@54321>" in embed.description, assert_msg
def test_create_job_embed_without_next_run_time(self) -> None:
"""Test the `create_job_embed` function to ensure it correctly creates a Discord embed for a job without the next run time."""
embed: discord.Embed = create_job_embed(self.job)
assert_msg: str = f"Expected discord.Embed, got {type(embed)}"
assert isinstance(embed, discord.Embed), assert_msg
assert_msg = f"Expected Test message, got {embed.title}"
assert embed.title == "Test message", assert_msg
assert_msg = "Expected embed description to not be None"
assert embed.description is not None, assert_msg
assert_msg = f"Expected 12345 in embed footer, got {embed.footer}"
assert "12345" in embed.footer.text if embed.footer.text else None, assert_msg
assert_msg = f"Expected Paused in embed description, got {embed.description}"
assert "Paused" in embed.description, assert_msg
assert_msg = f"Expected Interval: 1 day in embed description, got {embed.description}"
assert "Interval: 1 day" in embed.description, assert_msg
assert_msg = f"Expected Channel: <#67890> in embed description, got {embed.description}"
assert "Channel: <#67890>" in embed.description, assert_msg
assert_msg = f"Expected Created by: <@54321> in embed description, got {embed.description}"
assert "Created by: <@54321>" in embed.description, assert_msg
def test_create_job_embed_with_long_message(self) -> None:
"""Test the `create_job_embed` function to ensure it correctly truncates long messages."""
self.job.kwargs["message"] = "A" * 300
embed: discord.Embed = create_job_embed(self.job)
assert_msg: str = f"Expected A{'...' * 84} in embed title, got {embed.title}"
assert isinstance(embed, discord.Embed), assert_msg
assert_msg = f"Expected A{'...' * 84} in embed title, got {embed.title}"
assert embed.title == "A" * 256 + "...", assert_msg
assert_msg = "Expected embed description to not be None"
assert embed.description is not None, assert_msg
assert_msg = f"Expected 12345 in embed footer, got {embed.footer}"
assert "12345" in embed.footer.text if embed.footer.text else None, assert_msg
assert_msg = f"Expected Paused in embed description, got {embed.description}"
assert "Paused" in embed.description, assert_msg
assert_msg = f"Expected Interval: 1 day in embed description, got {embed.description}"
assert "Interval: 1 day" in embed.description, assert_msg
assert_msg = f"Expected Channel: <#67890> in embed description, got {embed.description}"
assert "Channel: <#67890>" in embed.description, assert_msg
assert_msg = f"Expected Created by: <@54321> in embed description, got {embed.description}"
assert "Created by: <@54321>" in embed.description, assert_msg