diff options
| author | 2019-09-23 23:03:58 +0200 | |
|---|---|---|
| committer | 2019-09-23 23:03:58 +0200 | |
| commit | 56a365b24e7f6e145718a13edc410848ab169a06 (patch) | |
| tree | 7a9793987d2071015d881aad1ddea7d9c016067c /tests | |
| parent | Fix failing duration conversion (diff) | |
Allow whitespace in duration strings and update tests
https://github.com/python-discord/bot/issues/446
After review feedback and a discussion in the dev-core team, I've
changed a couple of things:
- Allow a space between amount and unit in the duration string;
- Allow a space between different units in the duration string;
- Remove the old ExpirationDate converter completely;
- Remove the dependency `dateparser` from the Pipfile;
- Update tests for the two types of optional spaces;
- Change the test for valid cases to a more readable format;
This PR closes #446
Diffstat (limited to '')
| -rw-r--r-- | tests/test_converters.py | 113 | 
1 files changed, 66 insertions, 47 deletions
| diff --git a/tests/test_converters.py b/tests/test_converters.py index 3cf00035f..35fc5d88e 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -3,11 +3,11 @@ import datetime  from unittest.mock import MagicMock, patch  import pytest +from dateutil.relativedelta import relativedelta  from discord.ext.commands import BadArgument  from bot.converters import (      Duration, -    ExpirationDate,      TagContentConverter,      TagNameConverter,      ValidPythonIdentifier, @@ -17,18 +17,6 @@ from bot.converters import (  @pytest.mark.parametrize(      ('value', 'expected'),      ( -        # sorry aliens -        ('2199-01-01T00:00:00', datetime.datetime(2199, 1, 1)), -    ) -) -def test_expiration_date_converter_for_valid(value: str, expected: datetime.datetime): -    converter = ExpirationDate() -    assert asyncio.run(converter.convert(None, value)) == expected - - -    ('value', 'expected'), -    (          ('hello', 'hello'),          ('  h ello  ', 'h ello')      ) @@ -97,46 +85,68 @@ def test_valid_python_identifier_for_invalid(value: str):  FIXED_UTC_NOW = datetime.datetime.fromisoformat('2019-01-01T00:00:00') -    ('duration', 'expected'), -    ( +    params=(          # Simple duration strings -        ('1Y', datetime.datetime.fromisoformat('2020-01-01T00:00:00')), -        ('1y', datetime.datetime.fromisoformat('2020-01-01T00:00:00')), -        ('1year', datetime.datetime.fromisoformat('2020-01-01T00:00:00')), -        ('1years', datetime.datetime.fromisoformat('2020-01-01T00:00:00')), -        ('1m', datetime.datetime.fromisoformat('2019-02-01T00:00:00')), -        ('1month', datetime.datetime.fromisoformat('2019-02-01T00:00:00')), -        ('1months', datetime.datetime.fromisoformat('2019-02-01T00:00:00')), -        ('1w', datetime.datetime.fromisoformat('2019-01-08T00:00:00')), -        ('1W', datetime.datetime.fromisoformat('2019-01-08T00:00:00')), -        ('1week', datetime.datetime.fromisoformat('2019-01-08T00:00:00')), -        ('1weeks', datetime.datetime.fromisoformat('2019-01-08T00:00:00')), -        ('1d', datetime.datetime.fromisoformat('2019-01-02T00:00:00')), -        ('1D', datetime.datetime.fromisoformat('2019-01-02T00:00:00')), -        ('1day', datetime.datetime.fromisoformat('2019-01-02T00:00:00')), -        ('1days', datetime.datetime.fromisoformat('2019-01-02T00:00:00')), -        ('1h', datetime.datetime.fromisoformat('2019-01-01T01:00:00')), -        ('1H', datetime.datetime.fromisoformat('2019-01-01T01:00:00')), -        ('1hour', datetime.datetime.fromisoformat('2019-01-01T01:00:00')), -        ('1hours', datetime.datetime.fromisoformat('2019-01-01T01:00:00')), -        ('1M', datetime.datetime.fromisoformat('2019-01-01T00:01:00')), -        ('1minute', datetime.datetime.fromisoformat('2019-01-01T00:01:00')), -        ('1minutes', datetime.datetime.fromisoformat('2019-01-01T00:01:00')), -        ('1s', datetime.datetime.fromisoformat('2019-01-01T00:00:01')), -        ('1S', datetime.datetime.fromisoformat('2019-01-01T00:00:01')), -        ('1second', datetime.datetime.fromisoformat('2019-01-01T00:00:01')), -        ('1seconds', datetime.datetime.fromisoformat('2019-01-01T00:00:01')), +        ('1Y', {"years": 1}), +        ('1y', {"years": 1}), +        ('1year', {"years": 1}), +        ('1years', {"years": 1}), +        ('1m', {"months": 1}), +        ('1month', {"months": 1}), +        ('1months', {"months": 1}), +        ('1w', {"weeks": 1}), +        ('1W', {"weeks": 1}), +        ('1week', {"weeks": 1}), +        ('1weeks', {"weeks": 1}), +        ('1d', {"days": 1}), +        ('1D', {"days": 1}), +        ('1day', {"days": 1}), +        ('1days', {"days": 1}), +        ('1h', {"hours": 1}), +        ('1H', {"hours": 1}), +        ('1hour', {"hours": 1}), +        ('1hours', {"hours": 1}), +        ('1M', {"minutes": 1}), +        ('1minute', {"minutes": 1}), +        ('1minutes', {"minutes": 1}), +        ('1s', {"seconds": 1}), +        ('1S', {"seconds": 1}), +        ('1second', {"seconds": 1}), +        ('1seconds', {"seconds": 1}),          # Complex duration strings -        ('1y1m1w1d1H1M1S', datetime.datetime.fromisoformat('2020-02-09T01:01:01')), -        ('5y100S', datetime.datetime.fromisoformat('2024-01-01T00:01:40')), -        ('2w28H', datetime.datetime.fromisoformat('2019-01-16T04:00:00')), +        ( +            '1y1m1w1d1H1M1S', +            { +                "years": 1, +                "months": 1, +                "weeks": 1, +                "days": 1, +                "hours": 1, +                "minutes": 1, +                "seconds": 1 +            } +        ), +        ('5y100S', {"years": 5, "seconds": 100}), +        ('2w28H', {"weeks": 2, "hours": 28}), + +        # Duration strings with spaces +        ('1 year 2 months', {"years": 1, "months": 2}), +        ('1d 2H', {"days": 1, "hours": 2}), +        ('1 week2 days', {"weeks": 1, "days": 2}),      )  ) -def test_duration_converter_for_valid(duration: str, expected: datetime): -    converter = Duration() +def create_future_datetime(request): +    """Yields duration string and target datetime.datetime object.""" +    duration, duration_dict = request.param +    future_datetime = FIXED_UTC_NOW + relativedelta(**duration_dict) +    yield duration, future_datetime + +def test_duration_converter_for_valid(create_future_datetime: tuple): +    converter = Duration() +    duration, expected = create_future_datetime      with patch('bot.converters.datetime') as mock_datetime:          mock_datetime.utcnow.return_value = FIXED_UTC_NOW          assert asyncio.run(converter.convert(None, duration)) == expected @@ -149,6 +159,10 @@ def test_duration_converter_for_valid(duration: str, expected: datetime):          ('1d1w'),          ('1s1y'), +        # Duplicated units +        ('1 year 2 years'), +        ('1 M 10 minutes'), +          # Unknown substrings          ('1MVes'),          ('1y3breads'), @@ -156,6 +170,11 @@ def test_duration_converter_for_valid(duration: str, expected: datetime):          # Missing amount          ('ym'), +        # Incorrect whitespace +        (" 1y"), +        ("1S "), +        ("1y  1m"), +          # Garbage          ('Guido van Rossum'),          ('lemon lemon lemon lemon lemon lemon lemon'), | 
