swhid = 'foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505'
expected_error = "Wrong format: only supported namespace is 'swh'"
@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)
.tox/py3/lib/python3.7/site-packages/swh/model/tests/test_identifiers.py:1078:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
swhid = 'foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505'
def parse_swhid(swhid: str) -> SWHID:
"""Parse :ref:`persistent-identifiers`.
Args:
swhid (str): A persistent identifier
Raises:
swh.model.exceptions.ValidationError: in case of:
* missing mandatory values (4)
* invalid namespace supplied
* invalid version supplied
* invalid type supplied
* missing hash
* invalid hash identifier supplied
Returns:
a named tuple holding the parsing result
"""
# <swhid>;<contextual-information>
swhid_parts = swhid.split(SWHID_CTXT_SEP)
swhid_data = swhid_parts.pop(0).split(":")
if len(swhid_data) != 4:
raise ValidationError(
"""Wrong format: The supported format should be:
<identifier_core> ::= "swh" ":" <scheme_version> ":" <object_type> ":" <object_id> ;
<scheme_version> ::= "1" ;
<object_type> ::=
"snp" (* snapshot *)
| "rel" (* release *)
| "rev" (* revision *)
| "dir" (* directory *)
| "cnt" (* content *)
;
<object_id> ::= 40 * <hex_digit> ; (* intrinsic object id, as hex-encoded SHA1 *)
<dec_digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
<hex_digit> ::= <dec_digit> | "a" | "b" | "c" | "d" | "e" | "f" ;
"""
)
# Checking for parsing errors
_ns, _version, _type, _id = swhid_data
for otype, data in _object_type_map.items():
if _type == data["short_name"]:
_type = otype
break
if not _id:
raise ValidationError(
"""Wrong format: <object_id> should be provided:
<object_id> ::= 40 * <hex_digit> ; (* intrinsic object id, as hex-encoded SHA1 *)
<dec_digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
<hex_digit> ::= <dec_digit> | "a" | "b" | "c" | "d" | "e" | "f" ;
"""
)
_metadata = {}
for part in swhid_parts:
try:
key, val = part.split("=")
_metadata[key] = val
except Exception:
msg = """Wrong format: contextual data should be of form:
<context_qualifier> ::=
<origin_ctxt>
| <visit_ctxt>
| <anchor_ctxt>
| <path_ctxt>
;
<origin_ctxt> ::= "origin" "=" <url_escaped> ;
<visit_ctxt> ::= "visit" "=" <identifier_core> ;
<anchor_ctxt> ::= "anchor" "=" <identifier_core> ;
<path_ctxt> ::= "path" "=" <path_absolute_escaped> ;
"""
raise ValidationError(msg)
return SWHID(
_ns,
int(_version),
_type,
_id,
> _metadata, # type: ignore # mypy can't properly unify types
)
.tox/py3/lib/python3.7/site-packages/swh/model/identifiers.py:881:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = SWHID(namespace='foo', scheme_version=1, object_type='content', object_id='abc8bc9d7a6bcf6db04f476d29314f157507d505', metadata=ImmutableDict({}))
namespace = 'foo', scheme_version = 1, object_type = 'content'
object_id = 'abc8bc9d7a6bcf6db04f476d29314f157507d505', metadata = {}
def __init__(self, namespace=attr_dict['namespace'].default, scheme_version=attr_dict['scheme_version'].default, object_type=attr_dict['object_type'].default, object_id=attr_dict['object_id'].default, metadata=attr_dict['metadata'].default):
_setattr = _cached_setattr.__get__(self, self.__class__)
_inst_dict = self.__dict__
_inst_dict['namespace'] = namespace
_inst_dict['scheme_version'] = scheme_version
_inst_dict['object_type'] = object_type
_inst_dict['object_id'] = __attr_converter_object_id(object_id)
_inst_dict['metadata'] = __attr_converter_metadata(metadata)
if _config._run_validators is True:
> __attr_validator_namespace(self, __attr_namespace, self.namespace)
<attrs generated init swh.model.identifiers.SWHID>:10:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = SWHID(namespace='foo', scheme_version=1, object_type='content', object_id='abc8bc9d7a6bcf6db04f476d29314f157507d505', metadata=ImmutableDict({}))
attribute = Attribute(name='namespace', default='swh', validator=<function SWHID.check_namespace at 0x7fb0a89219d8>, repr=True, eq...t=True, metadata=mappingproxy({}), type=<class 'str'>, converter=None, kw_only=False, inherited=False, on_setattr=None)
value = 'foo'
@namespace.validator
def check_namespace(self, attribute, value):
if value != SWHID_NAMESPACE:
raise ValidationError(
> f"Wrong format: Supported namespace is '{SWHID_NAMESPACE}'"
)
E swh.model.exceptions.ValidationError: ["Wrong format: Supported namespace is 'swh'"]
.tox/py3/lib/python3.7/site-packages/swh/model/identifiers.py:731: ValidationError
During handling of the above exception, another exception occurred:
swhid = 'foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505'
expected_error = "Wrong format: only supported namespace is 'swh'"
@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)
E AssertionError: Regex pattern "Wrong format: only supported namespace is 'swh'" does not match '["Wrong format: Supported namespace is \'swh\'"]'.
.tox/py3/lib/python3.7/site-packages/swh/model/tests/test_identifiers.py:1078: AssertionError
TEST RESULT
TEST RESULT
- Run At
- Nov 10 2020, 6:22 PM