Changeset View
Changeset View
Standalone View
Standalone View
swh/model/hypothesis_strategies.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019-2020 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 General Public License version 3, or any later version | # License: GNU 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 | ||||
import attr | import attr | ||||
import datetime | import datetime | ||||
from hypothesis.strategies import ( | from hypothesis.strategies import ( | ||||
binary, builds, characters, composite, dictionaries, | binary, builds, characters, composite, dictionaries, | ||||
from_regex, integers, just, lists, none, one_of, | from_regex, integers, just, lists, none, one_of, | ||||
sampled_from, sets, text, tuples, | sampled_from, sets, text, tuples, | ||||
) | ) | ||||
from .from_disk import DentryPerms | from .from_disk import DentryPerms | ||||
from .model import ( | from .model import ( | ||||
Person, Timestamp, TimestampWithTimezone, Origin, OriginVisit, | Person, Timestamp, TimestampWithTimezone, Origin, OriginVisit, | ||||
Snapshot, SnapshotBranch, TargetType, Release, Revision, | OriginVisitUpdate, Snapshot, SnapshotBranch, TargetType, Release, | ||||
Directory, DirectoryEntry, Content, SkippedContent | Revision, Directory, DirectoryEntry, Content, SkippedContent | ||||
) | ) | ||||
from .identifiers import snapshot_identifier, identifier_to_bytes | from .identifiers import snapshot_identifier, identifier_to_bytes | ||||
pgsql_alphabet = characters( | pgsql_alphabet = characters( | ||||
blacklist_categories=('Cs', ), | blacklist_categories=('Cs', ), | ||||
blacklist_characters=['\u0000']) # postgresql does not like these | blacklist_characters=['\u0000']) # postgresql does not like these | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | |||||
def origin_visits(): | def origin_visits(): | ||||
return builds( | return builds( | ||||
OriginVisit, | OriginVisit, | ||||
visit=integers(0, 1000), | visit=integers(0, 1000), | ||||
origin=urls(), | origin=urls(), | ||||
status=sampled_from(['ongoing', 'full', 'partial']), | status=sampled_from(['ongoing', 'full', 'partial']), | ||||
type=pgsql_text(), | type=pgsql_text(), | ||||
snapshot=optional(sha1_git())) | snapshot=optional(sha1_git()), | ||||
) | |||||
vlorentz: plural, for consistency | |||||
def metadata_dicts(): | |||||
return dictionaries(pgsql_text(), pgsql_text()) | |||||
def origin_visit_updates(): | |||||
return builds( | |||||
OriginVisitUpdate, | |||||
visit=integers(0, 1000), | |||||
origin=urls(), | |||||
status=sampled_from(['ongoing', 'full', 'partial']), | |||||
snapshot=optional(sha1_git()), | |||||
metadata=one_of(none(), metadata_dicts())) | |||||
@composite | @composite | ||||
def releases(draw): | def releases(draw): | ||||
(date, author) = draw(one_of( | (date, author) = draw(one_of( | ||||
tuples(none(), none()), | tuples(none(), none()), | ||||
tuples(timestamps_with_timezone(), persons()))) | tuples(timestamps_with_timezone(), persons()))) | ||||
rel = draw(builds( | rel = draw(builds( | ||||
Release, | Release, | ||||
author=none(), | author=none(), | ||||
date=none(), | date=none(), | ||||
target=sha1_git())) | target=sha1_git())) | ||||
return attr.evolve( | return attr.evolve( | ||||
rel, | rel, | ||||
date=date, | date=date, | ||||
author=author) | author=author) | ||||
def revision_metadata(): | revision_metadata = metadata_dicts | ||||
return dictionaries(pgsql_text(), pgsql_text()) | |||||
def revisions(): | def revisions(): | ||||
return builds( | return builds( | ||||
Revision, | Revision, | ||||
author=persons(), | author=persons(), | ||||
committer=persons(), | committer=persons(), | ||||
date=timestamps_with_timezone(), | date=timestamps_with_timezone(), | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | return Snapshot( | ||||
id=identifier_to_bytes(id_), | id=identifier_to_bytes(id_), | ||||
branches=branches) | branches=branches) | ||||
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)), | |||||
snapshots().map(lambda x: ('snapshot', x)), | snapshots().map(lambda x: ('snapshot', x)), | ||||
releases().map(lambda x: ('release', x)), | releases().map(lambda x: ('release', x)), | ||||
revisions().map(lambda x: ('revision', x)), | revisions().map(lambda x: ('revision', x)), | ||||
directories().map(lambda x: ('directory', x)), | directories().map(lambda x: ('directory', x)), | ||||
contents().map(lambda x: ('content', x)), | contents().map(lambda x: ('content', x)), | ||||
) | ) | ||||
def object_dicts(): | def object_dicts(): | ||||
return objects().map(lambda x: (x[0], x[1].to_dict())) | return objects().map(lambda x: (x[0], x[1].to_dict())) |
plural, for consistency