Changeset View
Changeset View
Standalone View
Standalone View
swh/model/hypothesis_strategies.py
Show All 22 Lines | from hypothesis.strategies import ( | ||||
one_of, | one_of, | ||||
sampled_from, | sampled_from, | ||||
sets, | sets, | ||||
text, | text, | ||||
tuples, | tuples, | ||||
) | ) | ||||
from .from_disk import DentryPerms | from .from_disk import DentryPerms | ||||
from .identifiers import identifier_to_bytes, snapshot_identifier | from .identifiers import ( | ||||
ExtendedObjectType, | |||||
ExtendedSWHID, | |||||
identifier_to_bytes, | |||||
snapshot_identifier, | |||||
) | |||||
from .model import ( | from .model import ( | ||||
BaseContent, | BaseContent, | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
DirectoryEntry, | DirectoryEntry, | ||||
MetadataAuthority, | |||||
MetadataFetcher, | |||||
ObjectType, | ObjectType, | ||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
Person, | Person, | ||||
RawExtrinsicMetadata, | |||||
Release, | Release, | ||||
Revision, | Revision, | ||||
RevisionType, | RevisionType, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
SnapshotBranch, | SnapshotBranch, | ||||
TargetType, | TargetType, | ||||
Timestamp, | Timestamp, | ||||
Show All 16 Lines | |||||
def sha1_git(): | def sha1_git(): | ||||
return binary(min_size=20, max_size=20) | return binary(min_size=20, max_size=20) | ||||
def sha1(): | def sha1(): | ||||
return binary(min_size=20, max_size=20) | return binary(min_size=20, max_size=20) | ||||
@composite | |||||
def extended_swhids(draw): | |||||
object_type = draw(sampled_from(ExtendedObjectType)) | |||||
object_id = draw(sha1_git()) | |||||
return ExtendedSWHID(object_type=object_type, object_id=object_id) | |||||
def aware_datetimes(): | def aware_datetimes(): | ||||
# datetimes in Software Heritage are not used for software artifacts | # datetimes in Software Heritage are not used for software artifacts | ||||
# (which may be much older than 2000), but only for objects like scheduler | # (which may be much older than 2000), but only for objects like scheduler | ||||
# task runs, and origin visits, which were created by Software Heritage, | # task runs, and origin visits, which were created by Software Heritage, | ||||
# so at least in 2015. | # so at least in 2015. | ||||
# We're forbidding old datetimes, because until 1956, many timezones had seconds | # We're forbidding old datetimes, because until 1956, many timezones had seconds | ||||
# in their "UTC offsets" (see | # in their "UTC offsets" (see | ||||
# <https://en.wikipedia.org/wiki/Time_zone#Worldwide_time_zones>), which is not | # <https://en.wikipedia.org/wiki/Time_zone#Worldwide_time_zones>), which is not | ||||
▲ Show 20 Lines • Show All 319 Lines • ▼ Show 20 Lines | |||||
def snapshots(*, min_size=0, max_size=100, only_objects=False): | def snapshots(*, min_size=0, max_size=100, only_objects=False): | ||||
return snapshots_d( | return snapshots_d( | ||||
min_size=min_size, max_size=max_size, only_objects=only_objects | min_size=min_size, max_size=max_size, only_objects=only_objects | ||||
).map(Snapshot.from_dict) | ).map(Snapshot.from_dict) | ||||
def metadata_authorities(): | |||||
return builds(MetadataAuthority, url=urls(), metadata=just({})) | |||||
def metadata_fetchers(): | |||||
return builds(MetadataFetcher, metadata=just({})) | |||||
def raw_extrinsic_metadata(): | |||||
return builds( | |||||
RawExtrinsicMetadata, | |||||
target=extended_swhids(), | |||||
discovery_date=aware_datetimes(), | |||||
authority=metadata_authorities(), | |||||
fetcher=metadata_fetchers(), | |||||
) | |||||
def raw_extrinsic_metadata_d(): | |||||
return raw_extrinsic_metadata().map(RawExtrinsicMetadata.to_dict) | |||||
def objects(blacklist_types=("origin_visit_status",), split_content=False): | def objects(blacklist_types=("origin_visit_status",), split_content=False): | ||||
"""generates a random couple (type, obj) | """generates a random couple (type, obj) | ||||
which obj is an instance of the Model class corresponding to obj_type. | which obj is an instance of the Model class corresponding to obj_type. | ||||
`blacklist_types` is a list of obj_type to exclude from the strategy. | `blacklist_types` is a list of obj_type to exclude from the strategy. | ||||
If `split_content` is True, generates Content and SkippedContent under different | If `split_content` is True, generates Content and SkippedContent under different | ||||
obj_type, resp. "content" and "skipped_content". | obj_type, resp. "content" and "skipped_content". | ||||
""" | """ | ||||
strategies = [ | strategies = [ | ||||
("origin", origins), | ("origin", origins), | ||||
("origin_visit", origin_visits), | ("origin_visit", origin_visits), | ||||
("origin_visit_status", origin_visit_statuses), | ("origin_visit_status", origin_visit_statuses), | ||||
("snapshot", snapshots), | ("snapshot", snapshots), | ||||
("release", releases), | ("release", releases), | ||||
("revision", revisions), | ("revision", revisions), | ||||
("directory", directories), | ("directory", directories), | ||||
("raw_extrinsic_metadata", raw_extrinsic_metadata), | |||||
] | ] | ||||
if split_content: | if split_content: | ||||
strategies.append(("content", present_contents)) | strategies.append(("content", present_contents)) | ||||
strategies.append(("skipped_content", skipped_contents)) | strategies.append(("skipped_content", skipped_contents)) | ||||
else: | else: | ||||
strategies.append(("content", contents)) | strategies.append(("content", contents)) | ||||
args = [ | args = [ | ||||
obj_gen().map(lambda x, obj_type=obj_type: (obj_type, x)) | obj_gen().map(lambda x, obj_type=obj_type: (obj_type, x)) | ||||
Show All 17 Lines | def object_dicts(blacklist_types=("origin_visit_status",), split_content=False): | ||||
strategies = [ | strategies = [ | ||||
("origin", origins_d), | ("origin", origins_d), | ||||
("origin_visit", origin_visits_d), | ("origin_visit", origin_visits_d), | ||||
("origin_visit_status", origin_visit_statuses_d), | ("origin_visit_status", origin_visit_statuses_d), | ||||
("snapshot", snapshots_d), | ("snapshot", snapshots_d), | ||||
("release", releases_d), | ("release", releases_d), | ||||
("revision", revisions_d), | ("revision", revisions_d), | ||||
("directory", directories_d), | ("directory", directories_d), | ||||
("raw_extrinsic_metadata", raw_extrinsic_metadata_d), | |||||
] | ] | ||||
if split_content: | if split_content: | ||||
strategies.append(("content", present_contents_d)) | strategies.append(("content", present_contents_d)) | ||||
strategies.append(("skipped_content", skipped_contents_d)) | strategies.append(("skipped_content", skipped_contents_d)) | ||||
else: | else: | ||||
strategies.append(("content", contents_d)) | strategies.append(("content", contents_d)) | ||||
args = [ | args = [ | ||||
obj_gen().map(lambda x, obj_type=obj_type: (obj_type, x)) | obj_gen().map(lambda x, obj_type=obj_type: (obj_type, x)) | ||||
for (obj_type, obj_gen) in strategies | for (obj_type, obj_gen) in strategies | ||||
if obj_type not in blacklist_types | if obj_type not in blacklist_types | ||||
] | ] | ||||
return one_of(*args) | return one_of(*args) |