Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_hypothesis_strategies.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2022 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 | ||||
import iso8601 | import iso8601 | ||||
from swh.model.hashutil import DEFAULT_ALGORITHMS | from swh.model.hashutil import ALGORITHMS, DEFAULT_ALGORITHMS | ||||
from swh.model.hypothesis_strategies import ( | from swh.model.hypothesis_strategies import ( | ||||
aware_datetimes, | aware_datetimes, | ||||
contents, | contents, | ||||
object_dicts, | object_dicts, | ||||
objects, | objects, | ||||
origin_visits, | origin_visits, | ||||
persons, | persons, | ||||
skipped_contents, | skipped_contents, | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | |||||
@given(objects()) | @given(objects()) | ||||
def test_model_to_dicts(obj_type_and_obj): | def test_model_to_dicts(obj_type_and_obj): | ||||
_, object_ = obj_type_and_obj | _, object_ = obj_type_and_obj | ||||
object_type = object_.object_type | object_type = object_.object_type | ||||
obj_dict = object_.to_dict() | obj_dict = object_.to_dict() | ||||
assert_nested_dict(obj_dict) | assert_nested_dict(obj_dict) | ||||
if object_type in ("content", "skipped_content"): | if object_type in ("content", "skipped_content"): | ||||
COMMON_KEYS = set(DEFAULT_ALGORITHMS) | {"length", "status"} | COMMON_KEYS = set(ALGORITHMS) | {"length", "status"} | ||||
if object_.ctime is not None: | if object_.ctime is not None: | ||||
COMMON_KEYS |= {"ctime"} | COMMON_KEYS |= {"ctime"} | ||||
if obj_dict["status"] == "visible": | if obj_dict["status"] == "visible": | ||||
assert set(obj_dict) == COMMON_KEYS | {"data"} | assert set(obj_dict) == COMMON_KEYS | {"data"} | ||||
elif obj_dict["status"] == "absent": | elif obj_dict["status"] == "absent": | ||||
assert set(obj_dict) == COMMON_KEYS | {"reason"} | assert set(obj_dict) == COMMON_KEYS | {"reason"} | ||||
elif obj_dict["status"] == "hidden": | elif obj_dict["status"] == "hidden": | ||||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |