Skip to content

Commit

Permalink
make datetime from timestamp tests compare against Python result (#4275)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
davidhewitt and Cheukting committed Jun 22, 2024
1 parent 908ef6a commit a2f9399
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions pytests/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def tzname(self, dt):
IS_WINDOWS = sys.platform == "win32"

if IS_WINDOWS:
MIN_DATETIME = pdt.datetime(1971, 1, 2, 0, 0)
MIN_DATETIME = pdt.datetime(1970, 1, 1, 0, 0, 0)
if IS_32_BIT:
MAX_DATETIME = pdt.datetime(3001, 1, 19, 4, 59, 59)
MAX_DATETIME = pdt.datetime(2038, 1, 18, 23, 59, 59)
else:
MAX_DATETIME = pdt.datetime(3001, 1, 19, 7, 59, 59)
MAX_DATETIME = pdt.datetime(3000, 12, 31, 23, 59, 59)
else:
if IS_32_BIT:
# TS ±2147483648 (2**31)
Expand Down Expand Up @@ -93,11 +93,21 @@ def test_invalid_date_fails():

@given(d=st.dates(MIN_DATETIME.date(), MAX_DATETIME.date()))
def test_date_from_timestamp(d):
if PYPY and d < pdt.date(1900, 1, 1):
pytest.xfail("pdt.datetime.timestamp will raise on PyPy with dates before 1900")

ts = pdt.datetime.timestamp(pdt.datetime.combine(d, pdt.time(0)))
assert rdt.date_from_timestamp(int(ts)) == pdt.date.fromtimestamp(ts)
try:
ts = pdt.datetime.timestamp(d)
except Exception:
# out of range for timestamp
return

try:
expected = pdt.date.fromtimestamp(ts)
except Exception as pdt_fail:
# date from timestamp failed; expect the same from Rust binding
with pytest.raises(type(pdt_fail)) as exc_info:
rdt.date_from_timestamp(ts)
assert str(exc_info.value) == str(pdt_fail)
else:
assert rdt.date_from_timestamp(int(ts)) == expected


@pytest.mark.parametrize(
Expand Down Expand Up @@ -229,11 +239,21 @@ def test_datetime_typeerror():
@given(dt=st.datetimes(MIN_DATETIME, MAX_DATETIME))
@example(dt=pdt.datetime(1971, 1, 2, 0, 0))
def test_datetime_from_timestamp(dt):
if PYPY and dt < pdt.datetime(1900, 1, 1):
pytest.xfail("pdt.datetime.timestamp will raise on PyPy with dates before 1900")

ts = pdt.datetime.timestamp(dt)
assert rdt.datetime_from_timestamp(ts) == pdt.datetime.fromtimestamp(ts)
try:
ts = pdt.datetime.timestamp(dt)
except Exception:
# out of range for timestamp
return

try:
expected = pdt.datetime.fromtimestamp(ts)
except Exception as pdt_fail:
# datetime from timestamp failed; expect the same from Rust binding
with pytest.raises(type(pdt_fail)) as exc_info:
rdt.datetime_from_timestamp(ts)
assert str(exc_info.value) == str(pdt_fail)
else:
assert rdt.datetime_from_timestamp(ts) == expected


def test_datetime_from_timestamp_tzinfo():
Expand Down

0 comments on commit a2f9399

Please sign in to comment.