Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show All 10 Lines | |||||
import attr | import attr | ||||
import pytest | import pytest | ||||
from swh.model import hashutil, identifiers | from swh.model import hashutil, identifiers | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.hashutil import hash_to_bytes as _x | from swh.model.hashutil import hash_to_bytes as _x | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
CONTENT, | |||||
DIRECTORY, | |||||
RELEASE, | |||||
REVISION, | |||||
SNAPSHOT, | |||||
SWHID, | |||||
SWHID_QUALIFIERS, | SWHID_QUALIFIERS, | ||||
CoreSWHID, | CoreSWHID, | ||||
ExtendedObjectType, | ExtendedObjectType, | ||||
ExtendedSWHID, | ExtendedSWHID, | ||||
ObjectType, | ObjectType, | ||||
QualifiedSWHID, | QualifiedSWHID, | ||||
normalize_timestamp, | normalize_timestamp, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 853 Lines • ▼ Show 20 Lines | |||||
@pytest.mark.parametrize("dict_input", TS_DICTS_INVALID_TIMESTAMP) | @pytest.mark.parametrize("dict_input", TS_DICTS_INVALID_TIMESTAMP) | ||||
def test_normalize_timestamp_dict_invalid_timestamp(dict_input): | def test_normalize_timestamp_dict_invalid_timestamp(dict_input): | ||||
with pytest.raises(ValueError, match="non-integer timestamp"): | with pytest.raises(ValueError, match="non-integer timestamp"): | ||||
normalize_timestamp(dict_input) | normalize_timestamp(dict_input) | ||||
class TestSwhid(unittest.TestCase): | |||||
@pytest.mark.filterwarnings("ignore:.*SWHID.*:DeprecationWarning") | |||||
def test_swhid(self): | |||||
_snapshot_id = _x("c7c108084bc0bf3d81436bf980b46e98bd338453") | |||||
_release_id = "22ece559cc7cc2364edc5e5593d63ae8bd229f9f" | |||||
_revision_id = "309cf2674ee7a0749978cf8265ab91a60aea0f7d" | |||||
_directory_id = "d198bc9d7a6bcf6db04f476d29314f157507d505" | |||||
_content_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | |||||
_snapshot = {"id": _snapshot_id} | |||||
_release = {"id": _release_id} | |||||
_revision = {"id": _revision_id} | |||||
_directory = {"id": _directory_id} | |||||
_content = {"sha1_git": _content_id} | |||||
for full_type, _hash, expected_swhid, version, _meta in [ | |||||
( | |||||
SNAPSHOT, | |||||
_snapshot_id, | |||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | |||||
None, | |||||
{}, | |||||
), | |||||
( | |||||
RELEASE, | |||||
_release_id, | |||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | |||||
1, | |||||
{}, | |||||
), | |||||
( | |||||
REVISION, | |||||
_revision_id, | |||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | |||||
None, | |||||
{}, | |||||
), | |||||
( | |||||
DIRECTORY, | |||||
_directory_id, | |||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | |||||
None, | |||||
{}, | |||||
), | |||||
( | |||||
CONTENT, | |||||
_content_id, | |||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
1, | |||||
{}, | |||||
), | |||||
( | |||||
SNAPSHOT, | |||||
_snapshot, | |||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | |||||
None, | |||||
{}, | |||||
), | |||||
( | |||||
RELEASE, | |||||
_release, | |||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | |||||
1, | |||||
{}, | |||||
), | |||||
( | |||||
REVISION, | |||||
_revision, | |||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | |||||
None, | |||||
{}, | |||||
), | |||||
( | |||||
DIRECTORY, | |||||
_directory, | |||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | |||||
None, | |||||
{}, | |||||
), | |||||
( | |||||
CONTENT, | |||||
_content, | |||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
1, | |||||
{}, | |||||
), | |||||
( | |||||
CONTENT, | |||||
_content, | |||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=1", | |||||
1, | |||||
{"origin": "1"}, | |||||
), | |||||
]: | |||||
if version: | |||||
actual_value = identifiers.swhid( | |||||
full_type, _hash, version, metadata=_meta | |||||
) | |||||
else: | |||||
actual_value = identifiers.swhid(full_type, _hash, metadata=_meta) | |||||
self.assertEqual(actual_value, expected_swhid) | |||||
def test_swhid_wrong_input(self): | |||||
_snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | |||||
_snapshot = {"id": _snapshot_id} | |||||
for _type, _hash in [ | |||||
(SNAPSHOT, _snapshot_id), | |||||
(SNAPSHOT, _snapshot), | |||||
("lines", "42"), | |||||
]: | |||||
with self.assertRaises(ValidationError): | |||||
identifiers.swhid(_type, _hash) | |||||
@pytest.mark.filterwarnings("ignore:.*SWHID.*:DeprecationWarning") | |||||
def test_parse_swhid(self): | |||||
for swhid, _type, _version, _hash in [ | |||||
( | |||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
CONTENT, | |||||
1, | |||||
"94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | |||||
( | |||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | |||||
DIRECTORY, | |||||
1, | |||||
"d198bc9d7a6bcf6db04f476d29314f157507d505", | |||||
), | |||||
( | |||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | |||||
REVISION, | |||||
1, | |||||
"309cf2674ee7a0749978cf8265ab91a60aea0f7d", | |||||
), | |||||
( | |||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | |||||
RELEASE, | |||||
1, | |||||
"22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | |||||
), | |||||
( | |||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | |||||
SNAPSHOT, | |||||
1, | |||||
"c7c108084bc0bf3d81436bf980b46e98bd338453", | |||||
), | |||||
]: | |||||
with pytest.warns(DeprecationWarning): | |||||
expected_result = SWHID( | |||||
namespace="swh", | |||||
scheme_version=_version, | |||||
object_type=_type, | |||||
object_id=_hash, | |||||
metadata={}, | |||||
) | |||||
actual_result = identifiers.parse_swhid(swhid) | |||||
self.assertEqual(actual_result, expected_result) | |||||
self.assertEqual(str(expected_result), swhid) | |||||
for swhid, _type, _version, _hash, _metadata in [ | |||||
( | |||||
"swh:1:cnt:9c95815d9e9d91b8dae8e05d8bbc696fe19f796b;lines=1-18;origin=https://github.com/python/cpython", # noqa | |||||
CONTENT, | |||||
1, | |||||
"9c95815d9e9d91b8dae8e05d8bbc696fe19f796b", | |||||
{"lines": "1-18", "origin": "https://github.com/python/cpython"}, | |||||
), | |||||
( | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=deb://Debian/packages/linuxdoc-tools", # noqa | |||||
DIRECTORY, | |||||
1, | |||||
"0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | |||||
{"origin": "deb://Debian/packages/linuxdoc-tools"}, | |||||
), | |||||
]: | |||||
with pytest.warns(DeprecationWarning): | |||||
expected_result = SWHID( | |||||
namespace="swh", | |||||
scheme_version=_version, | |||||
object_type=_type, | |||||
object_id=_hash, | |||||
metadata=_metadata, | |||||
) | |||||
actual_result = identifiers.parse_swhid(swhid) | |||||
self.assertEqual(actual_result, expected_result) | |||||
self.assertEqual( | |||||
expected_result.to_dict(), | |||||
{ | |||||
"namespace": "swh", | |||||
"scheme_version": _version, | |||||
"object_type": _type, | |||||
"object_id": _hash, | |||||
"metadata": _metadata, | |||||
}, | |||||
) | |||||
self.assertEqual(str(expected_result), swhid) | |||||
@pytest.mark.parametrize( | |||||
"invalid_swhid", | |||||
[ | |||||
"swh:1:cnt", | |||||
"swh:1:", | |||||
"swh:", | |||||
"swh:1:cnt:", | |||||
"foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505", | |||||
"swh:2:dir:def8bc9d7a6bcf6db04f476d29314f157507d505", | |||||
"swh:1:foo:fed8bc9d7a6bcf6db04f476d29314f157507d505", | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;malformed", | |||||
"swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d", | |||||
"swh:1:snp:foo", | |||||
# wrong qualifier: ori should be origin | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;ori=something;anchor=1;visit=1;path=/", # noqa | |||||
# wrong qualifier: anc should be anchor | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=something;anc=1;visit=1;path=/", # noqa | |||||
# wrong qualifier: vis should be visit | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=something;anchor=1;vis=1;path=/", # noqa | |||||
# wrong qualifier: pa should be path | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=something;anchor=1;visit=1;pa=/", # noqa | |||||
# wrong qualifier: line should be lines | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;line=10;origin=something;anchor=1;visit=1;path=/", # noqa | |||||
# wrong qualifier value: it contains space before of after | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin= https://some-url", # noqa | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=something;anchor=some-anchor ", # noqa | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=something;anchor=some-anchor ;visit=1", # noqa | |||||
# invalid swhid: whitespaces | |||||
"swh :1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;ori=something;anchor=1;visit=1;path=/", # noqa | |||||
"swh: 1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;ori=something;anchor=1;visit=1;path=/", # noqa | |||||
"swh: 1: dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;ori=something;anchor=1;visit=1;path=/", # noqa | |||||
"swh:1: dir: 0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | |||||
"swh:1: dir: 0b6959356d30f1a4e9b7f6bca59b9a336464c03d; origin=blah", | |||||
"swh:1: dir: 0b6959356d30f1a4e9b7f6bca59b9a336464c03d;lines=12", | |||||
# other whitespaces | |||||
"swh\t:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;lines=12", | |||||
"swh:1\n:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;lines=12", | |||||
"swh:1:\rdir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;lines=12", | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d\f;lines=12", | |||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;lines=12\v", | |||||
], | |||||
) | |||||
def test_parse_swhid_parsing_error(invalid_swhid): | |||||
with pytest.raises(ValidationError): | |||||
identifiers.parse_swhid(invalid_swhid) | |||||
@pytest.mark.filterwarnings("ignore:.*SWHID.*:DeprecationWarning") | |||||
@pytest.mark.parametrize( | |||||
"ns,version,type,id", | |||||
[ | |||||
("foo", 1, CONTENT, "abc8bc9d7a6bcf6db04f476d29314f157507d505",), | |||||
("swh", 2, DIRECTORY, "def8bc9d7a6bcf6db04f476d29314f157507d505",), | |||||
("swh", 1, "foo", "fed8bc9d7a6bcf6db04f476d29314f157507d505",), | |||||
("swh", 1, SNAPSHOT, "gh6959356d30f1a4e9b7f6bca59b9a336464c03d",), | |||||
], | |||||
) | |||||
def test_SWHID_class_validation_error(ns, version, type, id): | |||||
with pytest.raises(ValidationError): | |||||
SWHID( | |||||
namespace=ns, scheme_version=version, object_type=type, object_id=id, | |||||
) | |||||
@pytest.mark.filterwarnings("ignore:.*SWHID.*:DeprecationWarning") | |||||
def test_SWHID_hash(): | |||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | |||||
assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( | |||||
SWHID(object_type="directory", object_id=object_id) | |||||
) | |||||
assert hash( | |||||
SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | |||||
) == hash( | |||||
SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | |||||
) | |||||
# Different order of the dictionary, so the underlying order of the tuple in | |||||
# ImmutableDict is different. | |||||
assert hash( | |||||
SWHID( | |||||
object_type="directory", | |||||
object_id=object_id, | |||||
metadata={"origin": "https://example.com", "lines": "42"}, | |||||
) | |||||
) == hash( | |||||
SWHID( | |||||
object_type="directory", | |||||
object_id=object_id, | |||||
metadata={"lines": "42", "origin": "https://example.com"}, | |||||
) | |||||
) | |||||
@pytest.mark.filterwarnings("ignore:.*SWHID.*:DeprecationWarning") | |||||
def test_SWHID_eq(): | |||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | |||||
assert SWHID(object_type="directory", object_id=object_id) == SWHID( | |||||
object_type="directory", object_id=object_id | |||||
) | |||||
assert SWHID( | |||||
object_type="directory", object_id=object_id, metadata=dummy_qualifiers, | |||||
) == SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | |||||
assert SWHID( | |||||
object_type="directory", object_id=object_id, metadata=dummy_qualifiers, | |||||
) == SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | |||||
# SWHIDs that are outright invalid, no matter the context | # SWHIDs that are outright invalid, no matter the context | ||||
INVALID_SWHIDS = [ | INVALID_SWHIDS = [ | ||||
"swh:1:cnt", | "swh:1:cnt", | ||||
"swh:1:", | "swh:1:", | ||||
"swh:", | "swh:", | ||||
"swh:1:cnt:", | "swh:1:cnt:", | ||||
"foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505", | "foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505", | ||||
"swh:2:dir:def8bc9d7a6bcf6db04f476d29314f157507d505", | "swh:2:dir:def8bc9d7a6bcf6db04f476d29314f157507d505", | ||||
▲ Show 20 Lines • Show All 584 Lines • Show Last 20 Lines |