| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import datetime as dt |
| 12 |
import sys |
| 13 |
import warnings |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import assume, given |
| 18 |
from hypothesis.errors import FailedHealthCheck, InvalidArgument |
| 19 |
from hypothesis.strategies import data, datetimes, just, sampled_from, times |
| 20 |
from hypothesis.strategies._internal.datetime import datetime_does_not_exist |
| 21 |
|
| 22 |
from tests.common.debug import assert_all_examples, find_any, minimal |
| 23 |
from tests.common.utils import Why, fails_with, xfail_on_crosshair |
| 24 |
|
| 25 |
with warnings.catch_warnings(): |
| 26 |
if sys.version_info[:2] >= (3, 12): |
| 27 |
# Prior to https://github.com/dateutil/dateutil/pull/1285/ |
| 28 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 29 |
from dateutil import tz, zoneinfo |
| 30 |
|
| 31 |
from hypothesis.extra.dateutil import timezones |
| 32 |
|
| 33 |
|
| 34 |
def test_utc_is_minimal(): |
| 35 |
assert tz.UTC is minimal(timezones()) |
| 36 |
|
| 37 |
|
| 38 |
def test_can_generate_non_naive_time(): |
| 39 |
assert minimal(times(timezones=timezones()), lambda d: d.tzinfo).tzinfo == tz.UTC |
| 40 |
|
| 41 |
|
| 42 |
def test_can_generate_non_naive_datetime(): |
| 43 |
assert ( |
| 44 |
minimal(datetimes(timezones=timezones()), lambda d: d.tzinfo).tzinfo == tz.UTC |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
@given(datetimes(timezones=timezones())) |
| 49 |
def test_timezone_aware_datetimes_are_timezone_aware(dt): |
| 50 |
assert dt.tzinfo is not None |
| 51 |
|
| 52 |
|
| 53 |
@given(sampled_from(["min_value", "max_value"]), datetimes(timezones=timezones())) |
| 54 |
def test_datetime_bounds_must_be_naive(name, val): |
| 55 |
with pytest.raises(InvalidArgument): |
| 56 |
datetimes(**{name: val}).validate() |
| 57 |
|
| 58 |
|
| 59 |
def test_timezones_arg_to_datetimes_must_be_search_strategy(): |
| 60 |
all_timezones = zoneinfo.get_zonefile_instance().zones |
| 61 |
with pytest.raises(InvalidArgument): |
| 62 |
datetimes(timezones=all_timezones).validate() |
| 63 |
|
| 64 |
|
| 65 |
@given(times(timezones=timezones())) |
| 66 |
def test_timezone_aware_times_are_timezone_aware(dt): |
| 67 |
assert dt.tzinfo is not None |
| 68 |
|
| 69 |
|
| 70 |
def test_can_generate_non_utc(): |
| 71 |
times(timezones=timezones()).filter( |
| 72 |
lambda d: assume(d.tzinfo) and d.tzinfo.zone != "UTC" |
| 73 |
).validate() |
| 74 |
|
| 75 |
|
| 76 |
@given(sampled_from(["min_value", "max_value"]), times(timezones=timezones())) |
| 77 |
def test_time_bounds_must_be_naive(name, val): |
| 78 |
with pytest.raises(InvalidArgument): |
| 79 |
times(**{name: val}).validate() |
| 80 |
|
| 81 |
|
| 82 |
def test_should_have_correct_ordering(): |
| 83 |
def offset(timezone): |
| 84 |
return abs(timezone.utcoffset(dt.datetime(2000, 1, 1))) |
| 85 |
|
| 86 |
next_interesting_tz = minimal(timezones(), lambda tz: offset(tz) > dt.timedelta(0)) |
| 87 |
assert offset(next_interesting_tz) == dt.timedelta(seconds=3600) |
| 88 |
|
| 89 |
|
| 90 |
@given(data(), datetimes(), datetimes()) |
| 91 |
def test_datetimes_stay_within_naive_bounds(data, lo, hi): |
| 92 |
if lo > hi: |
| 93 |
lo, hi = hi, lo |
| 94 |
out = data.draw(datetimes(lo, hi, timezones=timezones())) |
| 95 |
assert lo <= out.replace(tzinfo=None) <= hi |
| 96 |
|
| 97 |
|
| 98 |
DAY_WITH_IMAGINARY_HOUR_KWARGS = { |
| 99 |
# The day of a spring-forward transition; 2am is imaginary |
| 100 |
"min_value": dt.datetime(2020, 10, 4), |
| 101 |
"max_value": dt.datetime(2020, 10, 5), |
| 102 |
"timezones": just(tz.gettz("Australia/Sydney")), |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
@xfail_on_crosshair(Why.other, strict=False) # fromutc argument must be a datetime |
| 107 |
@given(datetimes(timezones=timezones()) | datetimes(**DAY_WITH_IMAGINARY_HOUR_KWARGS)) |
| 108 |
def test_dateutil_exists_our_not_exists_are_inverse(value): |
| 109 |
assert datetime_does_not_exist(value) == (not tz.datetime_exists(value)) |
| 110 |
|
| 111 |
|
| 112 |
def test_datetimes_can_exclude_imaginary(): |
| 113 |
find_any( |
| 114 |
datetimes(**DAY_WITH_IMAGINARY_HOUR_KWARGS, allow_imaginary=True), |
| 115 |
lambda x: not tz.datetime_exists(x), |
| 116 |
) |
| 117 |
assert_all_examples( |
| 118 |
datetimes(**DAY_WITH_IMAGINARY_HOUR_KWARGS, allow_imaginary=False), |
| 119 |
tz.datetime_exists, |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
@xfail_on_crosshair(Why.other) |
| 124 |
@fails_with(FailedHealthCheck) |
| 125 |
@given( |
| 126 |
datetimes( |
| 127 |
max_value=dt.datetime(1, 1, 1, 9), |
| 128 |
timezones=just(tz.gettz("Australia/Sydney")), |
| 129 |
allow_imaginary=False, |
| 130 |
) |
| 131 |
) |
| 132 |
def test_non_imaginary_datetimes_at_boundary(val): |
| 133 |
# This is expected to fail because Australia/Sydney is UTC+10, |
| 134 |
# and the filter logic overflows when checking for round-trips. |
| 135 |
raise AssertionError |