Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/strategies.py
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
hypothesis_default_settings = settings.get_profile("default") | hypothesis_default_settings = settings.get_profile("default") | ||||
if repr(settings()) == repr(hypothesis_default_settings): | if repr(settings()) == repr(hypothesis_default_settings): | ||||
settings.load_profile("swh-web") | settings.load_profile("swh-web") | ||||
# The following strategies exploit the hypothesis capabilities | # The following strategies exploit the hypothesis capabilities | ||||
def _filter_checksum(cs): | |||||
generated_checksums = get_tests_data()["generated_checksums"] | |||||
if not int.from_bytes(cs, byteorder="little") or cs in generated_checksums: | |||||
return False | |||||
generated_checksums.add(cs) | |||||
return True | |||||
def _known_swh_object(object_type): | def _known_swh_object(object_type): | ||||
return sampled_from(get_tests_data()[object_type]) | return sampled_from(get_tests_data()[object_type]) | ||||
def sha1(): | def sha1(): | ||||
""" | """ | ||||
Hypothesis strategy returning a valid hexadecimal sha1 value. | Hypothesis strategy returning a valid hexadecimal sha1 value. | ||||
""" | """ | ||||
return binary(min_size=20, max_size=20).filter(_filter_checksum).map(hash_to_hex) | return binary(min_size=20, max_size=20).map(hash_to_hex) | ||||
def invalid_sha1(): | def invalid_sha1(): | ||||
""" | """ | ||||
Hypothesis strategy returning an invalid sha1 representation. | Hypothesis strategy returning an invalid sha1 representation. | ||||
""" | """ | ||||
return binary(min_size=50, max_size=50).filter(_filter_checksum).map(hash_to_hex) | return binary(min_size=50, max_size=50).map(hash_to_hex) | ||||
def sha256(): | def sha256(): | ||||
""" | """ | ||||
Hypothesis strategy returning a valid hexadecimal sha256 value. | Hypothesis strategy returning a valid hexadecimal sha256 value. | ||||
""" | """ | ||||
return binary(min_size=32, max_size=32).filter(_filter_checksum).map(hash_to_hex) | return binary(min_size=32, max_size=32).map(hash_to_hex) | ||||
def content(): | def content(): | ||||
""" | """ | ||||
Hypothesis strategy returning a random content ingested | Hypothesis strategy returning a random content ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
return _known_swh_object("contents") | return _known_swh_object("contents") | ||||
▲ Show 20 Lines • Show All 247 Lines • ▼ Show 20 Lines | def origin_with_pull_request_branches(): | ||||
return sampled_from(ret) | return sampled_from(ret) | ||||
def new_origin(): | def new_origin(): | ||||
""" | """ | ||||
Hypothesis strategy returning a random origin not ingested | Hypothesis strategy returning a random origin not ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
return new_origin_strategy().filter( | return new_origin_strategy() | ||||
lambda origin: get_tests_data()["storage"].origin_get([origin.url])[0] is None | |||||
) | |||||
def new_origins(nb_origins=None): | def new_origins(nb_origins=None): | ||||
""" | """ | ||||
Hypothesis strategy returning random origins not ingested | Hypothesis strategy returning random origins not ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
min_size = nb_origins if nb_origins is not None else 2 | min_size = nb_origins if nb_origins is not None else 2 | ||||
▲ Show 20 Lines • Show All 301 Lines • Show Last 20 Lines |