Changeset View
Changeset View
Standalone View
Standalone View
swh/model/hypothesis_strategies.py
Show First 20 Lines • Show All 309 Lines • ▼ Show 20 Lines | branches = draw(dictionaries( | ||||
), | ), | ||||
min_size=min_size, | min_size=min_size, | ||||
max_size=max_size, | max_size=max_size, | ||||
)) | )) | ||||
if not only_objects: | if not only_objects: | ||||
# Make sure aliases point to actual branches | # Make sure aliases point to actual branches | ||||
unresolved_aliases = { | unresolved_aliases = { | ||||
target['target'] | branch: target['target'] | ||||
for target in branches.values() | for branch, target in branches.items() | ||||
if (target | if (target | ||||
and target['target_type'] == 'alias' | and target['target_type'] == 'alias' | ||||
and target['target'] not in branches) | and target['target'] not in branches) | ||||
} | } | ||||
for alias in unresolved_aliases: | for alias_name, alias_target in unresolved_aliases.items(): | ||||
# Override alias branch with one pointing to a real object | |||||
# if max_size constraint is reached | |||||
alias = alias_target if len(branches) < max_size else alias_name | |||||
branches[alias] = draw(branch_targets_d(only_objects=True)) | branches[alias] = draw(branch_targets_d(only_objects=True)) | ||||
# Ensure no cycles between aliases | # Ensure no cycles between aliases | ||||
while True: | while True: | ||||
try: | try: | ||||
id_ = snapshot_identifier({ | id_ = snapshot_identifier({ | ||||
'branches': { | 'branches': { | ||||
name: branch or None | name: branch or None | ||||
for (name, branch) in branches.items()}}) | for (name, branch) in branches.items()}}) | ||||
except ValueError as e: | except ValueError as e: | ||||
for (source, target) in e.args[1]: | for (source, target) in e.args[1]: | ||||
branches[source] = draw(branch_targets_d(only_objects=True)) | branches[source] = draw(branch_targets_d(only_objects=True)) | ||||
else: | else: | ||||
break | break | ||||
return dict( | return dict( | ||||
id=identifier_to_bytes(id_), | id=identifier_to_bytes(id_), | ||||
branches=branches) | branches=branches) | ||||
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(min_size=0, max_size=100, only_objects=False).map( | return snapshots_d(min_size=min_size, max_size=max_size, | ||||
only_objects=only_objects).map( | |||||
Snapshot.from_dict) | Snapshot.from_dict) | ||||
def objects(): | def objects(): | ||||
return one_of( | return one_of( | ||||
origins().map(lambda x: ('origin', x)), | origins().map(lambda x: ('origin', x)), | ||||
origin_visits().map(lambda x: ('origin_visit', x)), | origin_visits().map(lambda x: ('origin_visit', x)), | ||||
origin_visit_updates().map(lambda x: ('origin_visit_update', x)), | origin_visit_updates().map(lambda x: ('origin_visit_update', x)), | ||||
Show All 22 Lines |