Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
# Copyright (C) 2019-2020 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 collections | import collections | ||||
import copy | import copy | ||||
import datetime | import datetime | ||||
from typing import Any, List, Optional, Tuple, Union | from typing import Any, List, Optional, Tuple, Union | ||||
import attr | import attr | ||||
from attrs_strict import AttributeTypeError | from attrs_strict import AttributeTypeError | ||||
from hypothesis import given | from hypothesis import given | ||||
from hypothesis.strategies import binary | from hypothesis.strategies import binary | ||||
import pytest | import pytest | ||||
import zoneinfo | |||||
from swh.model.collections import ImmutableDict | from swh.model.collections import ImmutableDict | ||||
from swh.model.from_disk import DentryPerms | from swh.model.from_disk import DentryPerms | ||||
from swh.model.hashutil import MultiHash, hash_to_bytes | from swh.model.hashutil import MultiHash, hash_to_bytes | ||||
import swh.model.hypothesis_strategies as strategies | import swh.model.hypothesis_strategies as strategies | ||||
import swh.model.model | |||||
from swh.model.model import ( | from swh.model.model import ( | ||||
BaseModel, | BaseModel, | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
MetadataAuthority, | MetadataAuthority, | ||||
MetadataAuthorityType, | MetadataAuthorityType, | ||||
MetadataFetcher, | MetadataFetcher, | ||||
MissingData, | MissingData, | ||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
Person, | Person, | ||||
RawExtrinsicMetadata, | RawExtrinsicMetadata, | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
TargetType, | TargetType, | ||||
Timestamp, | Timestamp, | ||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
type_validator, | type_validator, | ||||
) | ) | ||||
import swh.model.swhids | |||||
from swh.model.swhids import CoreSWHID, ExtendedSWHID, ObjectType | from swh.model.swhids import CoreSWHID, ExtendedSWHID, ObjectType | ||||
from swh.model.tests.swh_model_data import TEST_OBJECTS | from swh.model.tests.swh_model_data import TEST_OBJECTS | ||||
from swh.model.tests.test_identifiers import ( | from swh.model.tests.test_identifiers import ( | ||||
TS_DATETIMES, | TS_DATETIMES, | ||||
TS_TIMEZONES, | TS_TIMEZONES, | ||||
directory_example, | directory_example, | ||||
metadata_example, | metadata_example, | ||||
release_example, | release_example, | ||||
Show All 19 Lines | def test_todict_inverse_fromdict(objtype_and_obj): | ||||
# Check from_dict() does not change the input dict | # Check from_dict() does not change the input dict | ||||
assert obj_as_dict == obj_as_dict_copy | assert obj_as_dict == obj_as_dict_copy | ||||
# Check the composition of from_dict and to_dict is the identity | # Check the composition of from_dict and to_dict is the identity | ||||
assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict() | assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict() | ||||
@given(strategies.objects()) | |||||
def test_repr(objtype_and_obj): | |||||
"""Checks every model object has a working repr(), and that it can be eval()uated | |||||
(so that printed objects can be copy-pasted to write test cases.)""" | |||||
(obj_type, obj) = objtype_and_obj | |||||
r = repr(obj) | |||||
env = { | |||||
"tzutc": lambda: datetime.timezone.utc, | |||||
"tzfile": lambda path: zoneinfo.ZoneInfo.from_file(open(path, "rb")), | |||||
"hash_to_bytes": hash_to_bytes, | |||||
**swh.model.swhids.__dict__, | |||||
**swh.model.model.__dict__, | |||||
} | |||||
try: | |||||
obj2 = eval(r, env) | |||||
except Exception: | |||||
raise | |||||
assert obj2 == obj | |||||
@attr.s | @attr.s | ||||
class Cls1: | class Cls1: | ||||
pass | pass | ||||
@attr.s | @attr.s | ||||
class Cls2(Cls1): | class Cls2(Cls1): | ||||
pass | pass | ||||
▲ Show 20 Lines • Show All 1,187 Lines • Show Last 20 Lines |