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 copy | import copy | ||||
import datetime | import datetime | ||||
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 | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
BaseModel, | |||||
Content, | Content, | ||||
SkippedContent, | SkippedContent, | ||||
Directory, | Directory, | ||||
Revision, | Revision, | ||||
Release, | Release, | ||||
Snapshot, | Snapshot, | ||||
Origin, | Origin, | ||||
Timestamp, | Timestamp, | ||||
▲ Show 20 Lines • Show All 439 Lines • ▼ Show 20 Lines | |||||
def test_snapshot_model_id_computation(): | def test_snapshot_model_id_computation(): | ||||
snp_dict = snapshot_example.copy() | snp_dict = snapshot_example.copy() | ||||
del snp_dict["id"] | del snp_dict["id"] | ||||
snp_id = hash_to_bytes(snapshot_identifier(snp_dict)) | snp_id = hash_to_bytes(snapshot_identifier(snp_dict)) | ||||
snp_model = Snapshot.from_dict(snp_dict) | snp_model = Snapshot.from_dict(snp_dict) | ||||
assert snp_model.id == snp_id | assert snp_model.id == snp_id | ||||
@given(strategies.objects(split_content=True)) | |||||
def test_object_type(objtype_and_obj): | |||||
obj_type, obj = objtype_and_obj | |||||
assert obj_type == obj.object_type | |||||
def test_object_type_is_final(): | |||||
object_types = set() | |||||
def check_final(cls): | |||||
if hasattr(cls, "object_type"): | |||||
assert cls.object_type not in object_types | |||||
object_types.add(cls.object_type) | |||||
if cls.__subclasses__(): | |||||
assert not hasattr(cls, "object_type") | |||||
for subcls in cls.__subclasses__(): | |||||
check_final(subcls) | |||||
check_final(BaseModel) |