Skip to content

Commit

Permalink
Add test for fromisoformat
Browse files Browse the repository at this point in the history
The documented contract of this function is that it is the inverse of
`datetime.isoformat()`.

See GH HypothesisWorks/hypothesis#1401, HypothesisWorks/hypothesis#69
and to a lesser extent HypothesisWorks/hypothesis#2273 for background on
why I have set max_examples so high.
  • Loading branch information
pganssle committed Jan 30, 2020
1 parent 7db3331 commit 5c497a3
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion tests/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import unittest
from datetime import datetime, timezone

from hypothesis import given, settings, strategies as st
from hypothesis.extra.dateutil import timezones as dateutil_timezones

TIME_ZONES_STRATEGY = st.one_of(
st.sampled_from([None, timezone.utc]), dateutil_timezones()
)


class TestDatetime(unittest.TestCase):
# TODO: https://docs.python.org/3/library/datetime.html
pass

# The search space here is large and as of January 2020 the sampling
# functions for characters() and datetimes() are not biased towards
# "interesting" examples, so it's worth throwing some extra cycles at this.
@settings(max_examples=2500)
@given(dt=st.datetimes(timezones=TIME_ZONES_STRATEGY), sep=st.characters())
def test_fromisoformat_auto(self, dt, sep):
"""Test isoformat with timespec="auto"."""
dtstr = dt.isoformat(sep=sep, timespec="auto")
dt_rt = datetime.fromisoformat(dtstr)

self.assertEqual(dt, dt_rt)

0 comments on commit 5c497a3

Please sign in to comment.