Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show First 20 Lines • Show All 921 Lines • ▼ Show 20 Lines | def test_parse_swhid(self): | ||||
"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, | ||||
}, | }, | ||||
) | ) | ||||
def test_parse_swhid_parsing_error(self): | |||||
for swhid in [ | |||||
("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"), | |||||
]: | |||||
with self.assertRaises(ValidationError): | |||||
identifiers.parse_swhid(swhid) | |||||
def test_persistentid_class_validation_error(self): | |||||
for _ns, _version, _type, _id in [ | |||||
("foo", 1, CONTENT, "abc8bc9d7a6bcf6db04f476d29314f157507d505"), | |||||
("swh", 2, DIRECTORY, "def8bc9d7a6bcf6db04f476d29314f157507d505"), | |||||
("swh", 1, "foo", "fed8bc9d7a6bcf6db04f476d29314f157507d505"), | |||||
("swh", 1, SNAPSHOT, "gh6959356d30f1a4e9b7f6bca59b9a336464c03d"), | |||||
]: | |||||
with self.assertRaises(ValidationError): | |||||
SWHID( | |||||
namespace=_ns, | |||||
scheme_version=_version, | |||||
object_type=_type, | |||||
object_id=_id, | |||||
) | |||||
class OriginIdentifier(unittest.TestCase): | class OriginIdentifier(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.origin = { | self.origin = { | ||||
"url": "https://github.com/torvalds/linux", | "url": "https://github.com/torvalds/linux", | ||||
} | } | ||||
def test_content_identifier(self): | def test_content_identifier(self): | ||||
▲ Show 20 Lines • Show All 103 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) | ||||
@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", | |||||
], | |||||
) | |||||
def test_parse_swhid_parsing_error(invalid_swhid): | |||||
with pytest.raises(ValidationError): | |||||
identifiers.parse_swhid(invalid_swhid) | |||||
@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, | |||||
) | |||||
def test_swhid_hash(): | def test_swhid_hash(): | ||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( | assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( | ||||
SWHID(object_type="directory", object_id=object_id) | SWHID(object_type="directory", object_id=object_id) | ||||
) | ) | ||||
assert hash( | assert hash( | ||||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |