Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_identifiers.py
Show All 19 Lines | from swh.web.common.identifiers import ( | ||||
get_swhids_info, | get_swhids_info, | ||||
group_swhids, | group_swhids, | ||||
parse_object_type, | parse_object_type, | ||||
resolve_swhid, | resolve_swhid, | ||||
) | ) | ||||
from swh.web.common.typing import SWHObjectInfo | from swh.web.common.typing import SWHObjectInfo | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.data import random_sha1 | from swh.web.tests.data import random_sha1 | ||||
from swh.web.tests.strategies import release, revision, snapshot | from swh.web.tests.strategies import revision, snapshot | ||||
def test_gen_swhid(content): | def test_gen_swhid(content): | ||||
swh_object_type = ObjectType.CONTENT | swh_object_type = ObjectType.CONTENT | ||||
sha1_git = content["sha1_git"] | sha1_git = content["sha1_git"] | ||||
expected_swhid = "swh:1:cnt:" + sha1_git | expected_swhid = "swh:1:cnt:" + sha1_git | ||||
Show All 21 Lines | def test_parse_object_type(): | ||||
assert parse_object_type("release") == ObjectType.RELEASE | assert parse_object_type("release") == ObjectType.RELEASE | ||||
assert parse_object_type("snapshot") == ObjectType.SNAPSHOT | assert parse_object_type("snapshot") == ObjectType.SNAPSHOT | ||||
with pytest.raises(BadInputExc) as e: | with pytest.raises(BadInputExc) as e: | ||||
parse_object_type("foo") | parse_object_type("foo") | ||||
assert e.match("Invalid swh object type") | assert e.match("Invalid swh object type") | ||||
@given(release(), revision(), snapshot()) | @given(revision(), snapshot()) | ||||
def test_resolve_swhid_legacy(content, directory, release, revision, snapshot): | def test_resolve_swhid_legacy(content, directory, release, revision, snapshot): | ||||
for obj_type, obj_id in ( | for obj_type, obj_id in ( | ||||
(ObjectType.CONTENT, content["sha1_git"]), | (ObjectType.CONTENT, content["sha1_git"]), | ||||
(ObjectType.DIRECTORY, directory), | (ObjectType.DIRECTORY, directory), | ||||
(ObjectType.RELEASE, release), | (ObjectType.RELEASE, release), | ||||
(ObjectType.REVISION, revision), | (ObjectType.REVISION, revision), | ||||
(ObjectType.SNAPSHOT, snapshot), | (ObjectType.SNAPSHOT, snapshot), | ||||
): | ): | ||||
Show All 20 Lines | ): | ||||
assert isinstance(resolved_swhid["swhid_parsed"], QualifiedSWHID) | assert isinstance(resolved_swhid["swhid_parsed"], QualifiedSWHID) | ||||
assert str(resolved_swhid["swhid_parsed"]) == swhid | assert str(resolved_swhid["swhid_parsed"]) == swhid | ||||
assert resolved_swhid["browse_url"] == browse_url | assert resolved_swhid["browse_url"] == browse_url | ||||
with pytest.raises(BadInputExc, match="'ori' is not a valid ObjectType"): | with pytest.raises(BadInputExc, match="'ori' is not a valid ObjectType"): | ||||
resolve_swhid(f"swh:1:ori:{random_sha1()}") | resolve_swhid(f"swh:1:ori:{random_sha1()}") | ||||
@given(release(), revision(), snapshot()) | @given(revision(), snapshot()) | ||||
def test_get_swhid(content, directory, release, revision, snapshot): | def test_get_swhid(content, directory, release, revision, snapshot): | ||||
for obj_type, obj_id in ( | for obj_type, obj_id in ( | ||||
(ObjectType.CONTENT, content["sha1_git"]), | (ObjectType.CONTENT, content["sha1_git"]), | ||||
(ObjectType.DIRECTORY, directory), | (ObjectType.DIRECTORY, directory), | ||||
(ObjectType.RELEASE, release), | (ObjectType.RELEASE, release), | ||||
(ObjectType.REVISION, revision), | (ObjectType.REVISION, revision), | ||||
(ObjectType.SNAPSHOT, snapshot), | (ObjectType.SNAPSHOT, snapshot), | ||||
): | ): | ||||
swhid = gen_swhid(obj_type, obj_id) | swhid = gen_swhid(obj_type, obj_id) | ||||
for swhid_ in (swhid, swhid.upper()): | for swhid_ in (swhid, swhid.upper()): | ||||
swh_parsed_swhid = get_swhid(swhid_) | swh_parsed_swhid = get_swhid(swhid_) | ||||
assert isinstance(swh_parsed_swhid, QualifiedSWHID) | assert isinstance(swh_parsed_swhid, QualifiedSWHID) | ||||
assert str(swh_parsed_swhid) == swhid.lower() | assert str(swh_parsed_swhid) == swhid.lower() | ||||
with pytest.raises(BadInputExc, match="Error when parsing identifier"): | with pytest.raises(BadInputExc, match="Error when parsing identifier"): | ||||
get_swhid("foo") | get_swhid("foo") | ||||
@given(release(), revision(), snapshot()) | @given(revision(), snapshot()) | ||||
def test_group_swhids(content, directory, release, revision, snapshot): | def test_group_swhids(content, directory, release, revision, snapshot): | ||||
swhids = [] | swhids = [] | ||||
expected = {} | expected = {} | ||||
for obj_type, obj_id in ( | for obj_type, obj_id in ( | ||||
(ObjectType.CONTENT, content["sha1_git"]), | (ObjectType.CONTENT, content["sha1_git"]), | ||||
(ObjectType.DIRECTORY, directory), | (ObjectType.DIRECTORY, directory), | ||||
(ObjectType.RELEASE, release), | (ObjectType.RELEASE, release), | ||||
(ObjectType.REVISION, revision), | (ObjectType.REVISION, revision), | ||||
▲ Show 20 Lines • Show All 638 Lines • Show Last 20 Lines |