Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/strategies.py
# Copyright (C) 2018-2021 The Software Heritage developers | # Copyright (C) 2018-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime | from datetime import datetime | ||||
import random | import random | ||||
from hypothesis import assume, settings | from hypothesis import assume, settings | ||||
from hypothesis.extra.dateutil import timezones | from hypothesis.extra.dateutil import timezones | ||||
from hypothesis.strategies import ( | from hypothesis.strategies import binary, characters, composite, datetimes, lists, text | ||||
binary, | |||||
characters, | |||||
composite, | |||||
datetimes, | |||||
lists, | |||||
sampled_from, | |||||
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 Person, Revision, RevisionType, TimestampWithTimezone | from swh.model.model import Person, Revision, RevisionType, TimestampWithTimezone | ||||
from swh.model.swhids import ObjectType | |||||
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 | ||||
# in the swh.web.tests.data module. | # in the swh.web.tests.data module. | ||||
# Set the swh-web hypothesis profile if none has been explicitly set | # Set the swh-web hypothesis profile if none has been explicitly set | ||||
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 | |||||
def _known_swh_object(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).map(hash_to_hex) | return binary(min_size=20, max_size=20).map(hash_to_hex) | ||||
def invalid_sha1(): | def invalid_sha1(): | ||||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Lines | |||||
def unknown_revisions(min_size=2, max_size=8): | def unknown_revisions(min_size=2, max_size=8): | ||||
""" | """ | ||||
Hypothesis strategy returning random revisions not ingested | Hypothesis strategy returning random revisions not ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
return lists(unknown_revision(), min_size=min_size, max_size=max_size) | return lists(unknown_revision(), min_size=min_size, max_size=max_size) | ||||
def snapshot(): | |||||
""" | |||||
Hypothesis strategy returning a random snapshot ingested | |||||
into the test archive. | |||||
""" | |||||
return _known_swh_object("snapshots") | |||||
def new_snapshots(nb_snapshots=None): | def new_snapshots(nb_snapshots=None): | ||||
min_size = nb_snapshots if nb_snapshots else 2 | min_size = nb_snapshots if nb_snapshots else 2 | ||||
max_size = nb_snapshots if nb_snapshots else 8 | max_size = nb_snapshots if nb_snapshots else 8 | ||||
return lists( | return lists( | ||||
new_snapshot(min_size=2, max_size=10, only_objects=True), | new_snapshot(min_size=2, max_size=10, only_objects=True), | ||||
min_size=min_size, | min_size=min_size, | ||||
max_size=max_size, | max_size=max_size, | ||||
) | ) | ||||
def unknown_snapshot(): | def unknown_snapshot(): | ||||
""" | """ | ||||
Hypothesis strategy returning a random revision not ingested | Hypothesis strategy returning a random revision not ingested | ||||
into the test archive. | into the test archive. | ||||
""" | """ | ||||
return sha1().filter( | return sha1().filter( | ||||
lambda s: get_tests_data()["storage"].snapshot_get_branches(hash_to_bytes(s)) | lambda s: get_tests_data()["storage"].snapshot_get_branches(hash_to_bytes(s)) | ||||
is None | is None | ||||
) | ) | ||||
def swhid(): | |||||
""" | |||||
Hypothesis strategy returning a qualified SWHID for any object | |||||
ingested into the test archive. | |||||
""" | |||||
return _known_swh_object("swhids") | |||||
def snapshot_swhid(): | |||||
""" | |||||
Hypothesis strategy returning a qualified SWHID for a snapshot object | |||||
ingested into the test archive. | |||||
""" | |||||
return swhid().filter(lambda swhid: swhid.object_type == ObjectType.SNAPSHOT) |