Skip to content

Commit a2f9399

Browse files
make datetime from timestamp tests compare against Python result (#4275)
* attemp to fix range for st.datetime * remove example * try fixing to utc * make datetime from timestamp tests compare against Python result --------- Co-authored-by: Cheukting <cheukting.ho@gmail.com>
1 parent 908ef6a commit a2f9399

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

pytests/tests/test_datetime.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ def tzname(self, dt):
5656
IS_WINDOWS = sys.platform == "win32"
5757

5858
if IS_WINDOWS:
59-
MIN_DATETIME = pdt.datetime(1971, 1, 2, 0, 0)
59+
MIN_DATETIME = pdt.datetime(1970, 1, 1, 0, 0, 0)
6060
if IS_32_BIT:
61-
MAX_DATETIME = pdt.datetime(3001, 1, 19, 4, 59, 59)
61+
MAX_DATETIME = pdt.datetime(2038, 1, 18, 23, 59, 59)
6262
else:
63-
MAX_DATETIME = pdt.datetime(3001, 1, 19, 7, 59, 59)
63+
MAX_DATETIME = pdt.datetime(3000, 12, 31, 23, 59, 59)
6464
else:
6565
if IS_32_BIT:
6666
# TS ±2147483648 (2**31)
@@ -93,11 +93,21 @@ def test_invalid_date_fails():
9393

9494
@given(d=st.dates(MIN_DATETIME.date(), MAX_DATETIME.date()))
9595
def test_date_from_timestamp(d):
96-
if PYPY and d < pdt.date(1900, 1, 1):
97-
pytest.xfail("pdt.datetime.timestamp will raise on PyPy with dates before 1900")
98-
99-
ts = pdt.datetime.timestamp(pdt.datetime.combine(d, pdt.time(0)))
100-
assert rdt.date_from_timestamp(int(ts)) == pdt.date.fromtimestamp(ts)
96+
try:
97+
ts = pdt.datetime.timestamp(d)
98+
except Exception:
99+
# out of range for timestamp
100+
return
101+
102+
try:
103+
expected = pdt.date.fromtimestamp(ts)
104+
except Exception as pdt_fail:
105+
# date from timestamp failed; expect the same from Rust binding
106+
with pytest.raises(type(pdt_fail)) as exc_info:
107+
rdt.date_from_timestamp(ts)
108+
assert str(exc_info.value) == str(pdt_fail)
109+
else:
110+
assert rdt.date_from_timestamp(int(ts)) == expected
101111

102112

103113
@pytest.mark.parametrize(
@@ -229,11 +239,21 @@ def test_datetime_typeerror():
229239
@given(dt=st.datetimes(MIN_DATETIME, MAX_DATETIME))
230240
@example(dt=pdt.datetime(1971, 1, 2, 0, 0))
231241
def test_datetime_from_timestamp(dt):
232-
if PYPY and dt < pdt.datetime(1900, 1, 1):
233-
pytest.xfail("pdt.datetime.timestamp will raise on PyPy with dates before 1900")
234-
235-
ts = pdt.datetime.timestamp(dt)
236-
assert rdt.datetime_from_timestamp(ts) == pdt.datetime.fromtimestamp(ts)
242+
try:
243+
ts = pdt.datetime.timestamp(dt)
244+
except Exception:
245+
# out of range for timestamp
246+
return
247+
248+
try:
249+
expected = pdt.datetime.fromtimestamp(ts)
250+
except Exception as pdt_fail:
251+
# datetime from timestamp failed; expect the same from Rust binding
252+
with pytest.raises(type(pdt_fail)) as exc_info:
253+
rdt.datetime_from_timestamp(ts)
254+
assert str(exc_info.value) == str(pdt_fail)
255+
else:
256+
assert rdt.datetime_from_timestamp(ts) == expected
237257

238258

239259
def test_datetime_from_timestamp_tzinfo():

0 commit comments

Comments
 (0)