Compare commits

...

7 Commits

Author SHA1 Message Date
stijndcl 1752d651a9 Undo last change 2022-08-29 02:26:16 +02:00
stijndcl 994ff01de1 Allow specifying return type for http methods 2022-08-29 02:04:42 +02:00
stijndcl 14e0472954 Make custom colour for Google embeds 2022-08-29 02:01:05 +02:00
stijndcl ea6b204cf0 Add tests & fixes for date parsing 2022-08-29 01:51:59 +02:00
stijndcl b85f5a612a Catch missing fields in menu 2022-08-29 01:30:58 +02:00
stijndcl 9225f61e47 Fix typing 2022-08-29 01:22:05 +02:00
stijndcl c31767c25f Add OS info to dev readme 2022-08-29 01:17:06 +02:00
10 changed files with 211 additions and 17 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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]]]:

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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():