diff --git a/didier/cogs/school.py b/didier/cogs/school.py index f8a0d1b..5aca554 100644 --- a/didier/cogs/school.py +++ b/didier/cogs/school.py @@ -44,6 +44,9 @@ class School(commands.Cog): Menus are Dutch, as a lot of dishes have very weird translations """ + if day_dt is None: + day_dt = date.today() + async with ctx.typing(): try: menu = await fetch_menu(self.client.http_session, day_dt) diff --git a/didier/data/embeds/google/google_search.py b/didier/data/embeds/google/google_search.py index dd0383b..f2a58b1 100644 --- a/didier/data/embeds/google/google_search.py +++ b/didier/data/embeds/google/google_search.py @@ -5,6 +5,7 @@ from overrides import overrides from didier.data.embeds.base import EmbedBaseModel from didier.data.scrapers.google import SearchData +from didier.utils.discord.colours import google_blue __all__ = ["GoogleSearch"] @@ -36,7 +37,7 @@ class GoogleSearch(EmbedBaseModel): if not self.data.results or self.data.status_code != HTTPStatus.OK: return self._error_embed() - embed = discord.Embed(title="Google Search", colour=discord.Colour.blue()) + embed = discord.Embed(title="Google Search", colour=google_blue()) embed.set_footer(text=self.data.result_stats or None) # Add all results into the description diff --git a/didier/data/embeds/hydra/menu.py b/didier/data/embeds/hydra/menu.py index 8d1e8d4..9212d70 100644 --- a/didier/data/embeds/hydra/menu.py +++ b/didier/data/embeds/hydra/menu.py @@ -94,12 +94,17 @@ class Menu(EmbedPydantic): return embed def _regular_embed(self, embed: discord.Embed) -> discord.Embed: - embed.add_field(name="🥣 Soep", value=self._get_soups(), inline=False) - embed.add_field(name="🍴 Hoofdgerechten", value=self._get_main_courses(), inline=False) - embed.add_field(name="❄️Koud", value=self._get_cold_meals(), inline=False) + if soups := self._get_soups(): + embed.add_field(name="🥣 Soep", value=soups, inline=False) - vegetables = "\n".join(list(sorted(self.vegetables))) - embed.add_field(name="🥦 Groenten", value=vegetables, inline=False) + if mains := self._get_main_courses(): + embed.add_field(name="🍴 Hoofdgerechten", value=mains, inline=False) + + if cold := self._get_cold_meals(): + embed.add_field(name="❄️Koud", value=cold, inline=False) + + if vegetables := "\n".join(list(sorted(self.vegetables))): + embed.add_field(name="🥦 Groenten", value=vegetables, inline=False) return embed diff --git a/didier/utils/discord/colours.py b/didier/utils/discord/colours.py index 7c26769..5e69d3e 100644 --- a/didier/utils/discord/colours.py +++ b/didier/utils/discord/colours.py @@ -1,6 +1,6 @@ import discord -__all__ = ["ghent_university_blue", "ghent_university_yellow", "urban_dictionary_green"] +__all__ = ["ghent_university_blue", "ghent_university_yellow", "google_blue", "urban_dictionary_green"] def ghent_university_blue() -> discord.Colour: @@ -11,5 +11,9 @@ def ghent_university_yellow() -> discord.Colour: return discord.Colour.from_rgb(255, 210, 0) +def google_blue() -> discord.Colour: + return discord.Colour.from_rgb(66, 133, 244) + + def urban_dictionary_green() -> discord.Colour: return discord.Colour.from_rgb(220, 255, 0) diff --git a/didier/utils/discord/converters/time.py b/didier/utils/discord/converters/time.py index 6fbad9a..2a328f9 100644 --- a/didier/utils/discord/converters/time.py +++ b/didier/utils/discord/converters/time.py @@ -5,7 +5,7 @@ from typing import Optional, Union import discord from discord import app_commands -from discord.ext.commands import ArgumentParsingError +from discord.ext import commands from overrides import overrides from didier.utils.discord.autocompletion.time import autocomplete_day @@ -15,7 +15,7 @@ from didier.utils.types.datetime import ( str_to_weekday, ) -__all__ = ["date_converter"] +__all__ = ["date_converter", "DateTransformer"] def date_converter(argument: Optional[str]) -> date: @@ -50,12 +50,13 @@ def date_converter(argument: Optional[str]) -> date: return parse_dm_string(argument) # Unparseable - raise ArgumentParsingError(f"Unable to interpret `{original_argument}` as a date.") + raise commands.ArgumentParsingError(f"Unable to interpret `{original_argument}` as a date.") class DateTransformer(app_commands.Transformer): """Application commands transformer for dates""" + @overrides async def autocomplete( self, interaction: discord.Interaction, value: Union[int, float, str] ) -> list[app_commands.Choice[Union[int, float, str]]]: diff --git a/didier/utils/types/datetime.py b/didier/utils/types/datetime.py index 07ad29a..7372118 100644 --- a/didier/utils/types/datetime.py +++ b/didier/utils/types/datetime.py @@ -25,7 +25,7 @@ def forward_to_next_weekday(day_dt: DateType, target_weekday: int, *, allow_toda raise ValueError # Skip at least one day - if not allow_today: + if not allow_today and day_dt.weekday() == target_weekday: day_dt += datetime.timedelta(days=1) while day_dt.weekday() != target_weekday: @@ -69,16 +69,16 @@ def parse_dm_string(argument: str) -> datetime.date: raise ValueError # Day Month - match = re.search(r"\d+", spl[0]).group() + match = re.search(r"\d+", spl[0]) if match is not None: - day = int(match) + day = int(match.group()) month = str_to_month(spl[1]) return datetime.date(day=day, month=month, year=today.year) # Month Day - match = re.search(r"\d+", spl[0]).group() + match = re.search(r"\d+", spl[1]) if match is not None: - day = int(match) + day = int(match.group()) month = str_to_month(spl[0]) return datetime.date(day=day, month=month, year=today.year) diff --git a/readme.md b/readme.md index e2340c6..0fad8b6 100644 --- a/readme.md +++ b/readme.md @@ -38,6 +38,8 @@ docker compose -f docker-compose.test.yml up -d ### Commands +_All of these are Python tools. Depending on your OS and configuration, you may have to prefix them with `python3 -m`._ + ```shell # Starting Didier python3 main.py diff --git a/requirements.txt b/requirements.txt index d72584a..b064107 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ aiohttp==3.8.1 alembic==1.8.0 asyncpg==0.25.0 beautifulsoup4==4.11.1 -discord.py==2.0.0 +discord.py==2.0.1 environs==9.5.0 feedparser==6.0.10 markdownify==0.11.2 diff --git a/tests/test_didier/test_utils/test_discord/test_converters/test_time.py b/tests/test_didier/test_utils/test_discord/test_converters/test_time.py new file mode 100644 index 0000000..8407ea4 --- /dev/null +++ b/tests/test_didier/test_utils/test_discord/test_converters/test_time.py @@ -0,0 +1,131 @@ +from datetime import date + +from freezegun import freeze_time + +from didier.utils.discord.converters.time import date_converter + + +@freeze_time("2022-08-20") +def test_date_converter_empty_returns_today(): + """Test that the date converter returns today by default""" + result = date_converter(None) + assert result == date.today() + + result = date_converter("") + assert result == date.today() + + +@freeze_time("2022-08-20") +def test_date_converter_keywords_tomorrow(): + """Test that the date converter works correctly for +1-offset keywords""" + result = date_converter("tomorrow") + assert (result.day, result.month, result.year) == (21, 8, 2022) + + result = date_converter("tmrw") + assert (result.day, result.month, result.year) == (21, 8, 2022) + + result = date_converter("morgen") + assert (result.day, result.month, result.year) == (21, 8, 2022) + + +@freeze_time("2022-08-20") +def test_date_converter_keywords_two_days(): + """Test that the date converter works correctly for +2-offset keywords""" + result = date_converter("overmorgen") + assert (result.day, result.month, result.year) == (22, 8, 2022) + + +@freeze_time("2022-08-20") # This is a Saturday +def test_date_converter_weekdays_english(): + """Test that the date converter works correctly for weekdays (English version)""" + # Full + result = date_converter("monday") + assert (result.day, result.month, result.year) == (22, 8, 2022) + + result = date_converter("tuesday") + assert (result.day, result.month, result.year) == (23, 8, 2022) + + result = date_converter("wednesday") + assert (result.day, result.month, result.year) == (24, 8, 2022) + + result = date_converter("thursday") + assert (result.day, result.month, result.year) == (25, 8, 2022) + + result = date_converter("friday") + assert (result.day, result.month, result.year) == (26, 8, 2022) + + result = date_converter("saturday") + assert (result.day, result.month, result.year) == (27, 8, 2022) + + result = date_converter("sunday") + assert (result.day, result.month, result.year) == (21, 8, 2022) + + # Abbreviated + result = date_converter("mon") + assert (result.day, result.month, result.year) == (22, 8, 2022) + + result = date_converter("tue") + assert (result.day, result.month, result.year) == (23, 8, 2022) + + result = date_converter("wed") + assert (result.day, result.month, result.year) == (24, 8, 2022) + + result = date_converter("thu") + assert (result.day, result.month, result.year) == (25, 8, 2022) + + result = date_converter("fri") + assert (result.day, result.month, result.year) == (26, 8, 2022) + + result = date_converter("sat") + assert (result.day, result.month, result.year) == (27, 8, 2022) + + result = date_converter("sun") + assert (result.day, result.month, result.year) == (21, 8, 2022) + + +@freeze_time("2022-08-20") # This is a Saturday +def test_date_converter_weekdays_dutch(): + """Test that the date converter works correctly for weekdays (Dutch version)""" + # Full + result = date_converter("maandag") + assert (result.day, result.month, result.year) == (22, 8, 2022) + + result = date_converter("dinsdag") + assert (result.day, result.month, result.year) == (23, 8, 2022) + + result = date_converter("woensdag") + assert (result.day, result.month, result.year) == (24, 8, 2022) + + result = date_converter("donderdag") + assert (result.day, result.month, result.year) == (25, 8, 2022) + + result = date_converter("vrijdag") + assert (result.day, result.month, result.year) == (26, 8, 2022) + + result = date_converter("zaterdag") + assert (result.day, result.month, result.year) == (27, 8, 2022) + + result = date_converter("zondag") + assert (result.day, result.month, result.year) == (21, 8, 2022) + + # Abbreviated + result = date_converter("ma") + assert (result.day, result.month, result.year) == (22, 8, 2022) + + result = date_converter("di") + assert (result.day, result.month, result.year) == (23, 8, 2022) + + result = date_converter("woe") + assert (result.day, result.month, result.year) == (24, 8, 2022) + + result = date_converter("do") + assert (result.day, result.month, result.year) == (25, 8, 2022) + + result = date_converter("vrij") + assert (result.day, result.month, result.year) == (26, 8, 2022) + + result = date_converter("za") + assert (result.day, result.month, result.year) == (27, 8, 2022) + + result = date_converter("zo") + assert (result.day, result.month, result.year) == (21, 8, 2022) diff --git a/tests/test_didier/test_utils/test_types/test_datetime.py b/tests/test_didier/test_utils/test_types/test_datetime.py index ecb1973..289285b 100644 --- a/tests/test_didier/test_utils/test_types/test_datetime.py +++ b/tests/test_didier/test_utils/test_types/test_datetime.py @@ -1,8 +1,55 @@ import datetime import pytest +from freezegun import freeze_time -from didier.utils.types.datetime import str_to_date +from didier.utils.types.datetime import parse_dm_string, str_to_date + + +@freeze_time("2022-08-20") +def test_parse_dm_string_ddmm(): + """Test parsing DD/MM""" + result = parse_dm_string("23/08") + assert (result.day, result.month, result.year) == (23, 8, 2022) + + result = parse_dm_string("8/9") + assert (result.day, result.month, result.year) == (8, 9, 2022) + + +def test_parse_dm_string_dm_too_long_raises(): + """Test parsing DD/MM format when something longer is passed in""" + with pytest.raises(ValueError): + parse_dm_string("23/08/2022") + + +def test_parse_dm_string_dm_garbage(): + """Test parsing DD/MM format when something invalid is passed in""" + with pytest.raises(ValueError): + parse_dm_string("AC/DC") + + +def test_parse_dm_string_semantic(): + """Test parsing date strings in the [DAY] [MONTH] and [MONTH] [DAY] formats""" + result = parse_dm_string("23rd november") + assert (result.day, result.month, result.year) == (23, 11, 2022) + + result = parse_dm_string("23 nov") + assert (result.day, result.month, result.year) == (23, 11, 2022) + + result = parse_dm_string("23ste november") + assert (result.day, result.month, result.year) == (23, 11, 2022) + + result = parse_dm_string("november 23rd") + assert (result.day, result.month, result.year) == (23, 11, 2022) + + result = parse_dm_string("nov 23") + assert (result.day, result.month, result.year) == (23, 11, 2022) + + +def test_parse_dm_string_unparseable_raises(): + """Test that any other input raises an error""" + with pytest.raises(ValueError): + parse_dm_string("WhateverThisMayBe") def test_str_to_date_single_valid():