Skip to content

Commit b43a3f5

Browse files
committed
Add strptime redundant directive ordering property tests
Permute agreeing date, clock and offset representations and compare the result with independently known fields. Cover calendar versus ISO years, 12-hour versus 24-hour clocks and both numeric offset directives.
1 parent b38cd94 commit b43a3f5

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

‎Lib/test/test_datetime_property.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import time
55
import unittest
6+
from collections.abc import Sequence
67
from test import support
78
from test.support import import_helper
89
from test.support.hypothesis_helper import hypothesis
@@ -253,6 +254,57 @@ class DateTimeTest(unittest.TestCase):
253254
datetime_module = datetime
254255
theclass = datetime.datetime
255256

257+
@support.run_with_locale("LC_TIME", "C")
258+
@hypothesis.settings(max_examples=25)
259+
@hypothesis.given(
260+
dt=st.datetimes(),
261+
representation=st.sampled_from(("calendar", "iso", "clock", "offset")),
262+
order=st.permutations((0, 1, 2, 3)),
263+
)
264+
@hypothesis.example(dt=datetime.datetime.min,
265+
representation="calendar", order=(3, 2, 1, 0))
266+
@hypothesis.example(dt=datetime.datetime(1969, 7, 20),
267+
representation="calendar", order=(0, 1, 2, 3))
268+
@hypothesis.example(dt=datetime.datetime(2016, 1, 1),
269+
representation="iso", order=(1, 0, 3, 2))
270+
@hypothesis.example(dt=datetime.datetime(2000, 1, 1, 0),
271+
representation="clock", order=(3, 1, 2, 0))
272+
@hypothesis.example(dt=datetime.datetime(2000, 1, 1, 12),
273+
representation="clock", order=(2, 1, 0, 3))
274+
@hypothesis.example(dt=datetime.datetime.max,
275+
representation="offset", order=(3, 2, 1, 0))
276+
def test_redundant_directive_order(
277+
self, dt: datetime.datetime, representation: str, order: Sequence[int],
278+
) -> None:
279+
date = ("%Y-%m-%d", dt.date().isoformat())
280+
clock = ("%H:%M:%S.%f", dt.time().isoformat(timespec="microseconds"))
281+
expected = dt
282+
suffix = ""
283+
if representation == "calendar":
284+
ordinal = dt.toordinal() - datetime.date(dt.year, 1, 1).toordinal() + 1
285+
parts = (("%Y", f"{dt.year:04}"),
286+
("%m-%d", f"{dt.month:02}-{dt.day:02}"),
287+
("%j", str(ordinal)), ("%u", str(dt.isoweekday())))
288+
expected = datetime.datetime(dt.year, dt.month, dt.day)
289+
elif representation == "iso":
290+
year, week, weekday = dt.isocalendar()
291+
parts = (date, ("%G %V %u", f"{year:04} {week} {weekday}"),
292+
clock, ("%z", "+010203.000004"))
293+
suffix = "+01:02:03.000004"
294+
elif representation == "clock":
295+
ampm = "AM" if dt.hour < 12 else "PM"
296+
parts = (date, ("%H", str(dt.hour)),
297+
("%I %p", f"{dt.hour % 12 or 12} {ampm}"),
298+
("%M:%S.%f", f"{dt.minute}:{dt.second}.{dt.microsecond:06}"))
299+
else:
300+
parts = (date, clock, ("%z", "+010203.000004"),
301+
("%:z", "+01:02:03.000004"))
302+
suffix = "+01:02:03.000004"
303+
fmt = "|".join(parts[index][0] for index in order)
304+
value = "|".join(parts[index][1] for index in order)
305+
actual = self.theclass.strptime(value, fmt)
306+
self.assertEqual(actual.isoformat(), expected.isoformat() + suffix)
307+
256308
@support.run_with_locale("LC_TIME", "fr_FR.UTF-8", "fr_FR.utf8",
257309
"de_DE.UTF-8", "de_DE.utf8", "en_US.UTF-8",
258310
"en_US.utf8", "en_US")

0 commit comments

Comments
 (0)