Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/test_utils.py
# Copyright (C) 2018-2020 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 | |||||
import pytest | import pytest | ||||
from swh.deposit import utils | from swh.deposit import utils | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.identifiers import CoreSWHID, QualifiedSWHID | from swh.model.identifiers import CoreSWHID, QualifiedSWHID | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | with pytest.raises(ValueError): | ||||
utils.merge(d1, d0) | utils.merge(d1, d0) | ||||
with pytest.raises(ValueError): | with pytest.raises(ValueError): | ||||
utils.merge(d1) | utils.merge(d1) | ||||
assert utils.merge(d0) == d0 | assert utils.merge(d0) == d0 | ||||
@patch("swh.deposit.utils.normalize_timestamp", side_effect=lambda x: x) | def test_normalize_date_0(): | ||||
def test_normalize_date_0(mock_normalize): | |||||
"""When date is a list, choose the first date and normalize it | """When date is a list, choose the first date and normalize it | ||||
Note: We do not test swh.model.identifiers which is already tested | |||||
in swh.model | |||||
""" | """ | ||||
actual_date = utils.normalize_date(["2017-10-12", "date1"]) | actual_date = utils.normalize_date(["2017-10-12", "date1"]) | ||||
expected_date = "2017-10-12 00:00:00+00:00" | assert actual_date == { | ||||
"timestamp": {"microseconds": 0, "seconds": 1507766400}, | |||||
assert str(actual_date) == expected_date | "negative_utc": False, | ||||
"offset": 0, | |||||
} | |||||
@patch("swh.deposit.utils.normalize_timestamp", side_effect=lambda x: x) | def test_normalize_date_1(): | ||||
def test_normalize_date_1(mock_normalize): | |||||
"""Providing a date in a reasonable format, everything is fine | """Providing a date in a reasonable format, everything is fine | ||||
Note: We do not test swh.model.identifiers which is already tested | |||||
in swh.model | |||||
""" | """ | ||||
actual_date = utils.normalize_date("2018-06-11 17:02:02") | actual_date = utils.normalize_date("2018-06-11 17:02:02") | ||||
expected_date = "2018-06-11 17:02:02+00:00" | assert actual_date == { | ||||
"timestamp": {"microseconds": 0, "seconds": 1528736522}, | |||||
assert str(actual_date) == expected_date | "negative_utc": False, | ||||
"offset": 0, | |||||
} | |||||
@patch("swh.deposit.utils.normalize_timestamp", side_effect=lambda x: x) | def test_normalize_date_doing_irrelevant_stuff(): | ||||
def test_normalize_date_doing_irrelevant_stuff(mock_normalize): | |||||
"""Providing a date with only the year results in a reasonable date | """Providing a date with only the year results in a reasonable date | ||||
Note: We do not test swh.model.identifiers which is already tested | |||||
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" | assert actual_date == { | ||||
"timestamp": {"seconds": 1483228800, "microseconds": 0}, | |||||
assert str(actual_date) == expected_date | "offset": 0, | ||||
"negative_utc": False, | |||||
} | |||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"swhid,expected_metadata_context", | "swhid,expected_metadata_context", | ||||
[ | [ | ||||
("swh:1:cnt:51b5c8cc985d190b5a7ef4878128ebfdc2358f49", {"origin": None},), | ("swh:1:cnt:51b5c8cc985d190b5a7ef4878128ebfdc2358f49", {"origin": None},), | ||||
( | ( | ||||
"swh:1:snp:51b5c8cc985d190b5a7ef4878128ebfdc2358f49;origin=http://blah", | "swh:1:snp:51b5c8cc985d190b5a7ef4878128ebfdc2358f49;origin=http://blah", | ||||
▲ Show 20 Lines • Show All 119 Lines • Show Last 20 Lines |