Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/tests/test_loader.py
# Copyright (C) 2019-2021 The Software Heritage developers | # Copyright (C) 2019-2021 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 hashlib | import hashlib | ||||
import string | import string | ||||
from unittest.mock import MagicMock | |||||
import attr | import attr | ||||
import pytest | import pytest | ||||
from swh.loader.package.loader import BasePackageInfo, PackageLoader | from swh.loader.package.loader import BaseManifestPackageInfo, PackageLoader | ||||
class FakeStorage: | class FakeStorage: | ||||
def origin_add(self, origins): | def origin_add(self, origins): | ||||
raise ValueError("We refuse to add an origin") | raise ValueError("We refuse to add an origin") | ||||
def origin_visit_get_latest(self, origin): | def origin_visit_get_latest(self, origin): | ||||
return None | return None | ||||
Show All 18 Lines | def test_loader_origin_visit_failure(swh_storage): | ||||
assert actual_load_status == {"status": "failed"} | assert actual_load_status == {"status": "failed"} | ||||
loader.storage = FakeStorage2() | loader.storage = FakeStorage2() | ||||
actual_load_status2 = loader.load() | actual_load_status2 = loader.load() | ||||
assert actual_load_status2 == {"status": "failed"} | assert actual_load_status2 == {"status": "failed"} | ||||
def test_extid(): | def test_resolve_revision_from(): | ||||
loader = PackageLoader(None, None) | |||||
loader.known_artifact_to_extid = MagicMock( | |||||
wraps=lambda known_artifact: known_artifact["key"].encode() | |||||
) | |||||
known_artifacts = { | |||||
b"a" * 40: {"key": "extid-of-aaaa"}, | |||||
b"b" * 40: {"key": "extid-of-bbbb"}, | |||||
} | |||||
p_info = MagicMock() | |||||
# No known artifact -> it would be useless to compute the extid | |||||
assert loader.resolve_revision_from({}, p_info) is None | |||||
p_info.extid.assert_not_called() | |||||
loader.known_artifact_to_extid.assert_not_called() | |||||
p_info.extid.reset_mock() | |||||
# Some artifacts, but the PackageInfo does not support extids | |||||
p_info.extid.return_value = None | |||||
assert loader.resolve_revision_from(known_artifacts, p_info) is None | |||||
p_info.extid.assert_called_once() | |||||
loader.known_artifact_to_extid.assert_not_called() | |||||
p_info.extid.reset_mock() | |||||
# Some artifacts, and the PackageInfo is not one of them (ie. cache miss) | |||||
p_info.extid.return_value = b"extid-of-cccc" | |||||
assert loader.resolve_revision_from(known_artifacts, p_info) is None | |||||
p_info.extid.assert_called_once() | |||||
loader.known_artifact_to_extid.assert_any_call({"key": "extid-of-aaaa"}) | |||||
loader.known_artifact_to_extid.assert_any_call({"key": "extid-of-bbbb"}) | |||||
p_info.extid.reset_mock() | |||||
loader.known_artifact_to_extid.reset_mock() | |||||
# Some artifacts, and the PackageInfo is one of them (ie. cache hit) | |||||
p_info.extid.return_value = b"extid-of-aaaa" | |||||
assert loader.resolve_revision_from(known_artifacts, p_info) == b"a" * 40 | |||||
p_info.extid.assert_called_once() | |||||
loader.known_artifact_to_extid.assert_called_once_with({"key": "extid-of-aaaa"}) | |||||
def test_manifest_extid(): | |||||
"""Compute primary key should return the right identity | """Compute primary key should return the right identity | ||||
""" | """ | ||||
@attr.s | @attr.s | ||||
class TestPackageInfo(BasePackageInfo): | class TestPackageInfo(BaseManifestPackageInfo): | ||||
a = attr.ib() | a = attr.ib() | ||||
b = attr.ib() | b = attr.ib() | ||||
length = attr.ib() | length = attr.ib() | ||||
filename = attr.ib() | filename = attr.ib() | ||||
version = attr.ib() | version = attr.ib() | ||||
MANIFEST_FORMAT = string.Template("$a $b") | MANIFEST_FORMAT = string.Template("$a $b") | ||||
Show All 29 Lines |