diff --git a/dakiny_bot/classes/election.py b/dakiny_bot/classes/election.py index 30d50dd..d8ad023 100644 --- a/dakiny_bot/classes/election.py +++ b/dakiny_bot/classes/election.py @@ -16,13 +16,13 @@ from dakiny_bot.database.sql_data_classes import ( Votes, engine, ) +from dakiny_bot.election_utils import get_datetime from dakiny_bot.utils import ( TEMPLATES_DIR, VoteType, build_highcouncil_votes_str, build_votes_str, edit_clan_council_info, - get_datetime, get_election_name, ) from sqlalchemy import select, true, update diff --git a/dakiny_bot/election_utils.py b/dakiny_bot/election_utils.py new file mode 100644 index 0000000..be18a7c --- /dev/null +++ b/dakiny_bot/election_utils.py @@ -0,0 +1,29 @@ +"""Broken out util functions to avoid importing sql during testing.""" + +import calendar +from datetime import UTC, datetime + + +def get_datetime(month_day: str) -> datetime: + """ + Takes in a string month and day and returns a datetime object of that Month/Day this year. + + Args: + month_day (str): Month/Day in that format. + + Returns: + datetime: Datetime representation of Month/Day. + """ + today = datetime.now(tz=UTC).date() + year = today.year + month = int(month_day.split("/")[0]) + day = int(month_day.split("/")[1]) + + # ensure that year rolls over if current month is December and the next month is less than 12. + if today.month == calendar.DECEMBER and month < calendar.OCTOBER: + year += 1 + + if today.month == calendar.JANUARY and month > calendar.MARCH: + year -= 1 + + return datetime(year, month, day, tzinfo=UTC) diff --git a/dakiny_bot/tests/utils/test_get_datetime.py b/dakiny_bot/tests/utils/test_get_datetime.py index f42c506..f71c342 100644 --- a/dakiny_bot/tests/utils/test_get_datetime.py +++ b/dakiny_bot/tests/utils/test_get_datetime.py @@ -4,10 +4,38 @@ import datetime from freezegun import freeze_time -from dakiny_bot.utils import get_datetime +from dakiny_bot.election_utils import get_datetime @freeze_time("2025-6-16") def test_regular_get_datetime() -> None: - date_time = get_datetime("6/16") - assert date_time == datetime.datetime(2025, 6, 16) + """ + Test a regular get_datetime. + + Expect the returned datetime to be the frozen date. + Have two different days to ensure that the date is being returned correctly. + """ + date_time = get_datetime("6/15") + assert date_time == datetime.datetime(2025, 6, 15, tzinfo=datetime.UTC) + + +@freeze_time("2025-1-16") +def test_jan_get_datetime() -> None: + """ + Test previous year get_datetime. + + Frozen day is in January 2025, datetime string is in December. Expect datetime to be from 2024. + """ + date_time = get_datetime("12/15") + assert date_time == datetime.datetime(2024, 12, 15, tzinfo=datetime.UTC) + + +@freeze_time("2025-12-16") +def test_dec_get_datetime() -> None: + """ + Test next year get_datetime. + + Frozen day is in December 2025, datetime string is in January. Expect datetime to be from 2026. + """ + date_time = get_datetime("1/15") + assert date_time == datetime.datetime(2026, 1, 15, tzinfo=datetime.UTC) diff --git a/dakiny_bot/utils.py b/dakiny_bot/utils.py index 69eb1f2..9c363bf 100644 --- a/dakiny_bot/utils.py +++ b/dakiny_bot/utils.py @@ -1,6 +1,5 @@ """File contains util functions.""" -import calendar import os from collections.abc import Sequence from datetime import UTC, datetime @@ -22,6 +21,7 @@ from dakiny_bot.database.sql_data_classes import ( ) from .dakiny_settings import settings +from .election_utils import get_datetime NOMINEE_SPLITTER = "========================================\n\n" REPO_DIR = "./dakiny-messages" @@ -34,31 +34,6 @@ VoteType = StrEnum("VoteType", ["COUNCIL", "DISCORDMANAGER", "CLANLEADER"]) PointType = StrEnum("PointType", ["CLAN", "EVENT"]) -def get_datetime(month_day: str) -> datetime: - """ - Takes in a string month and day and returns a datetime object of that Month/Day this year. - - Args: - month_day (str): Month/Day in that format. - - Returns: - datetime: Datetime representation of Month/Day. - """ - today = datetime.now(tz=UTC).date() - year = today.year - month = int(month_day.split("/")[0]) - day = int(month_day.split("/")[1]) - - # ensure that year rolls over if current month is December and the next month is less than 12. - if today.month == calendar.DECEMBER and month < calendar.OCTOBER: - year += 1 - - if today.month == calendar.JANUARY and month > calendar.MARCH: - year -= 1 - - return datetime(year, month, day, tzinfo=UTC) - - def get_election_name() -> str: """ Builds the unique name of the current election cycle.