Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/metadata_dictionary/test_npm.py
# Copyright (C) 2017-2022 The Software Heritage developers | # Copyright (C) 2017-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 json | import json | ||||
from hypothesis import HealthCheck, given, settings | from hypothesis import HealthCheck, given, settings | ||||
import pytest | import pytest | ||||
from swh.indexer.codemeta import COMPACT_CONTEXT | |||||
from swh.indexer.metadata_detector import detect_metadata | from swh.indexer.metadata_detector import detect_metadata | ||||
from swh.indexer.metadata_dictionary import MAPPINGS | from swh.indexer.metadata_dictionary import MAPPINGS | ||||
from swh.indexer.storage.model import ContentMetadataRow | from swh.indexer.storage.model import ContentMetadataRow | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from ..test_metadata import TRANSLATOR_TOOL, ContentMetadataTestIndexer | from ..test_metadata import TRANSLATOR_TOOL, ContentMetadataTestIndexer | ||||
from ..utils import ( | from ..utils import ( | ||||
BASE_TEST_CONFIG, | BASE_TEST_CONFIG, | ||||
Show All 31 Lines | content = b""" | ||||
}, | }, | ||||
"author": { | "author": { | ||||
"email": "moranegg@example.com", | "email": "moranegg@example.com", | ||||
"name": "Morane G" | "name": "Morane G" | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
declared_metadata = { | declared_metadata = { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
"name": "test_metadata", | "name": "test_metadata", | ||||
"version": "0.0.2", | "version": "0.0.2", | ||||
"description": "Simple package.json test for indexer", | "description": "Simple package.json test for indexer", | ||||
"codeRepository": "git+https://github.com/moranegg/metadata_test", | "codeRepository": "git+https://github.com/moranegg/metadata_test", | ||||
"author": [ | "author": [ | ||||
{ | { | ||||
"type": "Person", | "type": "Person", | ||||
Show All 14 Lines | def test_compute_metadata_invalid_description_npm(): | ||||
content = b""" | content = b""" | ||||
{ | { | ||||
"name": "test_metadata", | "name": "test_metadata", | ||||
"version": "0.0.2", | "version": "0.0.2", | ||||
"description": 1234 | "description": 1234 | ||||
} | } | ||||
""" | """ | ||||
declared_metadata = { | declared_metadata = { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
"name": "test_metadata", | "name": "test_metadata", | ||||
"version": "0.0.2", | "version": "0.0.2", | ||||
} | } | ||||
result = MAPPINGS["NpmMapping"]().translate(content) | result = MAPPINGS["NpmMapping"]().translate(content) | ||||
assert declared_metadata == result | assert declared_metadata == result | ||||
Show All 20 Lines | def test_index_content_metadata_npm(): | ||||
metadata_indexer.run(sha1s) | metadata_indexer.run(sha1s) | ||||
results = list(metadata_indexer.idx_storage.content_metadata_get(sha1s)) | results = list(metadata_indexer.idx_storage.content_metadata_get(sha1s)) | ||||
expected_results = [ | expected_results = [ | ||||
ContentMetadataRow( | ContentMetadataRow( | ||||
id=hash_to_bytes("26a9f72a7c87cc9205725cfd879f514ff4f3d8d5"), | id=hash_to_bytes("26a9f72a7c87cc9205725cfd879f514ff4f3d8d5"), | ||||
tool=TRANSLATOR_TOOL, | tool=TRANSLATOR_TOOL, | ||||
metadata={ | metadata={ | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
"codeRepository": "git+https://github.com/moranegg/metadata_test", | "codeRepository": "git+https://github.com/moranegg/metadata_test", | ||||
"description": "Simple package.json test for indexer", | "description": "Simple package.json test for indexer", | ||||
"name": "test_metadata", | "name": "test_metadata", | ||||
"version": "0.0.1", | "version": "0.0.1", | ||||
}, | }, | ||||
), | ), | ||||
ContentMetadataRow( | ContentMetadataRow( | ||||
id=hash_to_bytes("d4c647f0fc257591cc9ba1722484229780d1c607"), | id=hash_to_bytes("d4c647f0fc257591cc9ba1722484229780d1c607"), | ||||
tool=TRANSLATOR_TOOL, | tool=TRANSLATOR_TOOL, | ||||
metadata={ | metadata={ | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
"issueTracker": "https://github.com/npm/npm/issues", | "issueTracker": "https://github.com/npm/npm/issues", | ||||
"author": [ | "author": [ | ||||
{ | { | ||||
"type": "Person", | "type": "Person", | ||||
"name": "Isaac Z. Schlueter", | "name": "Isaac Z. Schlueter", | ||||
"email": "i@izs.me", | "email": "i@izs.me", | ||||
"url": "http://blog.izs.me", | "url": "http://blog.izs.me", | ||||
Show All 28 Lines | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"bugs": { | "bugs": { | ||||
"url": "https://github.com/owner/project/issues", | "url": "https://github.com/owner/project/issues", | ||||
"email": "foo@example.com" | "email": "foo@example.com" | ||||
} | } | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"issueTracker": "https://github.com/owner/project/issues", | "issueTracker": "https://github.com/owner/project/issues", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
# "invalid" dictionary | # "invalid" dictionary | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"bugs": { | "bugs": { | ||||
"email": "foo@example.com" | "email": "foo@example.com" | ||||
} | } | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
# string | # string | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"bugs": "https://github.com/owner/project/issues" | "bugs": "https://github.com/owner/project/issues" | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"issueTracker": "https://github.com/owner/project/issues", | "issueTracker": "https://github.com/owner/project/issues", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
def test_npm_repository_normalization(): | def test_npm_repository_normalization(): | ||||
# normal | # normal | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"repository": { | "repository": { | ||||
"type" : "git", | "type" : "git", | ||||
"url" : "https://github.com/npm/cli.git" | "url" : "https://github.com/npm/cli.git" | ||||
} | } | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"codeRepository": "git+https://github.com/npm/cli.git", | "codeRepository": "git+https://github.com/npm/cli.git", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
# missing url | # missing url | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"repository": { | "repository": { | ||||
"type" : "git" | "type" : "git" | ||||
} | } | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
# github shortcut | # github shortcut | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"repository": "github:npm/cli" | "repository": "github:npm/cli" | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
expected_result = { | expected_result = { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"codeRepository": "git+https://github.com/npm/cli.git", | "codeRepository": "git+https://github.com/npm/cli.git", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
assert result == expected_result | assert result == expected_result | ||||
# github shortshortcut | # github shortshortcut | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"repository": "npm/cli" | "repository": "npm/cli" | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == expected_result | assert result == expected_result | ||||
# gitlab shortcut | # gitlab shortcut | ||||
package_json = b"""{ | package_json = b"""{ | ||||
"name": "foo", | "name": "foo", | ||||
"repository": "gitlab:user/repo" | "repository": "gitlab:user/repo" | ||||
}""" | }""" | ||||
result = MAPPINGS["NpmMapping"]().translate(package_json) | result = MAPPINGS["NpmMapping"]().translate(package_json) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": COMPACT_CONTEXT, | ||||
"name": "foo", | "name": "foo", | ||||
"codeRepository": "git+https://gitlab.com/user/repo.git", | "codeRepository": "git+https://gitlab.com/user/repo.git", | ||||
"type": "SoftwareSourceCode", | "type": "SoftwareSourceCode", | ||||
} | } | ||||
@settings(suppress_health_check=[HealthCheck.too_slow]) | @settings(suppress_health_check=[HealthCheck.too_slow]) | ||||
@given(json_document_strategy(keys=list(MAPPINGS["NpmMapping"].mapping))) # type: ignore | @given(json_document_strategy(keys=list(MAPPINGS["NpmMapping"].mapping))) # type: ignore | ||||
Show All 37 Lines |