File: test_utils.py

package info (click to toggle)
zabbix-cli 3.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,860 kB
  • sloc: python: 18,557; makefile: 3
file content (171 lines) | stat: -rw-r--r-- 4,969 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from __future__ import annotations

from datetime import datetime
from datetime import timedelta

import pytest
from freezegun import freeze_time
from zabbix_cli.utils import convert_duration
from zabbix_cli.utils.utils import convert_time_to_interval
from zabbix_cli.utils.utils import convert_timestamp
from zabbix_cli.utils.utils import convert_timestamp_interval


@pytest.mark.parametrize(
    "input,expect",
    [
        (
            "1 hour",
            timedelta(hours=1),
        ),
        (
            "1 hour 30 minutes",
            timedelta(hours=1, minutes=30),
        ),
        (
            "1 hour 30 minutes 30 seconds",
            timedelta(hours=1, minutes=30, seconds=30),
        ),
        (
            "1 day 1 hour 30 minutes 30 seconds",
            timedelta(days=1, hours=1, minutes=30, seconds=30),
        ),
        (
            "2 hours 30 seconds",
            timedelta(hours=2, seconds=30),
        ),
        (
            "2h30s",
            timedelta(hours=2, seconds=30),
        ),
        (
            "2 hours 30 minutes 30 seconds",
            timedelta(hours=2, minutes=30, seconds=30),
        ),
        (
            "2 hour 30 minute 30 second",
            timedelta(hours=2, minutes=30, seconds=30),
        ),
        (
            "2h30m30s",
            timedelta(hours=2, minutes=30, seconds=30),
        ),
        (
            "9030s",
            timedelta(seconds=9030),
        ),
        (
            "9030",
            timedelta(seconds=9030),
        ),
        (
            "0",
            timedelta(seconds=0),
        ),
        (
            "0d0h0m0s",
            timedelta(days=0, hours=0, minutes=0, seconds=0),
        ),
    ],
)
def test_convert_duration(input: str, expect: timedelta) -> None:
    assert convert_duration(input) == expect
    assert convert_duration(input).total_seconds() == expect.total_seconds()


@pytest.mark.parametrize(
    "input, expect",
    [
        pytest.param(
            "2016-11-21T22:00 to 2016-11-21T23:00",
            (datetime(2016, 11, 21, 22, 0, 0), datetime(2016, 11, 21, 23, 0, 0)),
            id="Legacy format",  #  (ISO w/o timezone and seconds)
        ),
        pytest.param(
            "2016-11-21T22:00:00 to 2016-11-21T23:00:00",
            (datetime(2016, 11, 21, 22, 0, 0), datetime(2016, 11, 21, 23, 0, 0)),
            id="ISO w/o timezone",
        ),
        pytest.param(
            "2016-11-21 22:00:00 to 2016-11-21 23:00:00",
            (datetime(2016, 11, 21, 22, 0, 0), datetime(2016, 11, 21, 23, 0, 0)),
            id="ISO w/o timezone (space-separated)",
        ),
    ],
)
def test_convert_timestamp_interval(
    input: str, expect: tuple[datetime, datetime]
) -> None:
    assert convert_timestamp_interval(input) == expect
    # TODO: test with mix of formats. e.g. "2016-11-21T22:00 to 2016-11-21 23:00:00"


@pytest.mark.parametrize(
    "input,expect",
    [
        pytest.param(
            "2016-11-21T22:00",
            datetime(2016, 11, 21, 22, 0, 0),
            id="Legacy",
        ),
        pytest.param(
            "2016-11-21T22:00:00",
            datetime(2016, 11, 21, 22, 0, 0),
            id="ISO w/o timezone",
        ),
        pytest.param(
            "2016-11-21 22:00:00",
            datetime(2016, 11, 21, 22, 0, 0),
            id="ISO w/o timezone (space-separated)",
        ),
    ],
)
def test_convert_timestamp(input: str, expect: datetime) -> None:
    assert convert_timestamp(input) == expect


@pytest.mark.parametrize(
    "input,expect_duration",
    [
        pytest.param(
            "2016-11-21T22:00 to 2016-11-21T23:00",
            timedelta(hours=1),
            id="Range: Legacy",
        ),
        pytest.param(
            "2016-11-21T22:00:00 to 2016-11-21T23:00:00",
            timedelta(hours=1),
            id="Range: ISO w/o timezone",
        ),
        pytest.param(
            "2016-11-21 22:00:00 to 2016-11-21 23:00:00",
            timedelta(hours=1),
            id="Range: ISO w/o timezone (space-separated)",
        ),
        pytest.param(
            "1 hour",
            timedelta(hours=1),
            id="Duration: 1h (long form)",
        ),
        pytest.param(
            "1 day 1 hour 30 minutes 30 seconds",
            timedelta(days=1, hours=1, minutes=30, seconds=30),
            id="Duration: 1d1h30m30s (long form)",
        ),
        pytest.param(
            "1h",
            timedelta(hours=1),
            id="Duration: 1h (short form)",
        ),
        pytest.param(
            "1d1h30m30s",
            timedelta(days=1, hours=1, minutes=30, seconds=30),
            id="Duration: 1d1h30m30s (short form)",
        ),
    ],
)
@freeze_time("2016-11-21 22:00:00")
def test_convert_time_to_interval(input: str, expect_duration: timedelta) -> None:
    start, end = convert_time_to_interval(input)
    assert start == datetime(2016, 11, 21, 22, 0, 0)
    assert end == start + expect_duration