Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/strategies.py
Show All 17 Lines | from hypothesis.strategies import ( | ||||
lists, | lists, | ||||
sampled_from, | sampled_from, | ||||
text, | text, | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
from swh.model.hypothesis_strategies import origins as new_origin_strategy | from swh.model.hypothesis_strategies import origins as new_origin_strategy | ||||
from swh.model.hypothesis_strategies import snapshots as new_snapshot | from swh.model.hypothesis_strategies import snapshots as new_snapshot | ||||
from swh.model.model import ( | from swh.model.model import Person, Revision, RevisionType, TimestampWithTimezone | ||||
Directory, | |||||
Person, | |||||
Revision, | |||||
RevisionType, | |||||
TimestampWithTimezone, | |||||
) | |||||
from swh.model.swhids import ObjectType | from swh.model.swhids import ObjectType | ||||
from swh.storage.algos.revisions_walker import get_revisions_walker | from swh.storage.algos.revisions_walker import get_revisions_walker | ||||
from swh.storage.algos.snapshot import snapshot_get_latest | from swh.storage.algos.snapshot import snapshot_get_latest | ||||
from swh.web.tests.data import get_tests_data | from swh.web.tests.data import get_tests_data | ||||
# Module dedicated to the generation of input data for tests through | # Module dedicated to the generation of input data for tests through | ||||
# the use of hypothesis. | # the use of hypothesis. | ||||
# Some of these data are sampled from a test archive created and populated | # Some of these data are sampled from a test archive created and populated | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | |||||
def unknown_contents(): | def unknown_contents(): | ||||
""" | """ | ||||
Hypothesis strategy returning random contents not ingested | Hypothesis strategy returning random contents not ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
return lists(unknown_content(), min_size=2, max_size=8) | return lists(unknown_content(), min_size=2, max_size=8) | ||||
def directory(): | |||||
""" | |||||
Hypothesis strategy returning a random directory ingested | |||||
into the test archive. | |||||
""" | |||||
return _known_swh_object("directories") | |||||
def _directory_with_entry_type(type_): | |||||
return directory().filter( | |||||
lambda d: any( | |||||
[ | |||||
e["type"] == type_ | |||||
for e in list( | |||||
get_tests_data()["storage"].directory_ls(hash_to_bytes(d)) | |||||
) | |||||
] | |||||
) | |||||
) | |||||
def directory_with_subdirs(): | |||||
""" | |||||
Hypothesis strategy returning a random directory containing | |||||
sub directories ingested into the test archive. | |||||
""" | |||||
return _directory_with_entry_type("dir") | |||||
def directory_with_files(): | |||||
""" | |||||
Hypothesis strategy returning a random directory containing | |||||
at least one regular file | |||||
""" | |||||
return _directory_with_entry_type("file") | |||||
def empty_directory(): | |||||
""" | |||||
Hypothesis strategy returning the empty directory ingested | |||||
into the test archive. | |||||
""" | |||||
return just(Directory(entries=()).id.hex()) | |||||
def unknown_directory(): | def unknown_directory(): | ||||
""" | """ | ||||
Hypothesis strategy returning a random directory not ingested | Hypothesis strategy returning a random directory not ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
return sha1().filter( | return sha1().filter( | ||||
lambda s: len( | lambda s: len( | ||||
list(get_tests_data()["storage"].directory_missing([hash_to_bytes(s)])) | list(get_tests_data()["storage"].directory_missing([hash_to_bytes(s)])) | ||||
▲ Show 20 Lines • Show All 344 Lines • ▼ Show 20 Lines | |||||
def swhid(): | def swhid(): | ||||
""" | """ | ||||
Hypothesis strategy returning a qualified SWHID for any object | Hypothesis strategy returning a qualified SWHID for any object | ||||
ingested into the test archive. | ingested into the test archive. | ||||
""" | """ | ||||
return _known_swh_object("swhids") | return _known_swh_object("swhids") | ||||
def directory_swhid(): | |||||
""" | |||||
Hypothesis strategy returning a qualified SWHID for a directory object | |||||
ingested into the test archive. | |||||
""" | |||||
return swhid().filter(lambda swhid: swhid.object_type == ObjectType.DIRECTORY) | |||||
def release_swhid(): | def release_swhid(): | ||||
""" | """ | ||||
Hypothesis strategy returning a qualified SWHID for a release object | Hypothesis strategy returning a qualified SWHID for a release object | ||||
ingested into the test archive. | ingested into the test archive. | ||||
""" | """ | ||||
return swhid().filter(lambda swhid: swhid.object_type == ObjectType.RELEASE) | return swhid().filter(lambda swhid: swhid.object_type == ObjectType.RELEASE) | ||||
Show All 14 Lines |