Repository navigation
Expand file tree
/
Copy pathtest_ts.py
More file actions
276 lines (218 loc) · 8.32 KB
/
Copy pathtest_ts.py
File metadata and controls
276 lines (218 loc) · 8.32 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Tests for ts — timestamp converter."""
import subprocess
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
from zoneinfo import ZoneInfo
import pytest
# Import functions directly from the module under test
sys.path.insert(0, str(Path(__file__).parent))
from ts import (
detect_epoch_unit,
epoch_to_dt,
parse_human_to_epoch,
convert_one,
show_current,
resolve_tz,
build_parser,
)
TS_PATH = str(Path(__file__).parent / "ts.py")
# Correct epoch for 2024-06-20 14:33:54 UTC
KNOWN_EPOCH = 1718894034
KNOWN_DATE = "2024-06-20 14:33:54 UTC"
# ---------------------------------------------------------------------------
# detect_epoch_unit
# ---------------------------------------------------------------------------
class TestDetectEpochUnit:
def test_seconds(self):
assert detect_epoch_unit(KNOWN_EPOCH) == "s"
def test_seconds_small(self):
assert detect_epoch_unit(0) == "s"
def test_seconds_10_digits(self):
assert detect_epoch_unit(9999999999) == "s"
def test_milliseconds(self):
assert detect_epoch_unit(KNOWN_EPOCH * 1000) == "ms"
def test_milliseconds_13_digits(self):
assert detect_epoch_unit(9999999999999) == "ms"
def test_microseconds(self):
assert detect_epoch_unit(KNOWN_EPOCH * 1_000_000) == "us"
def test_microseconds_16_digits(self):
assert detect_epoch_unit(9999999999999999) == "us"
def test_negative_seconds(self):
assert detect_epoch_unit(-KNOWN_EPOCH) == "s"
# ---------------------------------------------------------------------------
# epoch_to_dt
# ---------------------------------------------------------------------------
class TestEpochToDt:
def test_known_epoch(self):
dt = epoch_to_dt(KNOWN_EPOCH)
assert dt.year == 2024
assert dt.month == 6
assert dt.day == 20
assert dt.hour == 14
assert dt.minute == 33
assert dt.second == 54
def test_zero_epoch(self):
dt = epoch_to_dt(0)
assert dt.year == 1970
assert dt.month == 1
assert dt.day == 1
def test_milliseconds(self):
dt = epoch_to_dt(KNOWN_EPOCH * 1000)
assert dt.year == 2024
assert dt.month == 6
assert dt.day == 20
def test_microseconds(self):
dt = epoch_to_dt(KNOWN_EPOCH * 1_000_000)
assert dt.year == 2024
assert dt.month == 6
assert dt.day == 20
def test_with_timezone(self):
tz = ZoneInfo("America/New_York")
dt = epoch_to_dt(KNOWN_EPOCH, tz=tz)
assert dt.tzinfo is not None
# 2024-06-20 14:33:54 UTC = 10:33:54 EDT (UTC-4 in summer)
assert dt.hour == 10
# ---------------------------------------------------------------------------
# parse_human_to_epoch
# ---------------------------------------------------------------------------
class TestParseHumanToEpoch:
def test_iso_with_z(self):
epoch = parse_human_to_epoch("2024-06-20T14:33:54Z")
assert epoch == KNOWN_EPOCH
def test_iso_with_t(self):
epoch = parse_human_to_epoch("2024-06-20 14:33:54")
assert epoch == KNOWN_EPOCH
def test_date_only(self):
epoch = parse_human_to_epoch("2024-06-20")
dt = datetime(2024, 6, 20, tzinfo=timezone.utc)
assert epoch == int(dt.timestamp())
def test_now(self):
before = int(time.time())
epoch = parse_human_to_epoch("now")
after = int(time.time())
assert before <= epoch <= after
def test_invalid_raises(self):
with pytest.raises(ValueError):
parse_human_to_epoch("not-a-date")
# ---------------------------------------------------------------------------
# convert_one
# ---------------------------------------------------------------------------
class TestConvertOne:
def test_epoch_to_human(self):
result = convert_one(str(KNOWN_EPOCH))
assert KNOWN_DATE in result
def test_epoch_ms_to_human(self):
result = convert_one(str(KNOWN_EPOCH * 1000))
assert KNOWN_DATE in result
def test_human_to_epoch(self):
result = convert_one("2024-06-20T14:33:54Z")
assert str(KNOWN_EPOCH) in result
def test_now_keyword(self):
result = convert_one("now")
assert "→" in result
def test_invalid_input(self):
result = convert_one("garbage")
assert "ERROR" in result
def test_with_timezone(self):
tz = ZoneInfo("America/New_York")
result = convert_one(str(KNOWN_EPOCH), tz=tz)
assert "America/New_York" in result or "EDT" in result or "EST" in result
# ---------------------------------------------------------------------------
# show_current
# ---------------------------------------------------------------------------
class TestShowCurrent:
def test_no_args_returns_list(self):
lines = show_current()
assert len(lines) >= 3
assert any("Epoch s" in l for l in lines)
assert any("Epoch ms" in l for l in lines)
assert any("UTC" in l for l in lines)
def test_with_timezone(self):
tz = ZoneInfo("UTC")
lines = show_current(tz=tz)
assert any("UTC" in l for l in lines)
# ---------------------------------------------------------------------------
# CLI integration tests
# ---------------------------------------------------------------------------
class TestCLI:
def _run(self, *args):
"""Run the ts CLI and return (stdout, stderr, returncode)."""
result = subprocess.run(
[sys.executable, TS_PATH, *args],
capture_output=True,
text=True,
)
return result.stdout.strip(), result.stderr.strip(), result.returncode
def test_no_args_shows_current(self):
out, err, rc = self._run()
assert rc == 0
assert "Epoch s" in out
assert "UTC" in out
def test_epoch_arg(self):
out, err, rc = self._run(str(KNOWN_EPOCH))
assert rc == 0
assert KNOWN_DATE in out
def test_epoch_ms_arg(self):
out, err, rc = self._run(str(KNOWN_EPOCH * 1000))
assert rc == 0
assert KNOWN_DATE in out
def test_human_to_epoch(self):
out, err, rc = self._run("2024-06-20T14:33:54Z")
assert rc == 0
assert str(KNOWN_EPOCH) in out
def test_now(self):
out, err, rc = self._run("now")
assert rc == 0
# Output is like "now → 1781950215 (2026-06-20 ...)"
assert "→" in out
# Extract the epoch number
parts = out.split("→")[1].strip().split()
epoch_str = parts[0]
epoch_val = int(epoch_str)
assert 1_700_000_000 < epoch_val < 2_000_000_000
def test_multiple_args(self):
out, err, rc = self._run(str(KNOWN_EPOCH), str(KNOWN_EPOCH + 1000))
assert rc == 0
lines = out.splitlines()
assert len(lines) == 2
def test_timezone_flag(self):
out, err, rc = self._run(str(KNOWN_EPOCH), "--tz", "America/New_York")
assert rc == 0
assert "2024-06-20" in out
def test_local_flag(self):
out, err, rc = self._run(str(KNOWN_EPOCH), "--local")
assert rc == 0
def test_stdin_mode(self):
inp = f"{KNOWN_EPOCH}\n{KNOWN_EPOCH + 1000}\n"
result = subprocess.run(
[sys.executable, TS_PATH, "--stdin"],
input=inp,
capture_output=True,
text=True,
)
assert result.returncode == 0
lines = result.stdout.strip().splitlines()
assert len(lines) == 2
def test_invalid_epoch(self):
out, err, rc = self._run("garbage")
assert rc == 0 # doesn't crash, shows error in output
assert "ERROR" in out
def test_zero_epoch(self):
out, err, rc = self._run("0")
assert rc == 0
assert "1970-01-01 00:00:00 UTC" in out
def test_microseconds(self):
out, err, rc = self._run(str(KNOWN_EPOCH * 1_000_000))
assert rc == 0
assert KNOWN_DATE in out
def test_date_only_input(self):
out, err, rc = self._run("2024-06-20")
assert rc == 0
expected_epoch = str(int(datetime(2024, 6, 20, tzinfo=timezone.utc).timestamp()))
assert expected_epoch in out
def test_bad_timezone_exits(self):
out, err, rc = self._run(str(KNOWN_EPOCH), "--tz", "Mars/Phobos")
assert rc == 1
assert "ERROR" in err