Created
July 25, 2019 06:25
-
-
Save suvayu/acc4ff97b239e1f3c7d9070a569b17e2 to your computer and use it in GitHub Desktop.
Hypothesis generates large number of invalid examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, time | |
import pytest | |
from hypothesis import given, assume, strategies as st | |
from hypothesis import settings, Verbosity | |
_cities = [f"city{i}" for i in range(4)] | |
_dt = st.shared( | |
st.datetimes( | |
min_value=datetime(2019, 1, 1), max_value=datetime(2029, 12, 31) | |
) | |
) | |
# working hours | |
utc = time(2, 30), time(2 + 12, 30) | |
@st.composite | |
def datestr(draw, dt): | |
return ( | |
draw(dt) | |
.replace(hour=0, minute=0, second=0, microsecond=0) | |
.isoformat(timespec="milliseconds") | |
+ "Z" | |
) | |
@st.composite | |
def timestamp(draw, dt, working_hrs): | |
ts = draw(dt) | |
if working_hrs: | |
t1, t2 = working_hrs | |
assume(t1 <= ts.time() <= t2) | |
return ts.isoformat(timespec="milliseconds") + "Z" | |
def get_req(dt, cities, hrs): | |
return st.fixed_dictionaries( | |
{ | |
"date": datestr(dt), | |
"city": st.sampled_from(cities), | |
"slots": st.lists(timestamp(dt, hrs), min_size=1, max_size=5), | |
} | |
) | |
api_request = st.lists(get_req(_dt, _cities, hrs=utc), min_size=1, max_size=5) | |
@settings(max_examples=100, verbosity=Verbosity.verbose) | |
@given(api_request) | |
def test_api_gen(reqs): | |
assert len(reqs) > 0 | |
def get_req_only_ts(dt, hrs): | |
return st.fixed_dictionaries( | |
{ | |
"date": datestr(dt), | |
"slots": st.lists(timestamp(dt, hrs), min_size=1, max_size=5), | |
} | |
) | |
api_request_no_city = st.lists( | |
get_req_only_ts(_dt, hrs=utc), min_size=1, max_size=5 | |
) | |
@settings(max_examples=100, verbosity=Verbosity.verbose) | |
@given(api_request_no_city) | |
def test_api_gen_no_city(reqs): | |
assert len(reqs) > 0 | |
api_request_all_day = st.lists( | |
get_req(_dt, _cities, hrs=None), min_size=1, max_size=5 | |
) | |
@settings(max_examples=100, verbosity=Verbosity.verbose) | |
@given(api_request_all_day) | |
def test_api_gen_all_day(reqs): | |
assert len(reqs) > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment