Debian Codegraph @

hypothesis-python/tests/cover/test_datetimes.py

raw
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
13 import pytest
14
15 from hypothesis import HealthCheck, given, settings
16 from hypothesis.strategies import dates, datetimes, timedeltas, times
17
18 from tests.common.debug import assert_simple_property, find_any, minimal
19
20
21 def test_can_find_positive_delta():
22 assert minimal(timedeltas(), lambda x: x.days > 0) == dt.timedelta(1)
23
24
25 def test_can_find_negative_delta():
26 assert minimal(
27 timedeltas(max_value=dt.timedelta(10**6)), lambda x: x.days < 0
28 ) == dt.timedelta(-1)
29
30
31 def test_can_find_on_the_second():
32 find_any(timedeltas(), lambda x: x.seconds == 0)
33
34
35 def test_can_find_off_the_second():
36 find_any(timedeltas(), lambda x: x.seconds != 0)
37
38
39 def test_simplifies_towards_zero_delta():
40 d = minimal(timedeltas())
41 assert d.days == d.seconds == d.microseconds == 0
42
43
44 def test_min_value_is_respected():
45 assert minimal(timedeltas(min_value=dt.timedelta(days=10))).days == 10
46
47
48 def test_max_value_is_respected():
49 assert minimal(timedeltas(max_value=dt.timedelta(days=-10))).days == -10
50
51
52 @settings(suppress_health_check=list(HealthCheck))
53 @given(timedeltas())
54 def test_single_timedelta(val):
55 assert_simple_property(timedeltas(val, val), lambda v: v is val)
56
57
58 def test_simplifies_towards_millenium():
59 d = minimal(datetimes())
60 assert d.year == 2000
61 assert d.month == d.day == 1
62 assert d.hour == d.minute == d.second == d.microsecond == 0
63
64
65 @given(datetimes())
66 def test_default_datetimes_are_naive(dt):
67 assert dt.tzinfo is None
68
69
70 def test_bordering_on_a_leap_year():
71 x = minimal(
72 datetimes(
73 dt.datetime.min.replace(year=2003), dt.datetime.max.replace(year=2005)
74 ),
75 lambda x: x.month == 2 and x.day == 29,
76 settings=settings(max_examples=2500),
77 )
78 assert x.year == 2004
79
80
81 def test_can_find_after_the_year_2000():
82 assert minimal(dates(), lambda x: x.year > 2000).year == 2001
83
84
85 def test_can_find_before_the_year_2000():
86 assert minimal(dates(), lambda x: x.year < 2000).year == 1999
87
88
89 @pytest.mark.parametrize("month", range(1, 13))
90 def test_can_find_each_month(month):
91 find_any(dates(), lambda x: x.month == month, settings(max_examples=10**6))
92
93
94 def test_min_year_is_respected():
95 assert minimal(dates(min_value=dt.date.min.replace(2003))).year == 2003
96
97
98 def test_max_year_is_respected():
99 assert minimal(dates(max_value=dt.date.min.replace(1998))).year == 1998
100
101
102 @given(dates())
103 def test_single_date(val):
104 assert find_any(dates(val, val)) is val
105
106
107 def test_can_find_midnight():
108 find_any(times(), lambda x: x.hour == x.minute == x.second == 0)
109
110
111 def test_can_find_non_midnight():
112 assert minimal(times(), lambda x: x.hour != 0).hour == 1
113
114
115 def test_can_find_on_the_minute():
116 find_any(times(), lambda x: x.second == 0)
117
118
119 def test_can_find_off_the_minute():
120 find_any(times(), lambda x: x.second != 0)
121
122
123 def test_simplifies_towards_midnight():
124 d = minimal(times())
125 assert d.hour == d.minute == d.second == d.microsecond == 0
126
127
128 def test_can_generate_naive_time():
129 find_any(times(), lambda d: not d.tzinfo)
130
131
132 @given(times())
133 def test_naive_times_are_naive(dt):
134 assert dt.tzinfo is None
135
136
137 def test_can_generate_datetime_with_fold_1():
138 find_any(datetimes(), lambda d: d.fold)
139
140
141 def test_can_generate_time_with_fold_1():
142 find_any(times(), lambda d: d.fold)
143
144
145 @given(datetimes(allow_imaginary=False))
146 def test_allow_imaginary_is_not_an_error_for_naive_datetimes(d):
147 pass