with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    min_dt = pd.Timestamp(1900, 1, 1).to_pydatetime(),
    max_dt = pd.Timestamp(1900, 1, 1).to_pydatetime(),

gen_date_range = st.builds(
    pd.date_range,
    start=st.datetimes(
        # TODO: Choose the min/max values more systematically
        min_value=pd.Timestamp(1900, 1, 1).to_pydatetime(),
        max_value=pd.Timestamp(2100, 1, 1).to_pydatetime()
    ),
    periods=st.integers(min_value=2, max_value=100),
    freq=st.sampled_from('Y Q M D H T s ms us ns'.split()),
    tz=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()),
)

gen_random_datetime = st.datetimes(
    min_value=min_dt,
    max_value=max_dt,
    timezones=st.one_of(st.none(), dateutil_timezones(), pytz_timezones())
)

# The strategy for each type is registered in conftest.py, as they don't carry
# enough runtime information (e.g. type hints) to infer how to build them.
gen_yqm_offset = st.one_of(*map(st.from_type, [
    MonthBegin, MonthEnd, BMonthBegin, BMonthEnd,
    QuarterBegin, QuarterEnd, BQuarterBegin, BQuarterEnd,
    YearBegin, YearEnd, BYearBegin, BYearEnd
]))
Example #2
0
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    min_dt = Timestamp(1900, 1, 1).to_pydatetime()
    max_dt = Timestamp(1900, 1, 1).to_pydatetime()

gen_date_range = st.builds(
    pd.date_range,
    start=st.datetimes(
        # TODO: Choose the min/max values more systematically
        min_value=Timestamp(1900, 1, 1).to_pydatetime(),
        max_value=Timestamp(2100, 1, 1).to_pydatetime(),
    ),
    periods=st.integers(min_value=2, max_value=100),
    freq=st.sampled_from("Y Q M D H T s ms us ns".split()),
    tz=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()),
)

gen_random_datetime = st.datetimes(
    min_value=min_dt,
    max_value=max_dt,
    timezones=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()),
)

# The strategy for each type is registered in conftest.py, as they don't carry
# enough runtime information (e.g. type hints) to infer how to build them.
gen_yqm_offset = st.one_of(
    *map(
        st.from_type,
        [
            MonthBegin,
Example #3
0
import unittest
from datetime import date, datetime, time, timezone

from hypothesis import given, 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):
    # Surrogate characters have been particularly problematic here in the past,
    # so we give them a boost by combining strategies pending an upstream
    # feature (https://github.com/HypothesisWorks/hypothesis/issues/1401)
    @unittest.skipIf(not hasattr(datetime, "fromisoformat"), reason="new in 3.7")
    @given(
        dt=st.datetimes(timezones=TIME_ZONES_STRATEGY),
        sep=st.characters() | st.characters(whitelist_categories=["Cs"]),
    )
    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)

    @unittest.skipIf(not hasattr(datetime, "fromisocalendar"), reason="new in 3.8")
    @given(dt=st.datetimes())
    def test_fromisocalendar_date_datetime(self, dt):
        isocalendar = dt.isocalendar()
        dt_rt = datetime.fromisocalendar(*isocalendar)