Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/metadata_dictionary/test_codemeta.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 | |||||
from swh.indexer.codemeta import CODEMETA_TERMS | from swh.indexer.codemeta import CODEMETA_TERMS | ||||
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 ..utils import json_document_strategy | from ..utils import json_document_strategy | ||||
▲ Show 20 Lines • Show All 232 Lines • ▼ Show 20 Lines | def test_sword_mixed(): | ||||
result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | ||||
assert result == { | assert result == { | ||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | "@context": "https://doi.org/10.5063/schema/codemeta-2.0", | ||||
"name": "My Software", | "name": "My Software", | ||||
"version": "1.2.3", | "version": "1.2.3", | ||||
} | } | ||||
@pytest.mark.parametrize("id_", ["", " ", "\n"]) | |||||
def test_sword_invalid_id(id_): | |||||
content = f"""<?xml version="1.0"?> | |||||
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | |||||
xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | |||||
xmlns:schema="http://schema.org/"> | |||||
<name>My Software</name> | |||||
<id>{id_}</id> | |||||
</atom:entry> | |||||
""" | |||||
result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | |||||
assert result == { | |||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | |||||
"name": "My Software", | |||||
} | |||||
@pytest.mark.parametrize( | |||||
"id_", | |||||
[ | |||||
"foo", | |||||
"42", | |||||
"http://example.org/", | |||||
"http://example.org/foo", | |||||
"https://example.org/", | |||||
"https://example.org/foo", | |||||
], | |||||
) | |||||
def test_sword_id(id_): | |||||
content = f"""<?xml version="1.0"?> | |||||
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | |||||
xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | |||||
xmlns:schema="http://schema.org/"> | |||||
<name>My Software</name> | |||||
<id>{id_}</id> | |||||
</atom:entry> | |||||
""" | |||||
result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | |||||
assert result == { | |||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | |||||
"id": id_, | |||||
"name": "My Software", | |||||
} | |||||
def test_sword_multiple_ids(): | |||||
"""JSON-LD only allows a single id, so we ignore all but the first one.""" | |||||
content = """<?xml version="1.0"?> | |||||
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | |||||
xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | |||||
xmlns:schema="http://schema.org/"> | |||||
<name>My Software</name> | |||||
<id>http://example.org/foo</id> | |||||
<id>http://example.org/bar</id> | |||||
</atom:entry> | |||||
""" | |||||
result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | |||||
assert result == { | |||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | |||||
"id": "http://example.org/foo", | |||||
"name": "My Software", | |||||
} | |||||
def test_sword_type(): | |||||
content = """<?xml version="1.0"?> | |||||
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | |||||
xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | |||||
xmlns:schema="http://schema.org/"> | |||||
<name>My Software</name> | |||||
<type>http://schema.org/WebSite</type> | |||||
</atom:entry> | |||||
""" | |||||
result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | |||||
assert result == { | |||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | |||||
"type": "schema:WebSite", | |||||
"name": "My Software", | |||||
} | |||||
def test_sword_multiple_type(): | |||||
content = """<?xml version="1.0"?> | |||||
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | |||||
xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | |||||
xmlns:schema="http://schema.org/"> | |||||
<name>My Software</name> | |||||
<type>http://schema.org/WebSite</type> | |||||
<type>http://schema.org/SoftwareSourceCode</type> | |||||
</atom:entry> | |||||
""" | |||||
result = MAPPINGS["SwordCodemetaMapping"]().translate(content) | |||||
assert result in ( | |||||
{ | |||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | |||||
"type": ["schema:WebSite", "SoftwareSourceCode"], | |||||
"name": "My Software", | |||||
}, | |||||
{ | |||||
"@context": "https://doi.org/10.5063/schema/codemeta-2.0", | |||||
"type": ["SoftwareSourceCode", "schema:WebSite"], | |||||
"name": "My Software", | |||||
}, | |||||
) | |||||
def test_sword_schemaorg_in_codemeta(): | def test_sword_schemaorg_in_codemeta(): | ||||
content = """<?xml version="1.0"?> | content = """<?xml version="1.0"?> | ||||
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | <atom:entry xmlns:atom="http://www.w3.org/2005/Atom" | ||||
xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | xmlns="https://doi.org/10.5063/schema/codemeta-2.0" | ||||
xmlns:schema="http://schema.org/"> | xmlns:schema="http://schema.org/"> | ||||
<name>My Software</name> | <name>My Software</name> | ||||
<schema:version>1.2.3</schema:version> | <schema:version>1.2.3</schema:version> | ||||
</atom:entry> | </atom:entry> | ||||
▲ Show 20 Lines • Show All 158 Lines • Show Last 20 Lines |