Move settings to a function and add tests
This commit is contained in:
77
tests/test_misc.py
Normal file
77
tests/test_misc.py
Normal file
@ -0,0 +1,77 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.date import DateTrigger
|
||||
|
||||
from discord_reminder_bot.misc import calc_time, calculate, get_human_time
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from apscheduler.job import Job
|
||||
|
||||
|
||||
def test_calc_time() -> None:
|
||||
"""Test the calc_time function with various datetime inputs."""
|
||||
test_datetime: datetime = datetime(2023, 10, 1, 12, 0, 0, tzinfo=UTC)
|
||||
expected_timestamp: str = f"<t:{int(test_datetime.timestamp())}:R>"
|
||||
assert calc_time(test_datetime) == expected_timestamp
|
||||
|
||||
now: datetime = datetime.now(tz=UTC)
|
||||
expected_timestamp_now: str = f"<t:{int(now.timestamp())}:R>"
|
||||
assert calc_time(now) == expected_timestamp_now
|
||||
|
||||
past_datetime: datetime = datetime(2000, 1, 1, 0, 0, 0, tzinfo=UTC)
|
||||
expected_timestamp_past: str = f"<t:{int(past_datetime.timestamp())}:R>"
|
||||
assert calc_time(past_datetime) == expected_timestamp_past
|
||||
|
||||
future_datetime: datetime = datetime(2100, 1, 1, 0, 0, 0, tzinfo=UTC)
|
||||
expected_timestamp_future: str = f"<t:{int(future_datetime.timestamp())}:R>"
|
||||
assert calc_time(future_datetime) == expected_timestamp_future
|
||||
|
||||
|
||||
def test_get_human_time() -> None:
|
||||
"""Test the get_human_time function with various timedelta inputs."""
|
||||
test_timedelta = timedelta(days=1, hours=2, minutes=3, seconds=4)
|
||||
expected_output = "1d2h3m4s"
|
||||
assert get_human_time(test_timedelta) == expected_output
|
||||
|
||||
test_timedelta = timedelta(hours=5, minutes=6, seconds=7)
|
||||
expected_output = "5h6m7s"
|
||||
assert get_human_time(test_timedelta) == expected_output
|
||||
|
||||
test_timedelta = timedelta(minutes=8, seconds=9)
|
||||
expected_output = "8m9s"
|
||||
assert get_human_time(test_timedelta) == expected_output
|
||||
|
||||
test_timedelta = timedelta(seconds=10)
|
||||
expected_output = "10s"
|
||||
assert get_human_time(test_timedelta) == expected_output
|
||||
|
||||
test_timedelta = timedelta(days=0, hours=0, minutes=0, seconds=0)
|
||||
expected_output = ""
|
||||
assert get_human_time(test_timedelta) == expected_output
|
||||
|
||||
|
||||
def test_calculate() -> None:
|
||||
"""Test the calculate function with various job inputs."""
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.start()
|
||||
|
||||
# Create a job with a DateTrigger
|
||||
run_date = datetime(2270, 10, 1, 12, 0, 0, tzinfo=UTC)
|
||||
job: Job = scheduler.add_job(lambda: None, trigger=DateTrigger(run_date=run_date), id="test_job", name="Test Job")
|
||||
|
||||
expected_output = "<t:9490737600:R>"
|
||||
assert calculate(job) == expected_output
|
||||
|
||||
# Modify the job to have a next_run_time
|
||||
job.modify(next_run_time=run_date)
|
||||
assert calculate(job) == expected_output
|
||||
|
||||
# Paused job should still return the same output
|
||||
job.pause()
|
||||
assert calculate(job) == expected_output
|
||||
|
||||
scheduler.shutdown()
|
53
tests/test_parser.py
Normal file
53
tests/test_parser.py
Normal file
@ -0,0 +1,53 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from freezegun import freeze_time
|
||||
|
||||
from discord_reminder_bot import settings
|
||||
from discord_reminder_bot.parser import parse_time
|
||||
|
||||
|
||||
def test_parse_time_valid_date() -> None:
|
||||
"""Test the `parse_time` function with a valid date string."""
|
||||
date_to_parse = "tomorrow at 5pm"
|
||||
timezone = "UTC"
|
||||
result: datetime.datetime | None = parse_time(date_to_parse, timezone, use_dotenv=False)
|
||||
assert result is not None
|
||||
assert result.tzinfo == ZoneInfo(timezone)
|
||||
|
||||
|
||||
def test_parse_time_no_date() -> None:
|
||||
"""Test the `parse_time` function with no date string."""
|
||||
date_to_parse: str = ""
|
||||
timezone = "UTC"
|
||||
result: datetime.datetime | None = parse_time(date_to_parse, timezone, use_dotenv=False)
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_parse_time_no_timezone() -> None:
|
||||
"""Test the `parse_time` function with no timezone."""
|
||||
date_to_parse = "tomorrow at 5pm"
|
||||
result: datetime.datetime | None = parse_time(date_to_parse, use_dotenv=False)
|
||||
assert result is not None
|
||||
assert result.tzinfo == ZoneInfo(settings.get_timezone(use_dotenv=False))
|
||||
|
||||
|
||||
def test_parse_time_invalid_date() -> None:
|
||||
"""Test the `parse_time` function with an invalid date string."""
|
||||
date_to_parse = "invalid date"
|
||||
timezone = "UTC"
|
||||
result: datetime.datetime | None = parse_time(date_to_parse, timezone, use_dotenv=False)
|
||||
assert result is None
|
||||
|
||||
|
||||
@freeze_time("2023-01-01 12:00:00")
|
||||
def test_parse_time_invalid_timezone() -> None:
|
||||
"""Test the `parse_time` function with an invalid timezone."""
|
||||
date_to_parse = "tomorrow at 5pm"
|
||||
timezone = "Invalid/Timezone"
|
||||
result: datetime.datetime | None = parse_time(date_to_parse, timezone, use_dotenv=False)
|
||||
assert result is not None
|
||||
assert result.tzinfo == ZoneInfo("UTC")
|
||||
assert result == datetime.datetime(2023, 1, 2, 17, 0, tzinfo=ZoneInfo("UTC"))
|
53
tests/test_settings.py
Normal file
53
tests/test_settings.py
Normal file
@ -0,0 +1,53 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
|
||||
from discord_reminder_bot.settings import get_settings
|
||||
|
||||
|
||||
def test_get_settings(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Test get_settings function with environment variables."""
|
||||
monkeypatch.setenv("SQLITE_LOCATION", "/test_jobs.sqlite")
|
||||
monkeypatch.setenv("TIMEZONE", "UTC")
|
||||
monkeypatch.setenv("BOT_TOKEN", "test_token")
|
||||
monkeypatch.setenv("LOG_LEVEL", "DEBUG")
|
||||
monkeypatch.setenv("WEBHOOK_URL", "http://test_webhook_url")
|
||||
|
||||
settings: dict[str, str | dict[str, SQLAlchemyJobStore] | dict[str, bool] | AsyncIOScheduler] = get_settings(use_dotenv=False)
|
||||
|
||||
assert settings["sqlite_location"] == "/test_jobs.sqlite"
|
||||
assert settings["config_timezone"] == "UTC"
|
||||
assert settings["bot_token"] == "test_token" # noqa: S105
|
||||
assert settings["log_level"] == "DEBUG"
|
||||
assert settings["webhook_url"] == "http://test_webhook_url"
|
||||
assert isinstance(settings["jobstores"]["default"], SQLAlchemyJobStore) # type: ignore # noqa: PGH003
|
||||
|
||||
|
||||
def test_get_settings_missing_bot_token(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Test get_settings function with missing bot token."""
|
||||
monkeypatch.delenv("BOT_TOKEN", raising=False)
|
||||
|
||||
with pytest.raises(ValueError, match="Missing bot token"):
|
||||
get_settings(use_dotenv=False)
|
||||
|
||||
|
||||
def test_get_settings_default_values(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Test get_settings function with default values."""
|
||||
monkeypatch.delenv("SQLITE_LOCATION", raising=False)
|
||||
monkeypatch.delenv("TIMEZONE", raising=False)
|
||||
monkeypatch.delenv("BOT_TOKEN", raising=False)
|
||||
monkeypatch.delenv("LOG_LEVEL", raising=False)
|
||||
monkeypatch.delenv("WEBHOOK_URL", raising=False)
|
||||
monkeypatch.setenv("BOT_TOKEN", "default_token")
|
||||
|
||||
settings: dict[str, str | dict[str, SQLAlchemyJobStore] | dict[str, bool] | AsyncIOScheduler] = get_settings(use_dotenv=False)
|
||||
|
||||
assert settings["sqlite_location"] == "/jobs.sqlite"
|
||||
assert settings["config_timezone"] == "UTC"
|
||||
assert settings["bot_token"] == "default_token" # noqa: S105
|
||||
assert settings["log_level"] == "INFO"
|
||||
assert not settings["webhook_url"]
|
||||
assert isinstance(settings["jobstores"]["default"], SQLAlchemyJobStore) # type: ignore # noqa: PGH003
|
||||
assert isinstance(settings["scheduler"], AsyncIOScheduler)
|
70
tests/test_ui.py
Normal file
70
tests/test_ui.py
Normal file
@ -0,0 +1,70 @@
|
||||
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 isinstance(embed, discord.Embed)
|
||||
assert embed.title == "Test message"
|
||||
assert embed.description is not None
|
||||
assert "ID: 12345" in embed.description
|
||||
assert "Next run: 2023-10-10 10:00:00" in embed.description
|
||||
assert "Interval: 1 day" in embed.description
|
||||
assert "Channel: <#67890>" in embed.description
|
||||
assert "Author: <@54321>" in embed.description
|
||||
|
||||
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 isinstance(embed, discord.Embed)
|
||||
assert embed.title == "Test message"
|
||||
assert embed.description is not None
|
||||
assert "ID: 12345" in embed.description
|
||||
assert "Paused" in embed.description
|
||||
assert "Interval: 1 day" in embed.description
|
||||
assert "Channel: <#67890>" in embed.description
|
||||
assert "Author: <@54321>" in embed.description
|
||||
|
||||
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 isinstance(embed, discord.Embed)
|
||||
assert embed.title == "A" * 256 + "..."
|
||||
assert embed.description is not None
|
||||
assert "ID: 12345" in embed.description
|
||||
assert "Paused" in embed.description
|
||||
assert "Interval: 1 day" in embed.description
|
||||
assert "Channel: <#67890>" in embed.description
|
||||
assert "Author: <@54321>" in embed.description
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user