Fix flaky date tests (#982)

* fix flaky datetime test with fixed values

* rename fixtures for clarity in date+time tests
This commit is contained in:
David Anderson 2019-06-01 16:41:45 -04:00 committed by Eran Kampf
parent eb7966eca7
commit ec9f76cfae

View File

@ -27,28 +27,48 @@ class Query(ObjectType):
schema = Schema(query=Query) schema = Schema(query=Query)
def test_datetime_query(): @pytest.fixture
now = datetime.datetime.now().replace(tzinfo=pytz.utc) def sample_datetime():
isoformat = now.isoformat() utc_datetime = datetime.datetime(2019, 5, 25, 5, 30, 15, 10, pytz.utc)
return utc_datetime
@pytest.fixture
def sample_time(sample_datetime):
time = datetime.time(
sample_datetime.hour,
sample_datetime.minute,
sample_datetime.second,
sample_datetime.microsecond,
sample_datetime.tzinfo,
)
return time
@pytest.fixture
def sample_date(sample_datetime):
date = sample_datetime.date()
return date
def test_datetime_query(sample_datetime):
isoformat = sample_datetime.isoformat()
result = schema.execute("""{ datetime(in: "%s") }""" % isoformat) result = schema.execute("""{ datetime(in: "%s") }""" % isoformat)
assert not result.errors assert not result.errors
assert result.data == {"datetime": isoformat} assert result.data == {"datetime": isoformat}
def test_date_query(): def test_date_query(sample_date):
now = datetime.datetime.now().replace(tzinfo=pytz.utc).date() isoformat = sample_date.isoformat()
isoformat = now.isoformat()
result = schema.execute("""{ date(in: "%s") }""" % isoformat) result = schema.execute("""{ date(in: "%s") }""" % isoformat)
assert not result.errors assert not result.errors
assert result.data == {"date": isoformat} assert result.data == {"date": isoformat}
def test_time_query(): def test_time_query(sample_time):
now = datetime.datetime.now().replace(tzinfo=pytz.utc) isoformat = sample_time.isoformat()
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
isoformat = time.isoformat()
result = schema.execute("""{ time(at: "%s") }""" % isoformat) result = schema.execute("""{ time(at: "%s") }""" % isoformat)
assert not result.errors assert not result.errors
@ -85,14 +105,13 @@ def test_bad_time_query():
assert result.data is None assert result.data is None
def test_datetime_query_variable(): def test_datetime_query_variable(sample_datetime):
now = datetime.datetime.now().replace(tzinfo=pytz.utc) isoformat = sample_datetime.isoformat()
isoformat = now.isoformat()
# test datetime variable provided as Python datetime # test datetime variable provided as Python datetime
result = schema.execute( result = schema.execute(
"""query Test($date: DateTime){ datetime(in: $date) }""", """query Test($date: DateTime){ datetime(in: $date) }""",
variables={"date": now}, variables={"date": sample_datetime},
) )
assert not result.errors assert not result.errors
assert result.data == {"datetime": isoformat} assert result.data == {"datetime": isoformat}
@ -106,13 +125,13 @@ def test_datetime_query_variable():
assert result.data == {"datetime": isoformat} assert result.data == {"datetime": isoformat}
def test_date_query_variable(): def test_date_query_variable(sample_date):
now = datetime.datetime.now().replace(tzinfo=pytz.utc).date() isoformat = sample_date.isoformat()
isoformat = now.isoformat()
# test date variable provided as Python date # test date variable provided as Python date
result = schema.execute( result = schema.execute(
"""query Test($date: Date){ date(in: $date) }""", variables={"date": now} """query Test($date: Date){ date(in: $date) }""",
variables={"date": sample_date},
) )
assert not result.errors assert not result.errors
assert result.data == {"date": isoformat} assert result.data == {"date": isoformat}
@ -125,14 +144,13 @@ def test_date_query_variable():
assert result.data == {"date": isoformat} assert result.data == {"date": isoformat}
def test_time_query_variable(): def test_time_query_variable(sample_time):
now = datetime.datetime.now().replace(tzinfo=pytz.utc) isoformat = sample_time.isoformat()
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
isoformat = time.isoformat()
# test time variable provided as Python time # test time variable provided as Python time
result = schema.execute( result = schema.execute(
"""query Test($time: Time){ time(at: $time) }""", variables={"time": time} """query Test($time: Time){ time(at: $time) }""",
variables={"time": sample_time},
) )
assert not result.errors assert not result.errors
assert result.data == {"time": isoformat} assert result.data == {"time": isoformat}
@ -148,11 +166,13 @@ def test_time_query_variable():
@pytest.mark.xfail( @pytest.mark.xfail(
reason="creating the error message fails when un-parsable object is not JSON serializable." reason="creating the error message fails when un-parsable object is not JSON serializable."
) )
def test_bad_variables(): def test_bad_variables(sample_date, sample_datetime, sample_time):
def _test_bad_variables(type, input): def _test_bad_variables(type_, input_):
result = schema.execute( result = schema.execute(
"""query Test($input: {}){{ {}(in: $input) }}""".format(type, type.lower()), """query Test($input: {}){{ {}(in: $input) }}""".format(
variables={"input": input}, type_, type_.lower()
),
variables={"input": input_},
) )
assert len(result.errors) == 1 assert len(result.errors) == 1
# when `input` is not JSON serializable formatting the error message in # when `input` is not JSON serializable formatting the error message in
@ -163,9 +183,9 @@ def test_bad_variables():
not_a_date = dict() not_a_date = dict()
not_a_date_str = "Some string that's not a date" not_a_date_str = "Some string that's not a date"
today = datetime.date.today() today = sample_date
now = datetime.datetime.now().replace(tzinfo=pytz.utc) now = sample_datetime
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo) time = sample_time
bad_pairs = [ bad_pairs = [
("DateTime", not_a_date), ("DateTime", not_a_date),
@ -182,5 +202,5 @@ def test_bad_variables():
("Time", today), ("Time", today),
] ]
for type, input in bad_pairs: for type_, input_ in bad_pairs:
_test_bad_variables(type, input) _test_bad_variables(type_, input_)