Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/test_utils.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2020 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 | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import pytest | import pytest | ||||
from swh.deposit import utils | from swh.deposit import utils | ||||
from swh.deposit.parsers import parse_xml | |||||
from swh.deposit.utils import read_swh_object_reference | |||||
from swh.model.identifiers import parse_swhid | |||||
def test_merge(): | def test_merge(): | ||||
"""Calling utils.merge on dicts should merge without losing information | """Calling utils.merge on dicts should merge without losing information | ||||
""" | """ | ||||
d0 = {"author": "someone", "license": [["gpl2"]], "a": 1} | d0 = {"author": "someone", "license": [["gpl2"]], "a": 1} | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | def test_normalize_date_doing_irrelevant_stuff(mock_normalize): | ||||
in swh.model | in swh.model | ||||
""" | """ | ||||
actual_date = utils.normalize_date("2017") | actual_date = utils.normalize_date("2017") | ||||
expected_date = "2017-01-01 00:00:00+00:00" | expected_date = "2017-01-01 00:00:00+00:00" | ||||
assert str(actual_date) == expected_date | assert str(actual_date) == expected_date | ||||
@pytest.fixture | |||||
def xml_with_origin_reference(): | |||||
xml_data = """<?xml version="1.0"?> | |||||
<entry xmlns="http://www.w3.org/2005/Atom" | |||||
xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0" | |||||
xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit"> | |||||
<swh:deposit> | |||||
<swh:reference> | |||||
<swh:origin url="{url}"/> | |||||
</swh:reference> | |||||
</swh:deposit> | |||||
</entry> | |||||
""" | |||||
return xml_data.strip() | |||||
def test_read_swh_reference_origin(xml_with_origin_reference): | |||||
url = "https://url" | |||||
xml_data = xml_with_origin_reference.format(url=url) | |||||
metadata = parse_xml(xml_data) | |||||
actual_origin = read_swh_object_reference(metadata) | |||||
assert actual_origin == url | |||||
@pytest.fixture | |||||
def xml_with_empty_reference(): | |||||
xml_data = """<?xml version="1.0"?> | |||||
<entry xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit"> | |||||
<swh:deposit> | |||||
{swh_reference} | |||||
</swh:deposit> | |||||
</entry> | |||||
""" | |||||
return xml_data.strip() | |||||
@pytest.mark.parametrize( | |||||
"xml_ref", | |||||
[ | |||||
"", | |||||
"<swh:reference></swh:reference>", | |||||
"<swh:reference><swh:object /></swh:reference>", | |||||
"""<swh:reference><swh:object swhid="" /></swh:reference>""", | |||||
], | |||||
) | |||||
def test_read_swh_reference_empty(xml_with_empty_reference, xml_ref): | |||||
xml_body = xml_with_empty_reference.format(swh_reference=xml_ref) | |||||
metadata = parse_xml(xml_body) | |||||
assert read_swh_object_reference(metadata) is None | |||||
@pytest.fixture | |||||
def xml_with_swhid(): | |||||
xml_data = """<?xml version="1.0"?> | |||||
<entry xmlns="http://www.w3.org/2005/Atom" | |||||
xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0" | |||||
xmlns:swh="https://www.softwareheritage.org/schema/2018/deposit"> | |||||
<swh:deposit> | |||||
<swh:reference> | |||||
<swh:object swhid="{swhid}" /> | |||||
</swh:reference> | |||||
</swh:deposit> | |||||
</entry> | |||||
""" | |||||
return xml_data.strip() | |||||
@pytest.mark.parametrize( | |||||
"swhid", | |||||
[ | |||||
"""swh:1:dir:31b5c8cc985d190b5a7ef4878128ebfdc2358f49; | |||||
origin=https://hal.archives-ouvertes.fr/hal-01243573; | |||||
visit=swh:1:snp:4fc1e36fca86b2070204bedd51106014a614f321; | |||||
anchor=swh:1:rev:9c5de20cfb54682370a398fcc733e829903c8cba; | |||||
path=/moranegg-AffectationRO-df7f68b/""", | |||||
"swh:1:dir:31b5c8cc985d190b5a7ef4878128ebfdc2358f49", | |||||
], | |||||
) | |||||
def test_read_swh_reference_swhid(swhid, xml_with_swhid): | |||||
xml_data = xml_with_swhid.format(swhid=swhid) | |||||
metadata = parse_xml(xml_data) | |||||
actual_swhid = read_swh_object_reference(metadata) | |||||
assert actual_swhid is not None | |||||
clean_swhid = ";".join(map(lambda s: s.strip(), swhid.split(";"))) | |||||
expected_swhid = parse_swhid(clean_swhid) | |||||
assert actual_swhid == expected_swhid |