Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/tests/test_utils.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 os | import os | ||||
import pytest | import pytest | ||||
import swh.loader.package | import swh.loader.package | ||||
from swh.loader.package.utils import download, api_info, release_name | from swh.loader.package.utils import ( | ||||
download, api_info, release_name, parse_author, artifact_identity | |||||
) | |||||
def test_version_generation(): | def test_version_generation(): | ||||
assert swh.loader.package.__version__ != 'devel', \ | assert swh.loader.package.__version__ != 'devel', \ | ||||
"Make sure swh.loader.core is installed (e.g. pip install -e .)" | "Make sure swh.loader.core is installed (e.g. pip install -e .)" | ||||
@pytest.mark.fs | @pytest.mark.fs | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | def test_api_info(requests_mock): | ||||
} | } | ||||
def test_release_name(): | def test_release_name(): | ||||
for version, filename, expected_release in [ | for version, filename, expected_release in [ | ||||
('0.0.1', None, 'releases/0.0.1'), | ('0.0.1', None, 'releases/0.0.1'), | ||||
('0.0.2', 'something', 'releases/0.0.2/something')]: | ('0.0.2', 'something', 'releases/0.0.2/something')]: | ||||
assert release_name(version, filename) == expected_release | assert release_name(version, filename) == expected_release | ||||
def _parse_author_string_test(author_str, expected_result): | |||||
assert parse_author(author_str) == expected_result | |||||
assert parse_author(' %s' % author_str) == expected_result | |||||
assert parse_author('%s ' % author_str) == expected_result | |||||
def test_parse_author(): | |||||
_parse_author_string_test( | |||||
'John Doe', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'<john.doe@foo.bar>', | |||||
{ | |||||
'email': 'john.doe@foo.bar' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'(https://john.doe)', | |||||
{ | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe <john.doe@foo.bar>', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe<john.doe@foo.bar>', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe (https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe(https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'<john.doe@foo.bar> (https://john.doe)', | |||||
{ | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'(https://john.doe) <john.doe@foo.bar>', | |||||
{ | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe <john.doe@foo.bar> (https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe (https://john.doe) <john.doe@foo.bar>', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe<john.doe@foo.bar> (https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe<john.doe@foo.bar>(https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test('', {}) | |||||
_parse_author_string_test('<>', {}) | |||||
_parse_author_string_test(' <>', {}) | |||||
_parse_author_string_test('<>()', {}) | |||||
_parse_author_string_test('<> ()', {}) | |||||
_parse_author_string_test('()', {}) | |||||
_parse_author_string_test(' ()', {}) | |||||
_parse_author_string_test( | |||||
'John Doe <> ()', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe <>', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe ()', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
def test_artifact_identity(): | |||||
"""Compute primary key should return the right identity | |||||
""" | |||||
data = { | |||||
'a': 1, | |||||
'b': 2, | |||||
'length': 221837, | |||||
'filename': '8sync-0.1.0.tar.gz', | |||||
'version': '0.1.0', | |||||
} | |||||
for id_keys, expected_id in [ | |||||
(['a', 'b'], [1, 2]), | |||||
([], []), | |||||
(['a', 'key-that-does-not-exist'], [1, None]) | |||||
]: | |||||
actual_id = artifact_identity(data, id_keys=id_keys) | |||||
assert actual_id == expected_id |