Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.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 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 ( | ||||
Content, SkippedContent, Directory, Revision, Release, Snapshot, | Content, SkippedContent, Directory, Revision, Release, Snapshot, | ||||
Timestamp, TimestampWithTimezone, | Origin, Timestamp, TimestampWithTimezone, | ||||
MissingData, Person | MissingData, Person | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes, MultiHash | from swh.model.hashutil import hash_to_bytes, MultiHash | ||||
from swh.model.hypothesis_strategies import timestamps | from swh.model.hypothesis_strategies import timestamps, skipped_contents_d | ||||
from swh.model.hypothesis_strategies import objects, origins, origin_visits | from swh.model.hypothesis_strategies import objects, origins, origin_visits | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
directory_identifier, revision_identifier, release_identifier, | directory_identifier, revision_identifier, release_identifier, | ||||
snapshot_identifier | snapshot_identifier | ||||
) | ) | ||||
from swh.model.tests.test_identifiers import ( | from swh.model.tests.test_identifiers import ( | ||||
directory_example, revision_example, release_example, snapshot_example | directory_example, revision_example, release_example, snapshot_example | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 341 Lines • ▼ Show 20 Lines | def test_skipped_content_from_data(data): | ||||
c = SkippedContent.from_data(data, reason='reason') | c = SkippedContent.from_data(data, reason='reason') | ||||
assert c.reason == 'reason' | assert c.reason == 'reason' | ||||
assert c.length == len(data) | assert c.length == len(data) | ||||
assert c.status == 'absent' | assert c.status == 'absent' | ||||
for key, value in MultiHash.from_data(data).digest().items(): | for key, value in MultiHash.from_data(data).digest().items(): | ||||
assert getattr(c, key) == value | assert getattr(c, key) == value | ||||
@given(skipped_contents_d()) | |||||
def test_skipped_content_origin_is_str(skipped_content_d): | |||||
assert SkippedContent.from_dict(skipped_content_d) | |||||
skipped_content_d['origin'] = 'http://path/to/origin' | |||||
assert SkippedContent.from_dict(skipped_content_d) | |||||
skipped_content_d['origin'] = Origin(url='http://path/to/origin') | |||||
with pytest.raises(ValueError, match='origin'): | |||||
SkippedContent.from_dict(skipped_content_d) | |||||
# ID computation | |||||
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 = hash_to_bytes(directory_identifier(dir_dict)) | ||||
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 | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |