Compare commits

...
3 Commits
Author SHA1 Message Date
imcompiling 09958c31ad Save: adding all files to save progress.
build_develop / build (push) Successful in 38s
I don't exactly remember where I was on this, but I need to move on
and work on other things. Adding all files just to save my work.
2025-07-15 09:12:38 -04:00
imcompiling 0f3596692f Move get_quarter_cycle into election_utils.py. 2025-04-24 09:34:53 -04:00
imcompiling def9c4aa79 Move get_datetime into own file.
Add test cases to cover get_datetime usage and edge cases.
2025-04-24 09:27:05 -04:00
7 changed files with 122 additions and 52 deletions
+6
View File
@@ -1 +1,7 @@
"""Empty init file for dakiny_bot module.""" """Empty init file for dakiny_bot module."""
from dakiny_bot.dakiny_settings import CONFIG_LOCATION, DakinySettings, settings
if settings is None:
tmp = DakinySettings.from_yaml_file(CONFIG_LOCATION)
settings = tmp[0] if isinstance(tmp, list) else tmp
+1 -1
View File
@@ -16,13 +16,13 @@ from dakiny_bot.database.sql_data_classes import (
Votes, Votes,
engine, engine,
) )
from dakiny_bot.election_utils import get_datetime
from dakiny_bot.utils import ( from dakiny_bot.utils import (
TEMPLATES_DIR, TEMPLATES_DIR,
VoteType, VoteType,
build_highcouncil_votes_str, build_highcouncil_votes_str,
build_votes_str, build_votes_str,
edit_clan_council_info, edit_clan_council_info,
get_datetime,
get_election_name, get_election_name,
) )
from sqlalchemy import select, true, update from sqlalchemy import select, true, update
+1 -2
View File
@@ -83,5 +83,4 @@ class DakinySettings(YAMLWizard):
self.to_yaml_file(CONFIG_LOCATION) self.to_yaml_file(CONFIG_LOCATION)
tmp = DakinySettings.from_yaml_file(CONFIG_LOCATION) settings: DakinySettings | None = None
settings: DakinySettings = tmp[0] if isinstance(tmp, list) else tmp
+51
View File
@@ -0,0 +1,51 @@
"""Broken out util functions to avoid importing sql during testing."""
import calendar
from datetime import UTC, datetime
from dakiny_bot.dakiny_settings import settings
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_quarter_cycle() -> int | None:
"""
Returns the index for the current cycle/quarter. Defined as between term start and end.
Can return None if entire year isn't covered by cycles.
Returns:
int: cycle index for current cycle.
"""
today = datetime.now(tz=UTC)
for cycle, election in enumerate(settings.election_cycles):
if (
today.date() >= get_datetime(election.term_start).date()
and today.date() <= get_datetime(election.term_end).date()
):
return cycle
return None
+31 -3
View File
@@ -4,10 +4,38 @@ import datetime
from freezegun import freeze_time 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") @freeze_time("2025-6-16")
def test_regular_get_datetime() -> None: 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)
@@ -0,0 +1,31 @@
"""Test functions for get_quarter_cycle."""
from unittest.mock import mock_open, patch
from freezegun import freeze_time
from dakiny_bot.dakiny_settings import ChannelIds, DakinySettings, Election, Params, PointSettings, RoleIds
from dakiny_bot.election_utils import get_quarter_cycle
# I only care about term start and term end for this test
q1 = Election(True, "", "", "", "", "q1", "4/30", "2/1", "", "") # noqa: FBT003
q2 = Election(True, "", "", "", "", "q2", "7/31", "5/1", "", "") # noqa: FBT003
q3 = Election(True, "", "", "", "", "q3", "10/31", "8/1", "", "") # noqa: FBT003
q4 = Election(True, "", "", "", "", "q4", "1/31", "11/1", "", "") # noqa: FBT003
# Dummy all the values I don't care about.
settings = DakinySettings(
ChannelIds(1, 1, 1, 1, 1, 1),
[q1, q2, q3, q4],
Params("", "", 1),
PointSettings(1, 1, 1, 1, 1, True, True), # noqa: FBT003
RoleIds(1, 1, 1, 1, 1),
)
@patch("election_utils.get_quarter_cycle.settings", settings)
@patch("builtins.open", new_callable=mock_open)
class TestGetQuarterCycle:
@freeze_time("2025-2-25")
def test_regular_q1(self) -> None:
assert get_quarter_cycle() == 0
+1 -46
View File
@@ -1,6 +1,5 @@
"""File contains util functions.""" """File contains util functions."""
import calendar
import os import os
from collections.abc import Sequence from collections.abc import Sequence
from datetime import UTC, datetime from datetime import UTC, datetime
@@ -22,6 +21,7 @@ from dakiny_bot.database.sql_data_classes import (
) )
from .dakiny_settings import settings from .dakiny_settings import settings
from .election_utils import get_datetime, get_quarter_cycle
NOMINEE_SPLITTER = "========================================\n\n" NOMINEE_SPLITTER = "========================================\n\n"
REPO_DIR = "./dakiny-messages" REPO_DIR = "./dakiny-messages"
@@ -34,31 +34,6 @@ VoteType = StrEnum("VoteType", ["COUNCIL", "DISCORDMANAGER", "CLANLEADER"])
PointType = StrEnum("PointType", ["CLAN", "EVENT"]) 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: def get_election_name() -> str:
""" """
Builds the unique name of the current election cycle. Builds the unique name of the current election cycle.
@@ -110,26 +85,6 @@ def get_active_election_cycle() -> int | None:
return get_quarter_cycle() return get_quarter_cycle()
def get_quarter_cycle() -> int | None:
"""
Returns the index for the current cycle/quarter. Defined as between term start and end.
Can return None if entire year isn't covered by cycles.
Returns:
int: cycle index for current cycle.
"""
today = datetime.now(tz=UTC)
for cycle, election in enumerate(settings.election_cycles):
if (
today.date() >= get_datetime(election.term_start).date()
and today.date() <= get_datetime(election.term_end).date()
):
return cycle
return None
def build_votes_str(guild: nextcord.Guild) -> str: def build_votes_str(guild: nextcord.Guild) -> str:
""" """
Creates a string that contains all nominees and their votes to be sent as a discord message. Creates a string that contains all nominees and their votes to be sent as a discord message.