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-2022 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 | ||||
import hashlib | import hashlib | ||||
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 | ||||
import dateutil | import dateutil | ||||
from hypothesis import given | from hypothesis import given | ||||
from hypothesis.strategies import binary, none | from hypothesis.strategies import binary, none | ||||
import pytest | import pytest | ||||
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 | ||||
import swh.model.git_objects | import swh.model.git_objects | ||||
from swh.model.hashutil import MultiHash, hash_to_bytes | from swh.model.hashutil import ALGORITHMS, MultiHash, hash_to_bytes | ||||
import swh.model.hypothesis_strategies as strategies | import swh.model.hypothesis_strategies as strategies | ||||
import swh.model.model | import swh.model.model | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
BaseModel, | BaseModel, | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
DirectoryEntry, | DirectoryEntry, | ||||
MetadataAuthority, | MetadataAuthority, | ||||
▲ Show 20 Lines • Show All 712 Lines • ▼ Show 20 Lines | |||||
def test_content_get_hash(): | def test_content_get_hash(): | ||||
hashes = dict(sha1=b"foo", sha1_git=b"bar", sha256=b"baz", blake2s256=b"qux") | hashes = dict(sha1=b"foo", sha1_git=b"bar", sha256=b"baz", blake2s256=b"qux") | ||||
c = Content(length=42, status="visible", **hashes) | c = Content(length=42, status="visible", **hashes) | ||||
for (hash_name, hash_) in hashes.items(): | for (hash_name, hash_) in hashes.items(): | ||||
assert c.get_hash(hash_name) == hash_ | assert c.get_hash(hash_name) == hash_ | ||||
with pytest.raises(ValueError, match="not a valid"): | |||||
c.get_hash("inexisting-hash-algo") | |||||
for hash_name in ALGORITHMS - set(hashes.keys()): | |||||
with pytest.raises(ValueError, match="do not store"): | |||||
c.get_hash(hash_name) | |||||
def test_content_hashes(): | def test_content_hashes(): | ||||
hashes = dict(sha1=b"foo", sha1_git=b"bar", sha256=b"baz", blake2s256=b"qux") | hashes = dict(sha1=b"foo", sha1_git=b"bar", sha256=b"baz", blake2s256=b"qux") | ||||
c = Content(length=42, status="visible", **hashes) | c = Content(length=42, status="visible", **hashes) | ||||
assert c.hashes() == hashes | assert c.hashes() == hashes | ||||
def test_content_data(): | def test_content_data(): | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | content_d = { | ||||
"blake2s256": b"\x00", | "blake2s256": b"\x00", | ||||
} | } | ||||
with pytest.raises(ValueError, match="must be a timezone-aware datetime."): | with pytest.raises(ValueError, match="must be a timezone-aware datetime."): | ||||
Content.from_dict(content_d) | Content.from_dict(content_d) | ||||
@given(binary(max_size=4096)) | @given(binary(max_size=4096)) | ||||
def test_content_from_data(data): | def test_content_from_data(data): | ||||
c = Content.from_data(data) | c = Content.from_data(data, hash_names=ALGORITHMS) | ||||
assert c.data == data | assert c.data == data | ||||
assert c.length == len(data) | assert c.length == len(data) | ||||
assert c.status == "visible" | assert c.status == "visible" | ||||
for key, value in MultiHash.from_data(data).digest().items(): | for key, value in MultiHash.from_data(data, hash_names=ALGORITHMS).digest().items(): | ||||
assert getattr(c, key) == value | assert getattr(c, key) == value | ||||
@given(binary(max_size=4096)) | @given(binary(max_size=4096)) | ||||
def test_hidden_content_from_data(data): | def test_hidden_content_from_data(data): | ||||
c = Content.from_data(data, status="hidden") | c = Content.from_data(data, status="hidden") | ||||
assert c.data == data | assert c.data == data | ||||
assert c.length == len(data) | assert c.length == len(data) | ||||
▲ Show 20 Lines • Show All 992 Lines • Show Last 20 Lines |