This repository was archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtest_time.py
76 lines (59 loc) · 2.95 KB
/
test_time.py
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
from datetime import datetime
from datetime import timezone
import pytest
from kube_downscaler.helper import matches_time_spec
def test_invalid_time_spec():
with pytest.raises(ValueError):
matches_time_spec(datetime.now(), "")
def test_time_spec():
assert not matches_time_spec(datetime.now(), "never")
assert matches_time_spec(datetime.now(), "always")
assert not matches_time_spec(datetime.now(), "Mon-Mon 00:00-00:00 CET")
assert matches_time_spec(datetime.now(), "Mon-Sun 00:00-24:00 CET")
# Sunday, November 26th 2017
dt = datetime(2017, 11, 26, 15, 33, tzinfo=timezone.utc)
assert matches_time_spec(dt, "Sat-Sun 15:30-16:00 UTC")
assert not matches_time_spec(dt, "Sat-Sun 15:34-16:00 UTC")
assert not matches_time_spec(dt, "Mon-Fri 08:00-18:00 UTC")
assert matches_time_spec(dt, "Mon-Fri 08:00-18:00 UTC, Sun-Sun 15:30-16:00 UTC")
assert matches_time_spec(dt, "2017-11-26T14:04:48+00:00-2017-11-26T16:04:48+00:00")
assert not matches_time_spec(
dt, "2017-11-26T14:04:48+00:00-2017-11-26T15:04:48+00:00"
)
dt = datetime(2018, 11, 4, 20, 30, 00, tzinfo=timezone.utc)
assert matches_time_spec(dt, "Mon-Fri 09:00-10:00 Pacific/Auckland")
assert not matches_time_spec(dt, "Sat-Sun 09:00-10:00 Pacific/Auckland")
assert matches_time_spec(dt, "2017-01-01T00:01:02+00:00-2018-12-31T23:59:00+00:00")
assert matches_time_spec(
dt,
"2017-01-01T00:01:02+00:00-2017-01-02T19:00:50+00:00, 2018-11-01T00:00:00+00:00-2018-12-31T23:59:00+00:00",
)
assert not matches_time_spec(
dt, "2019-01-01T00:01:02+00:00-2019-12-31T23:59:00+00:00"
)
assert matches_time_spec(dt, "2018-11-04T16:00:00-04:00-2018-11-04T16:40:00-04:00")
assert not matches_time_spec(
dt, "2018-11-04T20:00:00-04:00-2018-11-04T20:40:00-04:00"
)
def test_time_spec_error_message():
"""Test that the correct error message is shown to the user."""
dt = datetime(2020, 4, 10, 10, 11, tzinfo=timezone.utc)
with pytest.raises(ValueError) as excinfo:
matches_time_spec(
dt, "Mon-Thur 09:00-20:00 Europe/London,Fri-Fri 10:00-18:00 Europe/London"
)
assert (
'Time spec value "Mon-Thur 09:00-20:00 Europe/London" does not match format ("Mon-Fri 06:30-20:30 Europe/Berlin" or "2019-01-01T00:00:00+00:00-2019-01-02T12:34:56+00:00")'
in str(excinfo.value)
)
def test_week_starts_sunday():
# Monday, November 27th 2017
dt = datetime(2017, 11, 27, 15, 33, tzinfo=timezone.utc)
assert matches_time_spec(dt, "Sun-Fri 15:30-16:00 UTC")
assert matches_time_spec(dt, "Sun-Mon 00:00-16:00 UTC")
assert not matches_time_spec(dt, "Sun-Mon 00:00-15:00 UTC")
# Sunday, November 26th 2017
dt = datetime(2017, 11, 26, 15, 33, tzinfo=timezone.utc)
assert matches_time_spec(dt, "Sun-Fri 15:30-16:00 UTC")
assert matches_time_spec(dt, "Sun-Mon 00:00-16:00 UTC")
assert not matches_time_spec(dt, "Sun-Mon 00:00-15:00 UTC")