Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/npm/tests/test_npm.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 json | import json | ||||
import os | import os | ||||
import pytest | import pytest | ||||
from swh.loader.package import __version__ | from swh.loader.package import __version__ | ||||
from swh.loader.package.npm.loader import ( | from swh.loader.package.npm.loader import ( | ||||
NpmLoader, | NpmLoader, | ||||
_author_str, | _author_str, | ||||
artifact_to_revision_id, | |||||
extract_npm_package_author, | extract_npm_package_author, | ||||
) | ) | ||||
from swh.loader.package.tests.common import check_metadata_paths | from swh.loader.package.tests.common import check_metadata_paths | ||||
from swh.loader.tests import assert_last_visit_matches, check_snapshot, get_stats | from swh.loader.tests import assert_last_visit_matches, check_snapshot, get_stats | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
CoreSWHID, | CoreSWHID, | ||||
ExtendedObjectType, | ExtendedObjectType, | ||||
▲ Show 20 Lines • Show All 516 Lines • ▼ Show 20 Lines | expected_snapshot = Snapshot( | ||||
target_type=TargetType.REVISION, | target_type=TargetType.REVISION, | ||||
target=hash_to_bytes("05181c12cd8c22035dd31155656826b85745da37"), | target=hash_to_bytes("05181c12cd8c22035dd31155656826b85745da37"), | ||||
), | ), | ||||
}, | }, | ||||
) | ) | ||||
check_snapshot(expected_snapshot, swh_storage) | check_snapshot(expected_snapshot, swh_storage) | ||||
def test_npm_artifact_to_revision_id_none(): | def test_npm__known_artifact_to_extid__old_loader_version(): | ||||
"""Current loader version should stop soon if nothing can be found | """Current loader version should parse old metadata scheme | ||||
""" | """ | ||||
assert ( | |||||
NpmLoader.known_artifact_to_extid( | |||||
{"package_source": {"sha1": "something-wrong"}} | |||||
) | |||||
is None | |||||
) | |||||
class artifact_metadata: | sha1 = "05181c12cd8c22035dd31155656826b85745da37" | ||||
shasum = "05181c12cd8c22035dd31155656826b85745da37" | assert NpmLoader.known_artifact_to_extid( | ||||
{"package_source": {"sha1": sha1,}} | |||||
known_artifacts = { | ) == hash_to_bytes(sha1) | ||||
"b11ebac8c9d0c9e5063a2df693a18e3aba4b2f92": {}, | |||||
} | |||||
assert artifact_to_revision_id(known_artifacts, artifact_metadata) is None | |||||
def test_npm_artifact_to_revision_id_old_loader_version(): | def test_npm__known_artifact_to_extid__current_loader_version(): | ||||
"""Current loader version should solve old metadata scheme | """Current loader version should be able to parse current metadata scheme | ||||
""" | """ | ||||
sha1 = "05181c12cd8c22035dd31155656826b85745da37" | |||||
assert NpmLoader.known_artifact_to_extid( | |||||
{"original_artifact": [{"checksums": {"sha1": sha1},}],} | |||||
) == hash_to_bytes(sha1) | |||||
class artifact_metadata: | assert ( | ||||
shasum = "05181c12cd8c22035dd31155656826b85745da37" | NpmLoader.known_artifact_to_extid( | ||||
{"original_artifact": [{"checksums": {"sha1": "something-wrong"},}],}, | |||||
known_artifacts = { | |||||
hash_to_bytes("b11ebac8c9d0c9e5063a2df693a18e3aba4b2f92"): { | |||||
"package_source": {"sha1": "something-wrong"} | |||||
}, | |||||
hash_to_bytes("845673bfe8cbd31b1eaf757745a964137e6f9116"): { | |||||
"package_source": {"sha1": "05181c12cd8c22035dd31155656826b85745da37",} | |||||
}, | |||||
} | |||||
assert artifact_to_revision_id(known_artifacts, artifact_metadata) == hash_to_bytes( | |||||
"845673bfe8cbd31b1eaf757745a964137e6f9116" | |||||
) | ) | ||||
is None | |||||
def test_npm_artifact_to_revision_id_current_loader_version(): | |||||
"""Current loader version should be able to solve current metadata scheme | |||||
""" | |||||
class artifact_metadata: | |||||
shasum = "05181c12cd8c22035dd31155656826b85745da37" | |||||
known_artifacts = { | |||||
hash_to_bytes("b11ebac8c9d0c9e5063a2df693a18e3aba4b2f92"): { | |||||
"original_artifact": [ | |||||
{"checksums": {"sha1": "05181c12cd8c22035dd31155656826b85745da37"},} | |||||
], | |||||
}, | |||||
hash_to_bytes("845673bfe8cbd31b1eaf757745a964137e6f9116"): { | |||||
"original_artifact": [{"checksums": {"sha1": "something-wrong"},}], | |||||
}, | |||||
} | |||||
assert artifact_to_revision_id(known_artifacts, artifact_metadata) == hash_to_bytes( | |||||
"b11ebac8c9d0c9e5063a2df693a18e3aba4b2f92" | |||||
) | ) | ||||
def test_npm_artifact_with_no_intrinsic_metadata(swh_storage, requests_mock_datadir): | def test_npm_artifact_with_no_intrinsic_metadata(swh_storage, requests_mock_datadir): | ||||
"""Skip artifact with no intrinsic metadata during ingestion | """Skip artifact with no intrinsic metadata during ingestion | ||||
""" | """ | ||||
package = "nativescript-telerik-analytics" | package = "nativescript-telerik-analytics" | ||||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |