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 | from unittest.mock import Mock | ||||
import attr | import attr | ||||
import pytest | import pytest | ||||
from swh.loader.package.loader import BasePackageInfo, PackageLoader | from swh.loader.package.loader import BasePackageInfo, PackageLoader | ||||
class FakeStorage: | class FakeStorage: | ||||
Show All 23 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_resolve_revision_from_artifacts(): | def test_resolve_revision_from_artifacts() -> None: | ||||
loader = PackageLoader(None, None) | loader = PackageLoader(None, None) # type: ignore | ||||
ardumont: why those type ignore? | |||||
Done Inline Actionsbecause PackageLoader does not accept None arguments, and mypy does not allow assigning to methods vlorentz: because `PackageLoader` does not accept None arguments, and mypy does not allow assigning to… | |||||
loader.known_artifact_to_extid = MagicMock( | loader.known_artifact_to_extid = Mock( # type: ignore | ||||
wraps=lambda known_artifact: known_artifact["key"].encode() | wraps=lambda known_artifact: ("extid-type", known_artifact["key"].encode()) | ||||
) | ) | ||||
known_artifacts = { | known_artifacts = { | ||||
b"a" * 40: {"key": "extid-of-aaaa"}, | b"a" * 20: {"key": "extid-of-aaaa"}, | ||||
b"b" * 40: {"key": "extid-of-bbbb"}, | b"b" * 20: {"key": "extid-of-bbbb"}, | ||||
} | } | ||||
p_info = MagicMock() | p_info = Mock(wraps=BasePackageInfo(None, None)) # type: ignore | ||||
# No known artifact -> it would be useless to compute the extid | # No known artifact -> it would be useless to compute the extid | ||||
assert loader.resolve_revision_from_artifacts({}, p_info) is None | assert loader.resolve_revision_from_artifacts({}, p_info) is None | ||||
p_info.extid.assert_not_called() | p_info.extid.assert_not_called() | ||||
loader.known_artifact_to_extid.assert_not_called() | loader.known_artifact_to_extid.assert_not_called() | ||||
p_info.extid.reset_mock() | p_info.extid.reset_mock() | ||||
# Some artifacts, but the PackageInfo does not support extids | # Some artifacts, but the PackageInfo does not support extids | ||||
p_info.extid.return_value = None | p_info.extid.return_value = None | ||||
assert loader.resolve_revision_from_artifacts(known_artifacts, p_info) is None | assert loader.resolve_revision_from_artifacts(known_artifacts, p_info) is None | ||||
p_info.extid.assert_called_once() | p_info.extid.assert_called_once() | ||||
loader.known_artifact_to_extid.assert_not_called() | loader.known_artifact_to_extid.assert_not_called() | ||||
p_info.extid.reset_mock() | p_info.extid.reset_mock() | ||||
# Some artifacts, and the PackageInfo is not one of them (ie. cache miss) | # Some artifacts, and the PackageInfo is not one of them (ie. cache miss) | ||||
p_info.extid.return_value = b"extid-of-cccc" | p_info.extid.return_value = ("extid-type", b"extid-of-cccc") | ||||
assert loader.resolve_revision_from_artifacts(known_artifacts, p_info) is None | assert loader.resolve_revision_from_artifacts(known_artifacts, p_info) is None | ||||
p_info.extid.assert_called_once() | 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-aaaa"}) | ||||
loader.known_artifact_to_extid.assert_any_call({"key": "extid-of-bbbb"}) | loader.known_artifact_to_extid.assert_any_call({"key": "extid-of-bbbb"}) | ||||
p_info.extid.reset_mock() | p_info.extid.reset_mock() | ||||
loader.known_artifact_to_extid.reset_mock() | loader.known_artifact_to_extid.reset_mock() | ||||
# Some artifacts, and the PackageInfo is one of them (ie. cache hit) | # Some artifacts, and the PackageInfo is one of them (ie. cache hit) | ||||
p_info.extid.return_value = b"extid-of-aaaa" | p_info.extid.return_value = ("extid-type", b"extid-of-aaaa") | ||||
assert loader.resolve_revision_from_artifacts(known_artifacts, p_info) == b"a" * 40 | assert loader.resolve_revision_from_artifacts(known_artifacts, p_info) == b"a" * 20 | ||||
p_info.extid.assert_called_once() | p_info.extid.assert_called_once() | ||||
loader.known_artifact_to_extid.assert_called_once_with({"key": "extid-of-aaaa"}) | loader.known_artifact_to_extid.assert_called_once_with({"key": "extid-of-aaaa"}) | ||||
def test_manifest_extid(): | def test_manifest_extid(): | ||||
"""Compute primary key should return the right identity | """Compute primary key should return the right identity | ||||
""" | """ | ||||
Show All 40 Lines |
why those type ignore?