File: test_utils.py

package info (click to toggle)
gcalcli 4.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,376 kB
  • sloc: python: 4,135; makefile: 10; sh: 7
file content (83 lines) | stat: -rw-r--r-- 3,073 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from datetime import datetime, timedelta

import pytest
from dateutil.tz import UTC, tzutc

from gcalcli import utils


def test_get_time_from_str():
    assert utils.get_time_from_str('7am tomorrow')


def test_get_parsed_timedelta_from_str():
    assert utils.get_timedelta_from_str('3.5h') == timedelta(
                                        hours=3, minutes=30)
    assert utils.get_timedelta_from_str('1') == timedelta(minutes=1)
    assert utils.get_timedelta_from_str('1m') == timedelta(minutes=1)
    assert utils.get_timedelta_from_str('1h') == timedelta(hours=1)
    assert utils.get_timedelta_from_str('1h1m') == timedelta(
                                        hours=1, minutes=1)
    assert utils.get_timedelta_from_str('1:10') == timedelta(
                                        hours=1, minutes=10)
    assert utils.get_timedelta_from_str('2d:1h:3m') == timedelta(
                                        days=2, hours=1, minutes=3)
    assert utils.get_timedelta_from_str('2d 1h 3m 10s') == timedelta(
                                        days=2, hours=1, minutes=3, seconds=10)
    assert utils.get_timedelta_from_str(
        '2 days 1 hour 2 minutes 40 seconds') == timedelta(
                                        days=2, hours=1, minutes=2, seconds=40)
    with pytest.raises(ValueError) as ve:
        utils.get_timedelta_from_str('junk')
    assert str(ve.value) == "Duration is invalid: junk"


def test_get_times_from_duration():
    begin_1970 = '1970-01-01'
    begin_1970_midnight = begin_1970 + 'T00:00:00+00:00'
    two_hrs_later = begin_1970 + 'T02:00:00+00:00'
    next_day = '1970-01-02'
    assert (begin_1970_midnight, two_hrs_later) == \
        utils.get_times_from_duration(begin_1970_midnight, duration=120)

    assert (begin_1970_midnight, two_hrs_later) == \
        utils.get_times_from_duration(
            begin_1970_midnight, duration="2h")

    assert (begin_1970_midnight, two_hrs_later) == \
        utils.get_times_from_duration(
            begin_1970_midnight, duration="120m")

    assert (begin_1970, next_day) == \
        utils.get_times_from_duration(
            begin_1970_midnight, duration=1, allday=True)

    with pytest.raises(ValueError):
        utils.get_times_from_duration('this is not a date')

    with pytest.raises(ValueError):
        utils.get_times_from_duration(
            begin_1970_midnight, duration='not a duration')

    with pytest.raises(ValueError):
        utils.get_times_from_duration(
                begin_1970_midnight, duration='not a duraction', allday=True)


def test_days_since_epoch():
    assert utils.days_since_epoch(datetime(1970, 1, 1, 0, tzinfo=UTC)) == 0
    assert utils.days_since_epoch(datetime(1970, 12, 31)) == 364


def test_set_locale():
    with pytest.raises(ValueError):
        utils.set_locale('not_a_real_locale')


def test_localize_datetime(PatchedGCalI):
    dt = utils.localize_datetime(datetime.now())
    assert dt.tzinfo is not None

    dt = datetime.now(tzutc())
    dt = utils.localize_datetime(dt)
    assert dt.tzinfo is not None