Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show All 14 Lines | |||||
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, | CONTENT, | ||||
DIRECTORY, | DIRECTORY, | ||||
RELEASE, | RELEASE, | ||||
REVISION, | REVISION, | ||||
SNAPSHOT, | SNAPSHOT, | ||||
SWHID, | SWHID, | ||||
CoreSWHID, | |||||
ObjectType, | ObjectType, | ||||
QualifiedSWHID, | QualifiedSWHID, | ||||
normalize_timestamp, | normalize_timestamp, | ||||
) | ) | ||||
def remove_id(d: Dict) -> Dict: | def remove_id(d: Dict) -> Dict: | ||||
"""Returns a (shallow) copy of a dict with the 'id' key removed.""" | """Returns a (shallow) copy of a dict with the 'id' key removed.""" | ||||
▲ Show 20 Lines • Show All 1,369 Lines • ▼ Show 20 Lines | assert QualifiedSWHID( | ||||
object_type=ObjectType.DIRECTORY, | object_type=ObjectType.DIRECTORY, | ||||
object_id=object_id, | object_id=object_id, | ||||
qualifiers=dummy_qualifiers, | qualifiers=dummy_qualifiers, | ||||
) == QualifiedSWHID( | ) == QualifiedSWHID( | ||||
object_type=ObjectType.DIRECTORY, | object_type=ObjectType.DIRECTORY, | ||||
object_id=object_id, | object_id=object_id, | ||||
qualifiers=dummy_qualifiers, | qualifiers=dummy_qualifiers, | ||||
) | ) | ||||
def test_parse_serialize_core_swhid(): | |||||
for swhid, _type, _version, _hash in [ | |||||
( | |||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
ObjectType.CONTENT, | |||||
1, | |||||
_x("94a9ed024d3859793618152ea559a168bbcbb5e2"), | |||||
), | |||||
( | |||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | |||||
ObjectType.DIRECTORY, | |||||
1, | |||||
_x("d198bc9d7a6bcf6db04f476d29314f157507d505"), | |||||
), | |||||
( | |||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | |||||
ObjectType.REVISION, | |||||
1, | |||||
_x("309cf2674ee7a0749978cf8265ab91a60aea0f7d"), | |||||
), | |||||
( | |||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | |||||
ObjectType.RELEASE, | |||||
1, | |||||
_x("22ece559cc7cc2364edc5e5593d63ae8bd229f9f"), | |||||
), | |||||
( | |||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | |||||
ObjectType.SNAPSHOT, | |||||
1, | |||||
_x("c7c108084bc0bf3d81436bf980b46e98bd338453"), | |||||
), | |||||
]: | |||||
expected_result = CoreSWHID( | |||||
namespace="swh", | |||||
scheme_version=_version, | |||||
object_type=_type, | |||||
object_id=_hash, | |||||
) | |||||
actual_result = CoreSWHID.from_string(swhid) | |||||
assert actual_result == expected_result | |||||
assert str(expected_result) == str(actual_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;visit=swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d", # noqa | |||||
"swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d", | |||||
"swh:1:snp:foo", | |||||
"swh:1: dir: 0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | |||||
], | |||||
) | |||||
def test_parse_core_swhid_parsing_error(invalid_swhid): | |||||
with pytest.raises(ValidationError): | |||||
CoreSWHID.from_string(invalid_swhid) | |||||
@pytest.mark.filterwarnings("ignore:.*SWHID.*:DeprecationWarning") | |||||
@pytest.mark.parametrize( | |||||
"ns,version,type,id", | |||||
[ | |||||
("foo", 1, ObjectType.CONTENT, "abc8bc9d7a6bcf6db04f476d29314f157507d505"), | |||||
("swh", 2, ObjectType.CONTENT, "def8bc9d7a6bcf6db04f476d29314f157507d505"), | |||||
("swh", 1, ObjectType.DIRECTORY, "aaaa"), | |||||
], | |||||
) | |||||
def test_CoreSWHID_validation_error(ns, version, type, id): | |||||
with pytest.raises(ValidationError): | |||||
CoreSWHID( | |||||
namespace=ns, scheme_version=version, object_type=type, object_id=_x(id), | |||||
) | |||||
def test_CoreSWHID_hash(): | |||||
object_id = _x("94a9ed024d3859793618152ea559a168bbcbb5e2") | |||||
assert hash( | |||||
CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id) | |||||
) == hash(CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id)) | |||||
assert hash( | |||||
CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id,) | |||||
) == hash(CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id,)) | |||||
# Different order of the dictionary, so the underlying order of the tuple in | |||||
# ImmutableDict is different. | |||||
assert hash( | |||||
CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id,) | |||||
) == hash(CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id,)) | |||||
def test_CoreSWHID_eq(): | |||||
object_id = _x("94a9ed024d3859793618152ea559a168bbcbb5e2") | |||||
assert CoreSWHID( | |||||
object_type=ObjectType.DIRECTORY, object_id=object_id | |||||
) == CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id) | |||||
assert CoreSWHID( | |||||
object_type=ObjectType.DIRECTORY, object_id=object_id, | |||||
) == CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id,) | |||||
assert CoreSWHID( | |||||
object_type=ObjectType.DIRECTORY, object_id=object_id, | |||||
) == CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=object_id,) |