| 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 platform |
| 12 |
import zoneinfo |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import given, strategies as st |
| 17 |
from hypothesis.errors import InvalidArgument |
| 18 |
|
| 19 |
from tests.common.debug import assert_no_examples, find_any, minimal |
| 20 |
|
| 21 |
|
| 22 |
def test_utc_is_minimal(): |
| 23 |
assert minimal(st.timezones()) is zoneinfo.ZoneInfo("UTC") |
| 24 |
|
| 25 |
|
| 26 |
def test_can_generate_non_utc(): |
| 27 |
find_any( |
| 28 |
st.datetimes(timezones=st.timezones()).filter(lambda d: d.tzinfo.key != "UTC") |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
@given(st.data(), st.datetimes(), st.datetimes()) |
| 33 |
def test_datetimes_stay_within_naive_bounds(data, lo, hi): |
| 34 |
if lo > hi: |
| 35 |
lo, hi = hi, lo |
| 36 |
out = data.draw(st.datetimes(lo, hi, timezones=st.timezones())) |
| 37 |
assert lo <= out.replace(tzinfo=None) <= hi |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize("kwargs", [{"no_cache": 1}]) |
| 41 |
def test_timezones_argument_validation(kwargs): |
| 42 |
with pytest.raises(InvalidArgument): |
| 43 |
st.timezones(**kwargs).validate() |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.parametrize( |
| 47 |
"kwargs", |
| 48 |
[ |
| 49 |
# {"allow_alias": 1}, |
| 50 |
# {"allow_deprecated": 1}, |
| 51 |
{"allow_prefix": 1}, |
| 52 |
], |
| 53 |
) |
| 54 |
def test_timezone_keys_argument_validation(kwargs): |
| 55 |
with pytest.raises(InvalidArgument): |
| 56 |
st.timezone_keys(**kwargs).validate() |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.xfail(strict=False, reason="newly failing on GitHub Actions") |
| 60 |
@pytest.mark.skipif(platform.system() != "Linux", reason="platform-specific") |
| 61 |
def test_can_generate_prefixes_if_allowed_and_available(): |
| 62 |
""" |
| 63 |
This is actually kinda fiddly: we may generate timezone keys with the |
| 64 |
"posix/" or "right/" prefix if-and-only-if they are present on the filesystem. |
| 65 |
|
| 66 |
This immediately rules out Windows (which uses the tzdata package instead), |
| 67 |
along with OSX (which doesn't seem to have prefixed keys). We believe that |
| 68 |
they are present on at least most Linux distros, but have not done exhaustive |
| 69 |
testing. |
| 70 |
|
| 71 |
It's fine to just patch this test out if it fails - passing in the |
| 72 |
Hypothesis CI demonstrates that the feature works on *some* systems. |
| 73 |
""" |
| 74 |
find_any(st.timezone_keys(), lambda s: s.startswith("posix/")) |
| 75 |
find_any(st.timezone_keys(), lambda s: s.startswith("right/")) |
| 76 |
|
| 77 |
|
| 78 |
def test_can_disallow_prefixes(): |
| 79 |
assert_no_examples( |
| 80 |
st.timezone_keys(allow_prefix=False), |
| 81 |
lambda s: s.startswith(("posix/", "right/")), |
| 82 |
) |