| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
"""Test isodatetime.main.""" |
| 19 |
|
| 20 |
|
| 21 |
import os |
| 22 |
from subprocess import PIPE, Popen |
| 23 |
from unittest.mock import patch |
| 24 |
|
| 25 |
import pytest |
| 26 |
|
| 27 |
import metomi.isodatetime |
| 28 |
import metomi.isodatetime.main as isodatetime_main |
| 29 |
|
| 30 |
|
| 31 |
@patch('builtins.print') |
| 32 |
def test_1_version(mock_print): |
| 33 |
"""Test print version.""" |
| 34 |
for args in [['--version'], ['-V']]: |
| 35 |
isodatetime_main.main(args) |
| 36 |
mock_print.assert_called_with( |
| 37 |
metomi.isodatetime.__version__ |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
@patch('builtins.print') |
| 42 |
def test_1_null(mock_print): |
| 43 |
"""Test calling usage 1, no or now argument.""" |
| 44 |
with patch.object( |
| 45 |
isodatetime_main.DateTimeOperator, |
| 46 |
'process_time_point_str', |
| 47 |
return_value='20190101T1234Z' |
| 48 |
): |
| 49 |
for arg in ['', 'now']: |
| 50 |
isodatetime_main.main([arg]) |
| 51 |
mock_print.assert_called_with('20190101T1234Z') |
| 52 |
|
| 53 |
|
| 54 |
@patch('builtins.print') |
| 55 |
def test_1_good(mock_print): |
| 56 |
"""Test calling usage 1, sample good arguments.""" |
| 57 |
env_ref = isodatetime_main.DateTimeOperator.ENV_REF |
| 58 |
ref = os.environ.get(env_ref) |
| 59 |
for args, out in [ |
| 60 |
(['20200101T00Z'], '20200101T00Z'), |
| 61 |
(['ref'], '20201225T0000Z'), |
| 62 |
|
| 63 |
(['-u', '20200101T00+0100'], '20191231T23+0000'), |
| 64 |
(['--utc', '20200101T00+0100'], '20191231T23+0000'), |
| 65 |
|
| 66 |
(['-s', 'P1D', '20191231T00Z'], '20200101T00Z'), |
| 67 |
(['-s', 'P1D', '--offset=PT1H', '20191231T00Z'], '20200101T01Z'), |
| 68 |
|
| 69 |
(['-s', 'P1D', '--offset=-PT1H', '20191231T00Z'], '20191231T23Z'), |
| 70 |
(['-s', 'P1D', '--offset', '\\-PT1H', '20191231T00Z'], |
| 71 |
'20191231T23Z'), |
| 72 |
|
| 73 |
(['-f', 'CCYY', '20191231T00Z'], '2019'), |
| 74 |
(['--format', 'CCYY', '20191231T00Z'], '2019'), |
| 75 |
(['--print-format', 'CCYY', '20191231T00Z'], '2019'), |
| 76 |
(['20200101T00-1130', '-f', 'CCYYMMDDThhmm+0000'], |
| 77 |
'20200101T1130+0000'), |
| 78 |
|
| 79 |
(['--parse-format=%d/%m/%Y', '-f', 'CCYY-MM-DD', '31/12/2019'], |
| 80 |
'2019-12-31'), |
| 81 |
(['-p', '%d/%m/%Y', '-f', 'CCYY-MM-DD', '31/12/2019'], |
| 82 |
'2019-12-31'), |
| 83 |
]: |
| 84 |
os.environ[env_ref] = '20201225T0000Z' |
| 85 |
try: |
| 86 |
isodatetime_main.main(args) |
| 87 |
mock_print.assert_called_with(out) |
| 88 |
finally: |
| 89 |
if ref is not None: |
| 90 |
os.environ[env_ref] = ref |
| 91 |
else: |
| 92 |
del os.environ[env_ref] |
| 93 |
|
| 94 |
|
| 95 |
@patch('builtins.print') |
| 96 |
def test_1_bad(mock_print): |
| 97 |
"""Test calling usage 1, sample bad arguments.""" |
| 98 |
for args, out in [ |
| 99 |
|
| 100 |
(['201abc'], 'Invalid ISO 8601 date representation: 201abc'), |
| 101 |
|
| 102 |
(['-s', 'add-a-year', '2019'], 'add-a-year: bad offset value'), |
| 103 |
]: |
| 104 |
mock_print.reset_mock() |
| 105 |
with pytest.raises(SystemExit) as ctxmgr: |
| 106 |
isodatetime_main.main(args) |
| 107 |
mock_print.assert_not_called() |
| 108 |
assert str(ctxmgr.value) == out |
| 109 |
|
| 110 |
|
| 111 |
@patch('builtins.print') |
| 112 |
def test_2_good(mock_print): |
| 113 |
"""Test calling usage 2, sample good arguments.""" |
| 114 |
env_ref = isodatetime_main.DateTimeOperator.ENV_REF |
| 115 |
ref = os.environ.get(env_ref) |
| 116 |
for args, out in [ |
| 117 |
|
| 118 |
(['ref', 'ref'], 'P0Y'), |
| 119 |
(['20380119', '20380119'], 'P0Y'), |
| 120 |
|
| 121 |
(['20181225', '20191225'], 'P365D'), |
| 122 |
(['-1', 'PT12H', '-2', 'PT12H', '20181225', '20191225'], 'P365D'), |
| 123 |
|
| 124 |
(['20191225', '20181225'], '-P365D'), |
| 125 |
(['--offset1=-PT6H', '--offset2=-PT6H', '20191225', '20181225'], |
| 126 |
'-P365D'), |
| 127 |
(['--offset1', '\\-PT6H', '--offset2', '\\-PT6H', '20191225', |
| 128 |
'20181225'], '-P365D'), |
| 129 |
]: |
| 130 |
os.environ[env_ref] = '20201225T0000Z' |
| 131 |
try: |
| 132 |
isodatetime_main.main(args) |
| 133 |
mock_print.assert_called_with(out) |
| 134 |
finally: |
| 135 |
if ref is not None: |
| 136 |
os.environ[env_ref] = ref |
| 137 |
else: |
| 138 |
del os.environ[env_ref] |
| 139 |
|
| 140 |
|
| 141 |
@patch('builtins.print') |
| 142 |
def test_2_bad(mock_print): |
| 143 |
"""Test calling usage 2, sample bad arguments.""" |
| 144 |
for args, out in [ |
| 145 |
|
| 146 |
(['201abc', '2020'], |
| 147 |
'Invalid ISO 8601 date representation: 201abc'), |
| 148 |
|
| 149 |
(['-1', 'add-a-year', '2018', '2019'], |
| 150 |
'add-a-year: bad offset value'), |
| 151 |
]: |
| 152 |
mock_print.reset_mock() |
| 153 |
with pytest.raises(SystemExit) as ctxmgr: |
| 154 |
isodatetime_main.main(args) |
| 155 |
mock_print.assert_not_called() |
| 156 |
assert str(ctxmgr.value) == out |
| 157 |
|
| 158 |
|
| 159 |
@patch('builtins.print') |
| 160 |
def test_3_good(mock_print): |
| 161 |
"""Test calling usage 3, sample good arguments.""" |
| 162 |
for args, out in [ |
| 163 |
|
| 164 |
(['--as-total=s', 'PT1H30M'], 5400), |
| 165 |
(['--as-total=s', 'P1D'], 86400), |
| 166 |
(['--as-total=h', 'P1D'], 24), |
| 167 |
]: |
| 168 |
isodatetime_main.main(args) |
| 169 |
mock_print.assert_called_with(out) |
| 170 |
|
| 171 |
|
| 172 |
@patch('builtins.print') |
| 173 |
def test_3_bad(mock_print): |
| 174 |
"""Test calling usage 3, sample bad arguments.""" |
| 175 |
mock_print.reset_mock() |
| 176 |
with pytest.raises(SystemExit) as ctxmgr: |
| 177 |
isodatetime_main.main(['--as-total=s', 'PS4']) |
| 178 |
mock_print.assert_not_called() |
| 179 |
assert str(ctxmgr.value) == ( |
| 180 |
'Invalid ISO 8601 duration representation: PS4' |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
@patch('builtins.print') |
| 185 |
def test_4_good(mock_print): |
| 186 |
"""Test calling usage 4, sample good arguments.""" |
| 187 |
for args, out in [ |
| 188 |
|
| 189 |
(['-u', 'R/2020/P1Y'], |
| 190 |
'\n'.join('%d-01-01T00:00:00Z' % i for i in range(2020, 2030))), |
| 191 |
(['-u', 'R3/2020/P1Y'], |
| 192 |
'\n'.join('%d-01-01T00:00:00Z' % i for i in range(2020, 2023))), |
| 193 |
(['-u', '--max=5', 'R/2020/P1Y'], |
| 194 |
'\n'.join('%d-01-01T00:00:00Z' % i for i in range(2020, 2025))), |
| 195 |
(['-u', '--max=15', 'R/2020/P1Y'], |
| 196 |
'\n'.join('%d-01-01T00:00:00Z' % i for i in range(2020, 2035))), |
| 197 |
(['--print-format=%Y', 'R/2020/P1Y'], |
| 198 |
'\n'.join('%d' % i for i in range(2020, 2030))), |
| 199 |
|
| 200 |
(['-u', 'R/P1Y/2020'], |
| 201 |
'\n'.join('%d-01-01T00:00:00Z' % i |
| 202 |
for i in range(2020, 2010, -1))), |
| 203 |
]: |
| 204 |
isodatetime_main.main(args) |
| 205 |
mock_print.assert_called_with(out) |
| 206 |
|
| 207 |
|
| 208 |
@patch('builtins.print') |
| 209 |
def test_4_bad(mock_print): |
| 210 |
"""Test calling usage 4, sample bad arguments.""" |
| 211 |
mock_print.reset_mock() |
| 212 |
with pytest.raises(SystemExit) as ctxmgr: |
| 213 |
isodatetime_main.main(['R-1/2020/2025']) |
| 214 |
mock_print.assert_not_called() |
| 215 |
assert str(ctxmgr.value) == ( |
| 216 |
'Invalid ISO 8601 recurrence representation: R-1/2020/2025' |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def test_negative_duration(): |
| 221 |
"""Test arg parsing of negative durations.""" |
| 222 |
for variant in ('-P1D', r'\-P1D',): |
| 223 |
args = isodatetime_main.parse_args(['2000', '-s', variant]) |
| 224 |
assert args.offsets1 == ['-P1D'] |
| 225 |
args = isodatetime_main.parse_args(['2000', rf'-s={variant}']) |
| 226 |
assert args.offsets1 == ['-P1D'] |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.parametrize( |
| 230 |
'stdin,args,stdout', [ |
| 231 |
('2000', [], '2000'), |
| 232 |
('2000\n2001', ['--as-total', 'h'], '8784.0') |
| 233 |
] |
| 234 |
) |
| 235 |
def test_pipe(stdin, args, stdout): |
| 236 |
"""Test piping args into the command via stdin.""" |
| 237 |
assert Popen( |
| 238 |
['isodatetime', '-'] + args, |
| 239 |
stdout=PIPE, stdin=PIPE |
| 240 |
).communicate( |
| 241 |
stdin.encode() |
| 242 |
)[0].decode().strip() == stdout |