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()
|
Reference in New Issue
Block a user