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 | ||||
from hypothesis import given | from hypothesis import given | ||||
from hypothesis.strategies import binary | |||||
import pytest | import pytest | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Content, Directory, Revision, Release, Snapshot, | Content, SkippedContent, Directory, Revision, Release, Snapshot, | ||||
Timestamp, TimestampWithTimezone, | Timestamp, TimestampWithTimezone, | ||||
MissingData, | MissingData, | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes, MultiHash | ||||
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 106 Lines • ▼ Show 20 Lines | |||||
def test_content_data_missing(): | def test_content_data_missing(): | ||||
c = Content( | c = Content( | ||||
length=42, status='visible', | length=42, status='visible', | ||||
sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux') | sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux') | ||||
with pytest.raises(MissingData): | with pytest.raises(MissingData): | ||||
c.with_data() | c.with_data() | ||||
@given(binary(max_size=4096)) | |||||
def test_content_from_data(data): | |||||
c = Content.from_data(data) | |||||
assert c.data == data | |||||
assert c.length == len(data) | |||||
assert c.status == 'visible' | |||||
for key, value in MultiHash.from_data(data).digest().items(): | |||||
assert getattr(c, key) == value | |||||
@given(binary(max_size=4096)) | |||||
def test_hidden_content_from_data(data): | |||||
c = Content.from_data(data, status='hidden') | |||||
assert c.data == data | |||||
assert c.length == len(data) | |||||
assert c.status == 'hidden' | |||||
for key, value in MultiHash.from_data(data).digest().items(): | |||||
assert getattr(c, key) == value | |||||
@given(binary(max_size=4096)) | |||||
def test_skipped_content_from_data(data): | |||||
c = SkippedContent.from_data(data, reason='reason') | |||||
assert c.reason == 'reason' | |||||
assert c.length == len(data) | |||||
assert c.status == 'absent' | |||||
for key, value in MultiHash.from_data(data).digest().items(): | |||||
assert getattr(c, key) == value | |||||
def test_directory_model_id_computation(): | def test_directory_model_id_computation(): | ||||
dir_dict = dict(directory_example) | dir_dict = dict(directory_example) | ||||
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)) | ||||
for dir_model in [Directory(**dir_dict), Directory.from_dict(dir_dict)]: | for dir_model in [Directory(**dir_dict), Directory.from_dict(dir_dict)]: | ||||
assert dir_model.id == dir_id | assert dir_model.id == dir_id | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |