Skip to content

Commit ec9f76c

Browse files
dvndrsnekampf
authored andcommitted
Fix flaky date tests (#982)
* fix flaky datetime test with fixed values * rename fixtures for clarity in date+time tests
1 parent eb7966e commit ec9f76c

File tree

1 file changed

+52
-32
lines changed

1 file changed

+52
-32
lines changed

graphene/types/tests/test_datetime.py

+52-32
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,48 @@ def resolve_time(self, info, _at=None):
2727
schema = Schema(query=Query)
2828

2929

30-
def test_datetime_query():
31-
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
32-
isoformat = now.isoformat()
30+
@pytest.fixture
31+
def sample_datetime():
32+
utc_datetime = datetime.datetime(2019, 5, 25, 5, 30, 15, 10, pytz.utc)
33+
return utc_datetime
34+
35+
36+
@pytest.fixture
37+
def sample_time(sample_datetime):
38+
time = datetime.time(
39+
sample_datetime.hour,
40+
sample_datetime.minute,
41+
sample_datetime.second,
42+
sample_datetime.microsecond,
43+
sample_datetime.tzinfo,
44+
)
45+
return time
46+
47+
48+
@pytest.fixture
49+
def sample_date(sample_datetime):
50+
date = sample_datetime.date()
51+
return date
52+
53+
54+
def test_datetime_query(sample_datetime):
55+
isoformat = sample_datetime.isoformat()
3356

3457
result = schema.execute("""{ datetime(in: "%s") }""" % isoformat)
3558
assert not result.errors
3659
assert result.data == {"datetime": isoformat}
3760

3861

39-
def test_date_query():
40-
now = datetime.datetime.now().replace(tzinfo=pytz.utc).date()
41-
isoformat = now.isoformat()
62+
def test_date_query(sample_date):
63+
isoformat = sample_date.isoformat()
4264

4365
result = schema.execute("""{ date(in: "%s") }""" % isoformat)
4466
assert not result.errors
4567
assert result.data == {"date": isoformat}
4668

4769

48-
def test_time_query():
49-
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
50-
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
51-
isoformat = time.isoformat()
70+
def test_time_query(sample_time):
71+
isoformat = sample_time.isoformat()
5272

5373
result = schema.execute("""{ time(at: "%s") }""" % isoformat)
5474
assert not result.errors
@@ -85,14 +105,13 @@ def test_bad_time_query():
85105
assert result.data is None
86106

87107

88-
def test_datetime_query_variable():
89-
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
90-
isoformat = now.isoformat()
108+
def test_datetime_query_variable(sample_datetime):
109+
isoformat = sample_datetime.isoformat()
91110

92111
# test datetime variable provided as Python datetime
93112
result = schema.execute(
94113
"""query Test($date: DateTime){ datetime(in: $date) }""",
95-
variables={"date": now},
114+
variables={"date": sample_datetime},
96115
)
97116
assert not result.errors
98117
assert result.data == {"datetime": isoformat}
@@ -106,13 +125,13 @@ def test_datetime_query_variable():
106125
assert result.data == {"datetime": isoformat}
107126

108127

109-
def test_date_query_variable():
110-
now = datetime.datetime.now().replace(tzinfo=pytz.utc).date()
111-
isoformat = now.isoformat()
128+
def test_date_query_variable(sample_date):
129+
isoformat = sample_date.isoformat()
112130

113131
# test date variable provided as Python date
114132
result = schema.execute(
115-
"""query Test($date: Date){ date(in: $date) }""", variables={"date": now}
133+
"""query Test($date: Date){ date(in: $date) }""",
134+
variables={"date": sample_date},
116135
)
117136
assert not result.errors
118137
assert result.data == {"date": isoformat}
@@ -125,14 +144,13 @@ def test_date_query_variable():
125144
assert result.data == {"date": isoformat}
126145

127146

128-
def test_time_query_variable():
129-
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
130-
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
131-
isoformat = time.isoformat()
147+
def test_time_query_variable(sample_time):
148+
isoformat = sample_time.isoformat()
132149

133150
# test time variable provided as Python time
134151
result = schema.execute(
135-
"""query Test($time: Time){ time(at: $time) }""", variables={"time": time}
152+
"""query Test($time: Time){ time(at: $time) }""",
153+
variables={"time": sample_time},
136154
)
137155
assert not result.errors
138156
assert result.data == {"time": isoformat}
@@ -148,11 +166,13 @@ def test_time_query_variable():
148166
@pytest.mark.xfail(
149167
reason="creating the error message fails when un-parsable object is not JSON serializable."
150168
)
151-
def test_bad_variables():
152-
def _test_bad_variables(type, input):
169+
def test_bad_variables(sample_date, sample_datetime, sample_time):
170+
def _test_bad_variables(type_, input_):
153171
result = schema.execute(
154-
"""query Test($input: {}){{ {}(in: $input) }}""".format(type, type.lower()),
155-
variables={"input": input},
172+
"""query Test($input: {}){{ {}(in: $input) }}""".format(
173+
type_, type_.lower()
174+
),
175+
variables={"input": input_},
156176
)
157177
assert len(result.errors) == 1
158178
# when `input` is not JSON serializable formatting the error message in
@@ -163,9 +183,9 @@ def _test_bad_variables(type, input):
163183

164184
not_a_date = dict()
165185
not_a_date_str = "Some string that's not a date"
166-
today = datetime.date.today()
167-
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
168-
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
186+
today = sample_date
187+
now = sample_datetime
188+
time = sample_time
169189

170190
bad_pairs = [
171191
("DateTime", not_a_date),
@@ -182,5 +202,5 @@ def _test_bad_variables(type, input):
182202
("Time", today),
183203
]
184204

185-
for type, input in bad_pairs:
186-
_test_bad_variables(type, input)
205+
for type_, input_ in bad_pairs:
206+
_test_bad_variables(type_, input_)

0 commit comments

Comments
 (0)