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.hashutil import MultiHash, hash_to_bytes | from swh.model.hashutil import MultiHash, hash_to_bytes, hash_to_hex | ||||
import swh.model.hypothesis_strategies as strategies | import swh.model.hypothesis_strategies as strategies | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
SWHID, | SWHID, | ||||
content_identifier, | |||||
directory_identifier, | directory_identifier, | ||||
origin_identifier, | |||||
parse_swhid, | parse_swhid, | ||||
release_identifier, | release_identifier, | ||||
revision_identifier, | revision_identifier, | ||||
snapshot_identifier, | snapshot_identifier, | ||||
) | ) | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
BaseModel, | BaseModel, | ||||
Content, | Content, | ||||
Show All 11 Lines | from swh.model.model import ( | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
Timestamp, | Timestamp, | ||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
) | ) | ||||
from swh.model.tests.test_identifiers import ( | from swh.model.tests.test_identifiers import ( | ||||
content_example, | |||||
directory_example, | directory_example, | ||||
origin_example, | |||||
release_example, | release_example, | ||||
revision_example, | revision_example, | ||||
snapshot_example, | snapshot_example, | ||||
) | ) | ||||
@given(strategies.objects()) | @given(strategies.objects()) | ||||
def test_todict_inverse_fromdict(objtype_and_obj): | def test_todict_inverse_fromdict(objtype_and_obj): | ||||
▲ Show 20 Lines • Show All 641 Lines • ▼ Show 20 Lines | def test_revision_extra_headers_as_lists_from_dict(): | ||||
rev_model = Revision.from_dict(rev_dict) | rev_model = Revision.from_dict(rev_dict) | ||||
assert "extra_headers" not in rev_model.metadata | assert "extra_headers" not in rev_model.metadata | ||||
assert rev_model.extra_headers == extra_headers | assert rev_model.extra_headers == extra_headers | ||||
# ID computation | # ID computation | ||||
def test_content_model_id_computation(): | |||||
cnt_dict = content_example.copy() | |||||
cnt_id_str = hash_to_hex(content_identifier(cnt_dict)["sha1_git"]) | |||||
cnt_model = Content.from_data(cnt_dict["data"]) | |||||
assert str(cnt_model.swhid()) == "swh:1:cnt:" + cnt_id_str | |||||
def test_directory_model_id_computation(): | def test_directory_model_id_computation(): | ||||
dir_dict = directory_example.copy() | dir_dict = directory_example.copy() | ||||
del dir_dict["id"] | del dir_dict["id"] | ||||
dir_id = hash_to_bytes(directory_identifier(dir_dict)) | dir_id_str = directory_identifier(dir_dict) | ||||
dir_id = hash_to_bytes(dir_id_str) | |||||
dir_model = Directory.from_dict(dir_dict) | dir_model = Directory.from_dict(dir_dict) | ||||
assert dir_model.id == dir_id | assert dir_model.id == dir_id | ||||
assert str(dir_model.swhid()) == "swh:1:dir:" + dir_id_str | |||||
def test_revision_model_id_computation(): | def test_revision_model_id_computation(): | ||||
rev_dict = revision_example.copy() | rev_dict = revision_example.copy() | ||||
del rev_dict["id"] | del rev_dict["id"] | ||||
rev_id = hash_to_bytes(revision_identifier(rev_dict)) | rev_id_str = revision_identifier(rev_dict) | ||||
rev_id = hash_to_bytes(rev_id_str) | |||||
rev_model = Revision.from_dict(rev_dict) | rev_model = Revision.from_dict(rev_dict) | ||||
assert rev_model.id == rev_id | assert rev_model.id == rev_id | ||||
assert str(rev_model.swhid()) == "swh:1:rev:" + rev_id_str | |||||
def test_revision_model_id_computation_with_no_date(): | def test_revision_model_id_computation_with_no_date(): | ||||
"""We can have revision with date to None | """We can have revision with date to None | ||||
""" | """ | ||||
rev_dict = revision_example.copy() | rev_dict = revision_example.copy() | ||||
rev_dict["date"] = None | rev_dict["date"] = None | ||||
rev_dict["committer_date"] = None | rev_dict["committer_date"] = None | ||||
del rev_dict["id"] | del rev_dict["id"] | ||||
rev_id = hash_to_bytes(revision_identifier(rev_dict)) | rev_id = hash_to_bytes(revision_identifier(rev_dict)) | ||||
rev_model = Revision.from_dict(rev_dict) | rev_model = Revision.from_dict(rev_dict) | ||||
assert rev_model.date is None | assert rev_model.date is None | ||||
assert rev_model.committer_date is None | assert rev_model.committer_date is None | ||||
assert rev_model.id == rev_id | assert rev_model.id == rev_id | ||||
def test_release_model_id_computation(): | def test_release_model_id_computation(): | ||||
rel_dict = release_example.copy() | rel_dict = release_example.copy() | ||||
del rel_dict["id"] | del rel_dict["id"] | ||||
rel_id = hash_to_bytes(release_identifier(rel_dict)) | rel_id_str = release_identifier(rel_dict) | ||||
rel_id = hash_to_bytes(rel_id_str) | |||||
rel_model = Release.from_dict(rel_dict) | rel_model = Release.from_dict(rel_dict) | ||||
assert isinstance(rel_model.date, TimestampWithTimezone) | assert isinstance(rel_model.date, TimestampWithTimezone) | ||||
assert rel_model.id == hash_to_bytes(rel_id) | assert rel_model.id == hash_to_bytes(rel_id) | ||||
assert str(rel_model.swhid()) == "swh:1:rel:" + rel_id_str | |||||
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_str = snapshot_identifier(snp_dict) | ||||
snp_id = hash_to_bytes(snp_id_str) | |||||
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 | ||||
assert str(snp_model.swhid()) == "swh:1:snp:" + snp_id_str | |||||
def test_origin_model_id_computation(): | |||||
ori_dict = origin_example.copy() | |||||
ori_id_str = origin_identifier(ori_dict) | |||||
ori_model = Origin.from_dict(ori_dict) | |||||
assert str(ori_model.swhid()) == "swh:1:ori:" + ori_id_str | |||||
@given(strategies.objects(split_content=True)) | @given(strategies.objects(split_content=True)) | ||||
def test_object_type(objtype_and_obj): | def test_object_type(objtype_and_obj): | ||||
obj_type, obj = objtype_and_obj | obj_type, obj = objtype_and_obj | ||||
assert obj_type == obj.object_type | assert obj_type == obj.object_type | ||||
▲ Show 20 Lines • Show All 463 Lines • Show Last 20 Lines |