Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_hypothesis_strategies.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 datetime | import datetime | ||||
import attr | import attr | ||||
from hypothesis import given, settings | from hypothesis import given, settings | ||||
from swh.model.hashutil import DEFAULT_ALGORITHMS | from swh.model.hashutil import DEFAULT_ALGORITHMS | ||||
from swh.model.hypothesis_strategies import objects, object_dicts, snapshots | from swh.model.hypothesis_strategies import ( | ||||
objects, | |||||
object_dicts, | |||||
contents, | |||||
skipped_contents, | |||||
snapshots, | |||||
origin_visits, | |||||
) | |||||
from swh.model.model import TargetType | from swh.model.model import TargetType | ||||
target_types = ("content", "directory", "revision", "release", "snapshot", "alias") | target_types = ("content", "directory", "revision", "release", "snapshot", "alias") | ||||
all_but_skipped_content = ( | all_but_skipped_content = ( | ||||
"origin", | "origin", | ||||
"origin_visit", | "origin_visit", | ||||
"origin_visit_status", | "origin_visit_status", | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | if obj_type == "content": | ||||
assert False, obj_dict | assert False, obj_dict | ||||
elif obj_type == "release": | elif obj_type == "release": | ||||
assert obj_dict["target_type"] in target_types | assert obj_dict["target_type"] in target_types | ||||
elif obj_type == "snapshot": | elif obj_type == "snapshot": | ||||
for branch in obj_dict["branches"].values(): | for branch in obj_dict["branches"].values(): | ||||
assert branch is None or branch["target_type"] in target_types | assert branch is None or branch["target_type"] in target_types | ||||
@given(contents()) | |||||
def test_content_aware_datetime(cont): | |||||
assert cont.ctime is None or cont.ctime.tzinfo is not None | |||||
@given(skipped_contents()) | |||||
def test_skipped_content_aware_datetime(cont): | |||||
assert cont.ctime is None or cont.ctime.tzinfo is not None | |||||
_min_snp_size = 10 | _min_snp_size = 10 | ||||
_max_snp_size = 100 | _max_snp_size = 100 | ||||
@given(snapshots(min_size=_min_snp_size, max_size=_max_snp_size)) | @given(snapshots(min_size=_min_snp_size, max_size=_max_snp_size)) | ||||
@settings(max_examples=1) | @settings(max_examples=1) | ||||
def test_snapshots_strategy(snapshot): | def test_snapshots_strategy(snapshot): | ||||
Show All 23 Lines | for alias in aliases: | ||||
processed_alias.add(current_alias) | processed_alias.add(current_alias) | ||||
current_alias = branches[current_alias].target | current_alias = branches[current_alias].target | ||||
@given(snapshots(min_size=_min_snp_size, max_size=_min_snp_size)) | @given(snapshots(min_size=_min_snp_size, max_size=_min_snp_size)) | ||||
@settings(max_examples=1) | @settings(max_examples=1) | ||||
def test_snapshots_strategy_fixed_size(snapshot): | def test_snapshots_strategy_fixed_size(snapshot): | ||||
assert len(snapshot.branches) == _min_snp_size | assert len(snapshot.branches) == _min_snp_size | ||||
@given(origin_visits()) | |||||
def test_origin_visit_aware_datetime(visit): | |||||
assert visit.date.tzinfo is not None |