forked from HypothesisWorks/hypothesis
/
test_datetimes.py
142 lines (90 loc) · 3.65 KB
/
test_datetimes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Most of this work is copyright (C) 2013-2019 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
#
# END HEADER
from __future__ import absolute_import, division, print_function
import datetime as dt
import pytest
from hypothesis import given
from hypothesis.internal.compat import hrange
from hypothesis.strategies import dates, datetimes, timedeltas, times
from tests.common.debug import find_any, minimal
def test_can_find_positive_delta():
assert minimal(timedeltas(), lambda x: x.days > 0) == dt.timedelta(1)
def test_can_find_negative_delta():
assert minimal(
timedeltas(max_value=dt.timedelta(10 ** 6)), lambda x: x.days < 0
) == dt.timedelta(-1)
def test_can_find_on_the_second():
find_any(timedeltas(), lambda x: x.seconds == 0)
def test_can_find_off_the_second():
find_any(timedeltas(), lambda x: x.seconds != 0)
def test_simplifies_towards_zero_delta():
d = minimal(timedeltas())
assert d.days == d.seconds == d.microseconds == 0
def test_min_value_is_respected():
assert minimal(timedeltas(min_value=dt.timedelta(days=10))).days == 10
def test_max_value_is_respected():
assert minimal(timedeltas(max_value=dt.timedelta(days=-10))).days == -10
@given(timedeltas())
def test_single_timedelta(val):
assert find_any(timedeltas(val, val)) is val
def test_simplifies_towards_millenium():
d = minimal(datetimes())
assert d.year == 2000
assert d.month == d.day == 1
assert d.hour == d.minute == d.second == d.microsecond == 0
@given(datetimes())
def test_default_datetimes_are_naive(dt):
assert dt.tzinfo is None
def test_bordering_on_a_leap_year():
x = minimal(
datetimes(
dt.datetime.min.replace(year=2003), dt.datetime.max.replace(year=2005)
),
lambda x: x.month == 2 and x.day == 29,
timeout_after=60,
)
assert x.year == 2004
def test_can_find_after_the_year_2000():
assert minimal(dates(), lambda x: x.year > 2000).year == 2001
def test_can_find_before_the_year_2000():
assert minimal(dates(), lambda x: x.year < 2000).year == 1999
@pytest.mark.parametrize("month", hrange(1, 13))
def test_can_find_each_month(month):
find_any(dates(), lambda x: x.month == month)
def test_min_year_is_respected():
assert minimal(dates(min_value=dt.date.min.replace(2003))).year == 2003
def test_max_year_is_respected():
assert minimal(dates(max_value=dt.date.min.replace(1998))).year == 1998
@given(dates())
def test_single_date(val):
assert find_any(dates(val, val)) is val
def test_can_find_midnight():
find_any(times(), lambda x: x.hour == x.minute == x.second == 0)
def test_can_find_non_midnight():
assert minimal(times(), lambda x: x.hour != 0).hour == 1
def test_can_find_on_the_minute():
find_any(times(), lambda x: x.second == 0)
def test_can_find_off_the_minute():
find_any(times(), lambda x: x.second != 0)
def test_simplifies_towards_midnight():
d = minimal(times())
assert d.hour == d.minute == d.second == d.microsecond == 0
def test_can_generate_naive_time():
find_any(times(), lambda d: not d.tzinfo)
@given(times())
def test_naive_times_are_naive(dt):
assert dt.tzinfo is None