diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -927,37 +927,6 @@ }, ) - 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): def setUp(self): @@ -1077,6 +1046,78 @@ normalize_timestamp(dict_input) +@pytest.mark.parametrize( + "swhid,expected_error", + [ + ("swh:1:cnt", "Wrong format: There should be 4 mandatory values"), + ("swh:1:", "Wrong format: There should be 4 mandatory values"), + ("swh:", "Wrong format: There should be 4 mandatory values"), + ("swh:1:cnt:", "Wrong format: Identifier should be present"), + ( + "foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505", + "Wrong format: only supported namespace is 'swh'", + ), + ( + "swh:2:dir:def8bc9d7a6bcf6db04f476d29314f157507d505", + "Wrong format: only supported version is 1", + ), + ( + "swh:1:foo:fed8bc9d7a6bcf6db04f476d29314f157507d505", + "Wrong input: Supported types are", + ), + ( + "swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;malformed", + "Contextual data is badly formatted, form key=val expected", + ), + ("swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d", "Unexpected characters"), + ("swh:1:snp:foo", "Unexpected characters"), + ], +) +def test_parse_swhid_parsing_error(swhid, expected_error): + with pytest.raises(ValidationError, match=expected_error): + identifiers.parse_swhid(swhid) + + +@pytest.mark.parametrize( + "ns,version,type,id,expected_error", + [ + ( + "foo", + 1, + CONTENT, + "abc8bc9d7a6bcf6db04f476d29314f157507d505", + "Wrong format: only supported namespace is 'swh'", + ), + ( + "swh", + 2, + DIRECTORY, + "def8bc9d7a6bcf6db04f476d29314f157507d505", + "Wrong format: only supported version is 1", + ), + ( + "swh", + 1, + "foo", + "fed8bc9d7a6bcf6db04f476d29314f157507d505", + "Wrong input: Supported types are", + ), + ( + "swh", + 1, + SNAPSHOT, + "gh6959356d30f1a4e9b7f6bca59b9a336464c03d", + "Unexpected characters", + ), + ], +) +def test_SWHID_class_validation_error(ns, version, type, id, expected_error): + with pytest.raises(ValidationError, match=expected_error): + SWHID( + namespace=ns, scheme_version=version, object_type=type, object_id=id, + ) + + def test_swhid_hash(): object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2"