Repository navigation
Expand file tree
/
Copy pathtest_formatters.py
More file actions
377 lines (313 loc) · 12.6 KB
/
Copy pathtest_formatters.py
File metadata and controls
377 lines (313 loc) · 12.6 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Tests for the formatting helpers in ``python_utils.formatters``."""
import datetime
import types
import typing
from zoneinfo import ZoneInfo, available_timezones
import pytest
from python_utils import formatters
@pytest.mark.parametrize(
('days', 'expected'),
[
(30, '1 month ago'),
(35, '1 month and 5 days ago'),
(60, '2 months ago'),
(365, '1 year ago'),
(730, '2 years ago'),
],
)
def test_timesince_units_do_not_overlap(days: int, expected: str) -> None:
"""Take the weeks and days from what the larger units left over."""
delta: datetime.timedelta = datetime.timedelta(days=days)
assert formatters.timesince(delta) == expected
@pytest.mark.parametrize(
('delta', 'expected'),
[
(datetime.timedelta(seconds=-1), '1 second ago'),
(datetime.timedelta(seconds=-61), '1 minute and 1 second ago'),
(datetime.timedelta(days=-400), '1 year and 1 month ago'),
],
)
def test_timesince_negative_timedelta(
delta: datetime.timedelta, expected: str
) -> None:
"""Describe a negative timedelta by its size, like a datetime."""
assert formatters.timesince(delta) == expected
@pytest.mark.parametrize(
'timezone',
[
datetime.timezone.utc,
datetime.timezone(datetime.timedelta(hours=5, minutes=30)),
],
)
def test_timesince_aware_datetime(timezone: datetime.timezone) -> None:
"""Compare a timezone-aware datetime with an aware current time."""
# Only the two largest units are shown, so the time this test takes
# cannot change the result.
age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
moment: datetime.datetime = datetime.datetime.now(timezone) - age
assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
class _UnknownOffset(datetime.tzinfo):
"""A timezone that does not know its UTC offset, as ``tzinfo`` allows."""
def utcoffset(self, dt: datetime.datetime | None) -> None:
"""Report the offset as unknown."""
return
def dst(self, dt: datetime.datetime | None) -> None:
"""Report the daylight saving offset as unknown."""
return
def tzname(self, dt: datetime.datetime | None) -> str:
"""Name the timezone."""
return 'unknown'
def fromutc(self, dt: datetime.datetime) -> datetime.datetime:
"""Use the UTC wall clock as the local one."""
return dt.replace(tzinfo=self)
class _OwnOffsetDateTime(datetime.datetime):
"""A datetime subclass that reports an offset its tzinfo does not know."""
def utcoffset(self) -> datetime.timedelta:
"""Report a fixed offset of three hours."""
return datetime.timedelta(hours=3)
def test_timesince_datetime_subclass_with_own_offset() -> None:
"""Subtract a subclass with its own offset the way a datetime does."""
age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
moment: datetime.datetime = _OwnOffsetDateTime.now(_UnknownOffset()) - age
assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
class _ClockChange(datetime.tzinfo):
"""A zone that sets its clock back an hour at 2026-10-25 01:00 UTC.
One instance serves both sides of the change, as a ``dateutil`` zone
does, so two datetimes in it share their tzinfo.
"""
_CHANGE_UTC: datetime.datetime = datetime.datetime(2026, 10, 25, 1)
_CHANGE_WALL: datetime.datetime = datetime.datetime(2026, 10, 25, 2)
def utcoffset(self, dt: datetime.datetime | None) -> datetime.timedelta:
"""Report two hours before the change and one hour after it."""
if dt is None or dt.replace(tzinfo=None) < self._CHANGE_WALL:
return datetime.timedelta(hours=2)
return datetime.timedelta(hours=1)
def dst(self, dt: datetime.datetime | None) -> datetime.timedelta:
"""Report the daylight saving part of the offset."""
return self.utcoffset(dt) - datetime.timedelta(hours=1)
def tzname(self, dt: datetime.datetime | None) -> str:
"""Name the timezone."""
return 'clock change'
def fromutc(self, dt: datetime.datetime) -> datetime.datetime:
"""Convert from UTC by the offset in force at that instant."""
if dt.replace(tzinfo=None) < self._CHANGE_UTC:
return dt + datetime.timedelta(hours=2)
return dt + datetime.timedelta(hours=1)
def test_timesince_counts_elapsed_time_across_dst_in_any_zone(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Count a clock change for a tzinfo that is not a ``ZoneInfo``."""
zone = _ClockChange()
# 23:00 at +02:00 is 21:00 UTC, six hours before the frozen current time.
moment = datetime.datetime(2026, 10, 24, 23, tzinfo=zone)
current = datetime.datetime(2026, 10, 25, 3, tzinfo=datetime.timezone.utc)
class FrozenDateTime(datetime.datetime):
"""Return the chosen instant as the current time."""
@classmethod
def now(cls, tz: datetime.tzinfo | None = None) -> 'FrozenDateTime':
"""Keep the same instant while returning the requested timezone."""
return cls.fromtimestamp(current.timestamp(), tz)
monkeypatch.setattr(
formatters,
'datetime',
types.SimpleNamespace(
datetime=FrozenDateTime,
timedelta=datetime.timedelta,
timezone=datetime.timezone,
),
)
assert formatters.timesince(moment) == '6 hours ago'
class _LookAlike:
"""Not a datetime, but subtracts from one, as ``arrow.Arrow`` does."""
def __init__(self, moment: datetime.datetime) -> None:
"""Wrap an aware datetime."""
self.moment: datetime.datetime = moment
self.tzinfo: datetime.tzinfo | None = moment.tzinfo
def __rsub__(self, other: datetime.datetime) -> datetime.timedelta:
"""Subtract the wrapped datetime from ``other``."""
return other - self.moment
def test_timesince_datetime_look_alike() -> None:
"""Let an object that is not a datetime do its own subtraction."""
age: datetime.timedelta = datetime.timedelta(hours=1, minutes=30)
# Not one of the documented types, so typed as Any like the date test.
moment: typing.Any = _LookAlike(
datetime.datetime.now(datetime.timezone.utc) - age
)
assert formatters.timesince(moment) == '1 hour and 30 minutes ago'
@pytest.mark.parametrize(
('moment', 'current'),
[
(
datetime.datetime(2026, 11, 1, 1, 30, fold=0),
datetime.datetime(2026, 11, 1, 1, 30, fold=1),
),
(
datetime.datetime(2026, 11, 1, 1, 30, fold=1),
datetime.datetime(2026, 11, 1, 1, 30, fold=0),
),
(
datetime.datetime(2026, 3, 8, 1, 30),
datetime.datetime(2026, 3, 8, 3, 30),
),
(
datetime.datetime(2026, 3, 8, 3, 30),
datetime.datetime(2026, 3, 8, 1, 30),
),
],
ids=['fall-back-past', 'fall-back-future', 'spring-past', 'spring-future'],
)
@pytest.mark.skipif(
'America/New_York' not in available_timezones(),
reason='IANA timezone data is not available',
)
def test_timesince_counts_elapsed_time_across_dst(
moment: datetime.datetime,
current: datetime.datetime,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Count elapsed time when both datetimes share a changing UTC offset."""
timezone = ZoneInfo('America/New_York')
current = current.replace(tzinfo=timezone)
moment = moment.replace(tzinfo=timezone)
class FrozenDateTime(datetime.datetime):
"""Return the chosen instant as the current time."""
@classmethod
def now(cls, tz: datetime.tzinfo | None = None) -> 'FrozenDateTime':
"""Keep the same instant while returning the requested timezone."""
return cls.fromtimestamp(current.timestamp(), tz)
monkeypatch.setattr(
formatters,
'datetime',
types.SimpleNamespace(
datetime=FrozenDateTime,
timedelta=datetime.timedelta,
timezone=datetime.timezone,
),
)
assert formatters.timesince(moment) == '1 hour ago'
class _ElapsedRsub(datetime.datetime):
"""A subclass whose own subtraction counts elapsed time, as in pandas."""
def __rsub__(self, other: datetime.datetime) -> datetime.timedelta:
"""Subtract by UTC timestamp, so a DST change is counted."""
seconds: float = other.timestamp() - datetime.datetime.timestamp(self)
return datetime.timedelta(seconds=seconds)
@pytest.mark.skipif(
'America/New_York' not in available_timezones(),
reason='IANA timezone data is not available',
)
def test_timesince_subclass_subtraction_counts_dst_once(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Leave a subclass that counts elapsed time itself to count it once."""
timezone = ZoneInfo('America/New_York')
current = datetime.datetime(2026, 11, 1, 1, 30, fold=1, tzinfo=timezone)
moment = _ElapsedRsub(2026, 11, 1, 1, 30, fold=0, tzinfo=timezone)
def frozen_now(tz: datetime.tzinfo | None = None) -> datetime.datetime:
"""Return the chosen instant as a plain datetime, like the clock."""
return datetime.datetime.fromtimestamp(current.timestamp(), tz)
monkeypatch.setattr(
formatters,
'datetime',
types.SimpleNamespace(
datetime=types.SimpleNamespace(now=frozen_now),
timedelta=datetime.timedelta,
timezone=datetime.timezone,
),
)
assert formatters.timesince(moment) == '1 hour ago'
@pytest.mark.parametrize(
('moment', 'expected'),
[
(
datetime.datetime.min.replace(
tzinfo=datetime.timezone(datetime.timedelta(hours=1))
),
'2000 years and 3 months ago',
),
(
datetime.datetime.max.replace(
tzinfo=datetime.timezone(datetime.timedelta(hours=-1))
),
'8005 years and 3 months ago',
),
],
ids=['min-positive-offset', 'max-negative-offset'],
)
def test_timesince_aware_datetime_boundaries(
moment: datetime.datetime,
expected: str,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Accept aware inputs whose corresponding UTC date is out of range."""
current = datetime.datetime(2000, 1, 1, tzinfo=datetime.timezone.utc)
class FrozenDateTime(datetime.datetime):
"""Return the chosen instant as the current time."""
@classmethod
def now(cls, tz: datetime.tzinfo | None = None) -> 'FrozenDateTime':
"""Keep the same instant while returning the requested timezone."""
return cls.fromtimestamp(current.timestamp(), tz)
monkeypatch.setattr(
formatters,
'datetime',
types.SimpleNamespace(
datetime=FrozenDateTime,
timedelta=datetime.timedelta,
timezone=datetime.timezone,
),
)
assert formatters.timesince(moment) == expected
@pytest.mark.parametrize(
('name', 'expected'),
[
('SPAM_EGGS', 'spam_eggs'),
('HTTP_OK', 'http_ok'),
('MAX_SIZE_LIMIT', 'max_size_limit'),
('HTML5Parser', 'html5_parser'),
('toUTF8String', 'to_utf8_string'),
('ABCD_e', 'abcd_e'),
],
)
def test_camel_to_underscore_keeps_acronyms_whole(
name: str, expected: str
) -> None:
"""Leave an acronym whole when an underscore or a digit follows it."""
assert formatters.camel_to_underscore(name) == expected
@pytest.mark.parametrize(
('name', 'expected'),
[
('ABc', 'a_bc'),
('ABCd', 'ab_cd'),
('IPad', 'i_pad'),
('AName', 'a_name'),
('__ABc__', '__a_bc__'),
('XMLParser', 'xml_parser'),
],
)
def test_camel_to_underscore_splits_short_acronym_boundary(
name: str, expected: str
) -> None:
"""Keep the final capital with the word following even a short acronym."""
assert formatters.camel_to_underscore(name) == expected
@pytest.mark.parametrize(
('name', 'expected'),
[
('IDs', 'ids'),
('URLs', 'urls'),
('UserIDs', 'user_ids'),
('GetURLs', 'get_urls'),
('IDsAndNames', 'ids_and_names'),
('PDFs_v2', 'pdfs_v2'),
('ISpam', 'i_spam'),
('IPsec', 'i_psec'),
],
)
def test_camel_to_underscore_keeps_plural_acronyms_whole(
name: str, expected: str
) -> None:
"""Keep a plural ``s`` that ends a word with the acronym before it."""
assert formatters.camel_to_underscore(name) == expected
def test_timesince_date_raises_type_error() -> None:
"""Keep raising ``TypeError`` for a date, which has no time."""
today: typing.Any = datetime.date.today()
with pytest.raises(TypeError):
formatters.timesince(today)