Add tests & fixes for date parsing

pull/128/head
stijndcl 2022-08-29 01:51:59 +02:00
parent b85f5a612a
commit ea6b204cf0
3 changed files with 181 additions and 3 deletions

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:
@ -76,7 +76,7 @@ def parse_dm_string(argument: str) -> datetime.date:
return datetime.date(day=day, month=month, year=today.year)
# Month Day
match = re.search(r"\d+", spl[0])
match = re.search(r"\d+", spl[1])
if match is not None:
day = int(match.group())
month = str_to_month(spl[0])

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