Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show All 11 Lines | |||||
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, | CONTENT, | ||||
DIRECTORY, | DIRECTORY, | ||||
RELEASE, | RELEASE, | ||||
REVISION, | REVISION, | ||||
SNAPSHOT, | SNAPSHOT, | ||||
PersistentId, | SWHID, | ||||
normalize_timestamp, | normalize_timestamp, | ||||
) | ) | ||||
class UtilityFunctionsIdentifier(unittest.TestCase): | class UtilityFunctionsIdentifier(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.str_id = "c2e41aae41ac17bd4a650770d6ee77f62e52235b" | self.str_id = "c2e41aae41ac17bd4a650770d6ee77f62e52235b" | ||||
self.bytes_id = binascii.unhexlify(self.str_id) | self.bytes_id = binascii.unhexlify(self.str_id) | ||||
▲ Show 20 Lines • Show All 705 Lines • ▼ Show 20 Lines | def test_unresolved_force(self): | ||||
) | ) | ||||
def test_all_types(self): | def test_all_types(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.snapshot_identifier(self.all_types), | identifiers.snapshot_identifier(self.all_types), | ||||
identifiers.identifier_to_str(self.all_types["id"]), | identifiers.identifier_to_str(self.all_types["id"]), | ||||
) | ) | ||||
def test_persistent_identifier(self): | def test_swhid(self): | ||||
_snapshot_id = _x("c7c108084bc0bf3d81436bf980b46e98bd338453") | _snapshot_id = _x("c7c108084bc0bf3d81436bf980b46e98bd338453") | ||||
_release_id = "22ece559cc7cc2364edc5e5593d63ae8bd229f9f" | _release_id = "22ece559cc7cc2364edc5e5593d63ae8bd229f9f" | ||||
_revision_id = "309cf2674ee7a0749978cf8265ab91a60aea0f7d" | _revision_id = "309cf2674ee7a0749978cf8265ab91a60aea0f7d" | ||||
_directory_id = "d198bc9d7a6bcf6db04f476d29314f157507d505" | _directory_id = "d198bc9d7a6bcf6db04f476d29314f157507d505" | ||||
_content_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | _content_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
_snapshot = {"id": _snapshot_id} | _snapshot = {"id": _snapshot_id} | ||||
_release = {"id": _release_id} | _release = {"id": _release_id} | ||||
_revision = {"id": _revision_id} | _revision = {"id": _revision_id} | ||||
_directory = {"id": _directory_id} | _directory = {"id": _directory_id} | ||||
_content = {"sha1_git": _content_id} | _content = {"sha1_git": _content_id} | ||||
for full_type, _hash, expected_persistent_id, version, _meta in [ | for full_type, _hash, expected_swhid, version, _meta in [ | ||||
( | ( | ||||
SNAPSHOT, | SNAPSHOT, | ||||
_snapshot_id, | _snapshot_id, | ||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | "swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | def test_swhid(self): | ||||
CONTENT, | CONTENT, | ||||
_content, | _content, | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=1", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=1", | ||||
1, | 1, | ||||
{"origin": "1"}, | {"origin": "1"}, | ||||
), | ), | ||||
]: | ]: | ||||
if version: | if version: | ||||
actual_value = identifiers.persistent_identifier( | actual_value = identifiers.swhid( | ||||
full_type, _hash, version, metadata=_meta | full_type, _hash, version, metadata=_meta | ||||
) | ) | ||||
else: | else: | ||||
actual_value = identifiers.persistent_identifier( | actual_value = identifiers.swhid(full_type, _hash, metadata=_meta) | ||||
full_type, _hash, metadata=_meta | |||||
) | |||||
self.assertEqual(actual_value, expected_persistent_id) | self.assertEqual(actual_value, expected_swhid) | ||||
def test_persistent_identifier_wrong_input(self): | def test_swhid_wrong_input(self): | ||||
_snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | _snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | ||||
_snapshot = {"id": _snapshot_id} | _snapshot = {"id": _snapshot_id} | ||||
for _type, _hash in [ | for _type, _hash in [ | ||||
(SNAPSHOT, _snapshot_id), | (SNAPSHOT, _snapshot_id), | ||||
(SNAPSHOT, _snapshot), | (SNAPSHOT, _snapshot), | ||||
("foo", ""), | ("foo", ""), | ||||
]: | ]: | ||||
with self.assertRaises(ValidationError): | with self.assertRaises(ValidationError): | ||||
identifiers.persistent_identifier(_type, _hash) | identifiers.swhid(_type, _hash) | ||||
def test_parse_persistent_identifier(self): | def test_parse_swhid(self): | ||||
for pid, _type, _version, _hash in [ | for swhid, _type, _version, _hash in [ | ||||
( | ( | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
CONTENT, | CONTENT, | ||||
1, | 1, | ||||
"94a9ed024d3859793618152ea559a168bbcbb5e2", | "94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
), | ), | ||||
( | ( | ||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | "swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | ||||
Show All 15 Lines | def test_parse_swhid(self): | ||||
), | ), | ||||
( | ( | ||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | "swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
SNAPSHOT, | SNAPSHOT, | ||||
1, | 1, | ||||
"c7c108084bc0bf3d81436bf980b46e98bd338453", | "c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
), | ), | ||||
]: | ]: | ||||
expected_result = PersistentId( | expected_result = SWHID( | ||||
namespace="swh", | namespace="swh", | ||||
scheme_version=_version, | scheme_version=_version, | ||||
object_type=_type, | object_type=_type, | ||||
object_id=_hash, | object_id=_hash, | ||||
metadata={}, | metadata={}, | ||||
) | ) | ||||
actual_result = identifiers.parse_persistent_identifier(pid) | actual_result = identifiers.parse_swhid(swhid) | ||||
self.assertEqual(actual_result, expected_result) | self.assertEqual(actual_result, expected_result) | ||||
for pid, _type, _version, _hash, _metadata in [ | for swhid, _type, _version, _hash, _metadata in [ | ||||
( | ( | ||||
"swh:1:cnt:9c95815d9e9d91b8dae8e05d8bbc696fe19f796b;lines=1-18;origin=https://github.com/python/cpython", # noqa | "swh:1:cnt:9c95815d9e9d91b8dae8e05d8bbc696fe19f796b;lines=1-18;origin=https://github.com/python/cpython", # noqa | ||||
CONTENT, | CONTENT, | ||||
1, | 1, | ||||
"9c95815d9e9d91b8dae8e05d8bbc696fe19f796b", | "9c95815d9e9d91b8dae8e05d8bbc696fe19f796b", | ||||
{"lines": "1-18", "origin": "https://github.com/python/cpython"}, | {"lines": "1-18", "origin": "https://github.com/python/cpython"}, | ||||
), | ), | ||||
( | ( | ||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=deb://Debian/packages/linuxdoc-tools", # noqa | "swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=deb://Debian/packages/linuxdoc-tools", # noqa | ||||
DIRECTORY, | DIRECTORY, | ||||
1, | 1, | ||||
"0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | "0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | ||||
{"origin": "deb://Debian/packages/linuxdoc-tools"}, | {"origin": "deb://Debian/packages/linuxdoc-tools"}, | ||||
), | ), | ||||
]: | ]: | ||||
expected_result = PersistentId( | expected_result = SWHID( | ||||
namespace="swh", | namespace="swh", | ||||
scheme_version=_version, | scheme_version=_version, | ||||
object_type=_type, | object_type=_type, | ||||
object_id=_hash, | object_id=_hash, | ||||
metadata=_metadata, | metadata=_metadata, | ||||
) | ) | ||||
actual_result = identifiers.parse_persistent_identifier(pid) | actual_result = identifiers.parse_swhid(swhid) | ||||
self.assertEqual(actual_result, expected_result) | self.assertEqual(actual_result, expected_result) | ||||
def test_parse_persistent_identifier_parsing_error(self): | def test_parse_swhid_parsing_error(self): | ||||
for pid in [ | for swhid in [ | ||||
("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"), | ||||
("swh:1:foo:fed8bc9d7a6bcf6db04f476d29314f157507d505"), | ("swh:1:foo:fed8bc9d7a6bcf6db04f476d29314f157507d505"), | ||||
("swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;" "malformed"), | ("swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;" "malformed"), | ||||
("swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d"), | ("swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d"), | ||||
("swh:1:snp:foo"), | ("swh:1:snp:foo"), | ||||
]: | ]: | ||||
with self.assertRaises(ValidationError): | with self.assertRaises(ValidationError): | ||||
identifiers.parse_persistent_identifier(pid) | identifiers.parse_swhid(swhid) | ||||
def test_persistentid_class_validation_error(self): | def test_persistentid_class_validation_error(self): | ||||
for _ns, _version, _type, _id in [ | for _ns, _version, _type, _id in [ | ||||
("foo", 1, CONTENT, "abc8bc9d7a6bcf6db04f476d29314f157507d505"), | ("foo", 1, CONTENT, "abc8bc9d7a6bcf6db04f476d29314f157507d505"), | ||||
("swh", 2, DIRECTORY, "def8bc9d7a6bcf6db04f476d29314f157507d505"), | ("swh", 2, DIRECTORY, "def8bc9d7a6bcf6db04f476d29314f157507d505"), | ||||
("swh", 1, "foo", "fed8bc9d7a6bcf6db04f476d29314f157507d505"), | ("swh", 1, "foo", "fed8bc9d7a6bcf6db04f476d29314f157507d505"), | ||||
("swh", 1, SNAPSHOT, "gh6959356d30f1a4e9b7f6bca59b9a336464c03d"), | ("swh", 1, SNAPSHOT, "gh6959356d30f1a4e9b7f6bca59b9a336464c03d"), | ||||
]: | ]: | ||||
with self.assertRaises(ValidationError): | with self.assertRaises(ValidationError): | ||||
PersistentId( | SWHID( | ||||
namespace=_ns, | namespace=_ns, | ||||
scheme_version=_version, | scheme_version=_version, | ||||
object_type=_type, | object_type=_type, | ||||
object_id=_id, | object_id=_id, | ||||
) | ) | ||||
class OriginIdentifier(unittest.TestCase): | class OriginIdentifier(unittest.TestCase): | ||||
▲ Show 20 Lines • Show All 115 Lines • Show Last 20 Lines |