aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utils/test_time.py
blob: 61dd55c4ac6d1abd8d4b2070143629333babc957 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import asyncio
from datetime import datetime, timezone
from unittest.mock import patch

import pytest
from dateutil.relativedelta import relativedelta

from bot.utils import time
from tests.helpers import AsyncMock


@pytest.mark.parametrize(
    ('delta', 'precision', 'max_units', 'expected'),
    (
        (relativedelta(days=2), 'seconds', 1, '2 days'),
        (relativedelta(days=2, hours=2), 'seconds', 2, '2 days and 2 hours'),
        (relativedelta(days=2, hours=2), 'seconds', 1, '2 days'),
        (relativedelta(days=2, hours=2), 'days', 2, '2 days'),

        # Does not abort for unknown units, as the unit name is checked
        # against the attribute of the relativedelta instance.
        (relativedelta(days=2, hours=2), 'elephants', 2, '2 days and 2 hours'),

        # Very high maximum units, but it only ever iterates over
        # each value the relativedelta might have.
        (relativedelta(days=2, hours=2), 'hours', 20, '2 days and 2 hours'),

        # Negative maximum units.
        (relativedelta(days=2, hours=2), 'hours', -1, 'less than a hour'),
    )
)
def test_humanize_delta(
        delta: relativedelta,
        precision: str,
        max_units: int,
        expected: str
):
    assert time.humanize_delta(delta, precision, max_units) == expected


@pytest.mark.parametrize(
    ('stamp', 'expected'),
    (
        ('Sun, 15 Sep 2019 12:00:00 GMT', datetime(2019, 9, 15, 12, 0, 0, tzinfo=timezone.utc)),
    )
)
def test_parse_rfc1123(stamp: str, expected: str):
    assert time.parse_rfc1123(stamp) == expected


@patch('asyncio.sleep', new_callable=AsyncMock)
def test_wait_until(sleep_patch):
    start = datetime(2019, 1, 1, 0, 0)
    then = datetime(2019, 1, 1, 0, 10)

    # No return value
    assert asyncio.run(time.wait_until(then, start)) is None

    sleep_patch.assert_called_once_with(10 * 60)