aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/converters.py9
-rw-r--r--tests/bot/test_converters.py60
2 files changed, 64 insertions, 5 deletions
diff --git a/bot/converters.py b/bot/converters.py
index d8df44534..05b7d55d9 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -583,8 +583,9 @@ class DayDuration(Converter):
The following formats are accepted:
- H:M
- - H:M am/pm
- - H am/pm
+ - H:Mam/pm
+ - HMam/pm
+ - Ham/pm
- H
where `H` represents Hours and `M` represents Minutes.
@@ -598,11 +599,11 @@ class DayDuration(Converter):
async def convert(self, _ctx: Context, argument: str) -> datetime:
"""Attempts to converting `argument` to an UTC datetime object."""
- match = self.TIME_RE.fullmatch(argument).groups()
+ match = self.TIME_RE.fullmatch(argument)
if not match:
raise BadArgument(f"`{argument}` is not a valid time duration string.")
- hour_12, minute_12, meridiem, hour_24, minute_24 = match
+ hour_12, minute_12, meridiem, hour_24, minute_24 = match.groups()
time = None
if hour_12 and meridiem and minute_12:
diff --git a/tests/bot/test_converters.py b/tests/bot/test_converters.py
index 1bb678db2..880f86e28 100644
--- a/tests/bot/test_converters.py
+++ b/tests/bot/test_converters.py
@@ -6,7 +6,7 @@ from unittest.mock import MagicMock, patch
from dateutil.relativedelta import relativedelta
from discord.ext.commands import BadArgument
-from bot.converters import Duration, HushDurationConverter, ISODateTime, PackageName
+from bot.converters import DayDuration, Duration, HushDurationConverter, ISODateTime, PackageName
class ConverterTests(unittest.IsolatedAsyncioTestCase):
@@ -252,3 +252,61 @@ class ConverterTests(unittest.IsolatedAsyncioTestCase):
with self.subTest(invalid_minutes_string=invalid_minutes_string, exception_message=exception_message):
with self.assertRaisesRegex(BadArgument, re.escape(exception_message)):
await converter.convert(self.context, invalid_minutes_string)
+
+ async def test_day_duration_convertor_for_valid(self):
+ """DayDuration converter returns correct datetime for valid datetime string."""
+ test_values = (
+ # H:M am/pm
+ ("2:14 pm", 51240),
+ ("2:14 am", 8040),
+ ("2:14Pm", 51240),
+ ("2:14AM", 8040),
+
+ # HM am/pm
+ ("942pm", 78120),
+ ("854 am", 32040),
+
+ # H am/pm
+ ("11pm", 82800),
+ ("2 am", 7200),
+
+ # H:M
+ ("2:14", 8040),
+ ("23:05", 83100),
+ ("2305", 83100),
+
+ # H
+ ("5", 18000),
+ ("18", 64800),
+ )
+ converter = DayDuration()
+ for day_duration_string, expected_1970 in test_values:
+ with self.subTest(day_duration_string=day_duration_string, expected_1970_dt=expected_1970):
+ converted = await converter.convert(self.context, day_duration_string)
+
+ expected_1970_dt = datetime.utcfromtimestamp(expected_1970)
+ today = datetime.utcnow().date()
+ expected_now = expected_1970_dt.replace(
+ year=today.year,
+ month=today.month,
+ day=today.day
+ )
+
+ self.assertEqual(expected_now, converted)
+
+ async def test_day_duration_convertor_for_invalid(self):
+ """DayDuration converter raises the correct exception for invalid datetime strings."""
+ test_values = (
+ # Check if it fails when providing the date part
+ '2019-11-12 09:15',
+
+ # Other non-valid strings
+ 'fisk the tag master',
+ )
+
+ converter = DayDuration()
+ for datetime_string in test_values:
+ with self.subTest(datetime_string=datetime_string):
+ exception_message = f"`{datetime_string}` is not a valid time duration string."
+ with self.assertRaisesRegex(BadArgument, re.escape(exception_message)):
+ await converter.convert(self.context, datetime_string)