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 ( | from swh.web.tests.strategies import release, revision, snapshot | ||||
origin, | |||||
origin_with_multiple_visits, | |||||
release, | |||||
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 20 Lines • Show All 222 Lines • ▼ Show 20 Lines | def test_get_swhids_info_revision_context(archive_data, revision): | ||||
if dir_entry["type"] == "file": | if dir_entry["type"] == "file": | ||||
swhid_cnt_parsed = get_swhid(swhids[2]["swhid_with_context"]) | swhid_cnt_parsed = get_swhid(swhids[2]["swhid_with_context"]) | ||||
assert swhid_cnt_parsed.qualifiers() == { | assert swhid_cnt_parsed.qualifiers() == { | ||||
"anchor": anchor, | "anchor": anchor, | ||||
"path": f'/{dir_entry["name"]}', | "path": f'/{dir_entry["name"]}', | ||||
} | } | ||||
@given(origin_with_multiple_visits()) | def test_get_swhids_info_origin_snapshot_context( | ||||
def test_get_swhids_info_origin_snapshot_context(archive_data, origin): | archive_data, origin_with_multiple_visits | ||||
): | |||||
""" | """ | ||||
Test SWHIDs with contextual info computation under a variety of origin / snapshot | Test SWHIDs with contextual info computation under a variety of origin / snapshot | ||||
browsing contexts. | browsing contexts. | ||||
""" | """ | ||||
origin_url = origin_with_multiple_visits["url"] | |||||
visits = archive_data.origin_visit_get(origin["url"]) | visits = archive_data.origin_visit_get(origin_url) | ||||
for visit in visits: | for visit in visits: | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
snapshot_id = snapshot["id"] | snapshot_id = snapshot["id"] | ||||
branches = { | branches = { | ||||
k: v["target"] | k: v["target"] | ||||
for k, v in snapshot["branches"].items() | for k, v in snapshot["branches"].items() | ||||
if v["target_type"] == "revision" | if v["target_type"] == "revision" | ||||
Show All 33 Lines | for visit in visits: | ||||
{"snapshot_id": snapshot_id, "release_name": release_name}, | {"snapshot_id": snapshot_id, "release_name": release_name}, | ||||
{"anchor_type": ObjectType.RELEASE, "anchor_id": releases[release]}, | {"anchor_type": ObjectType.RELEASE, "anchor_id": releases[release]}, | ||||
), | ), | ||||
( | ( | ||||
{"snapshot_id": snapshot_id, "revision_id": revision_id}, | {"snapshot_id": snapshot_id, "revision_id": revision_id}, | ||||
{"anchor_type": ObjectType.REVISION, "anchor_id": revision_id}, | {"anchor_type": ObjectType.REVISION, "anchor_id": revision_id}, | ||||
), | ), | ||||
( | ( | ||||
{"origin_url": origin["url"], "snapshot_id": snapshot_id}, | {"origin_url": origin_url, "snapshot_id": snapshot_id}, | ||||
{"anchor_type": ObjectType.REVISION, "anchor_id": head_rev_id}, | {"anchor_type": ObjectType.REVISION, "anchor_id": head_rev_id}, | ||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"origin_url": origin["url"], | "origin_url": origin_url, | ||||
"snapshot_id": snapshot_id, | "snapshot_id": snapshot_id, | ||||
"branch_name": branch_name, | "branch_name": branch_name, | ||||
}, | }, | ||||
{ | { | ||||
"anchor_type": ObjectType.REVISION, | "anchor_type": ObjectType.REVISION, | ||||
"anchor_id": branches[branch_name], | "anchor_id": branches[branch_name], | ||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"origin_url": origin["url"], | "origin_url": origin_url, | ||||
"snapshot_id": snapshot_id, | "snapshot_id": snapshot_id, | ||||
"release_name": release_name, | "release_name": release_name, | ||||
}, | }, | ||||
{"anchor_type": ObjectType.RELEASE, "anchor_id": releases[release]}, | {"anchor_type": ObjectType.RELEASE, "anchor_id": releases[release]}, | ||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"origin_url": origin["url"], | "origin_url": origin_url, | ||||
"snapshot_id": snapshot_id, | "snapshot_id": snapshot_id, | ||||
"revision_id": revision_id, | "revision_id": revision_id, | ||||
}, | }, | ||||
{"anchor_type": ObjectType.REVISION, "anchor_id": revision_id}, | {"anchor_type": ObjectType.REVISION, "anchor_id": revision_id}, | ||||
), | ), | ||||
): | ): | ||||
snapshot_context = get_snapshot_context(**snp_ctx_params) | snapshot_context = get_snapshot_context(**snp_ctx_params) | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | for visit in visits: | ||||
"anchor": anchor, | "anchor": anchor, | ||||
} | } | ||||
expected_rev_context = {"visit": snapshot_swhid} | expected_rev_context = {"visit": snapshot_swhid} | ||||
expected_snp_context = {} | expected_snp_context = {} | ||||
if "origin_url" in snp_ctx_params: | if "origin_url" in snp_ctx_params: | ||||
expected_cnt_context["origin"] = origin["url"] | expected_cnt_context["origin"] = origin_url | ||||
expected_dir_context["origin"] = origin["url"] | expected_dir_context["origin"] = origin_url | ||||
expected_rev_context["origin"] = origin["url"] | expected_rev_context["origin"] = origin_url | ||||
expected_snp_context["origin"] = origin["url"] | expected_snp_context["origin"] = origin_url | ||||
assert swhid_cnt_parsed.qualifiers() == expected_cnt_context | assert swhid_cnt_parsed.qualifiers() == expected_cnt_context | ||||
assert swhid_dir_parsed.qualifiers() == expected_dir_context | assert swhid_dir_parsed.qualifiers() == expected_dir_context | ||||
assert swhid_rev_parsed.qualifiers() == expected_rev_context | assert swhid_rev_parsed.qualifiers() == expected_rev_context | ||||
assert swhid_snp_parsed.qualifiers() == expected_snp_context | assert swhid_snp_parsed.qualifiers() == expected_snp_context | ||||
if "release_name" in snp_ctx_params: | if "release_name" in snp_ctx_params: | ||||
assert swhid_rel_parsed.qualifiers() == expected_rev_context | assert swhid_rel_parsed.qualifiers() == expected_rev_context | ||||
@given(origin()) | |||||
def test_get_swhids_info_characters_and_url_escaping(archive_data, directory, origin): | def test_get_swhids_info_characters_and_url_escaping(archive_data, directory, origin): | ||||
snapshot_context = get_snapshot_context(origin_url=origin["url"]) | snapshot_context = get_snapshot_context(origin_url=origin["url"]) | ||||
snapshot_context["origin_info"]["url"] = "http://example.org/?project=abc;def%" | snapshot_context["origin_info"]["url"] = "http://example.org/?project=abc;def%" | ||||
path = "/foo;/bar%" | path = "/foo;/bar%" | ||||
swhid_info = get_swhids_info( | swhid_info = get_swhids_info( | ||||
[SWHObjectInfo(object_type=ObjectType.DIRECTORY, object_id=directory)], | [SWHObjectInfo(object_type=ObjectType.DIRECTORY, object_id=directory)], | ||||
snapshot_context=snapshot_context, | snapshot_context=snapshot_context, | ||||
Show All 12 Lines | def test_get_swhids_info_characters_and_url_escaping(archive_data, directory, origin): | ||||
) | ) | ||||
assert ( | assert ( | ||||
parsed_url_swhid.qualifiers()["origin"] | parsed_url_swhid.qualifiers()["origin"] | ||||
== "http://example.org/%3Fproject%253Dabc%253Bdef%2525" | == "http://example.org/%3Fproject%253Dabc%253Bdef%2525" | ||||
) | ) | ||||
assert parsed_url_swhid.qualifiers()["path"] == "/foo%253B/bar%2525" | assert parsed_url_swhid.qualifiers()["path"] == "/foo%253B/bar%2525" | ||||
@given(origin_with_multiple_visits()) | def test_resolve_swhids_snapshot_context( | ||||
def test_resolve_swhids_snapshot_context(client, archive_data, origin): | client, archive_data, origin_with_multiple_visits | ||||
visits = archive_data.origin_visit_get(origin["url"]) | ): | ||||
origin_url = origin_with_multiple_visits["url"] | |||||
visits = archive_data.origin_visit_get(origin_url) | |||||
visit = random.choice(visits) | visit = random.choice(visits) | ||||
snapshot = archive_data.snapshot_get(visit["snapshot"]) | snapshot = archive_data.snapshot_get(visit["snapshot"]) | ||||
head_rev_id = archive_data.snapshot_get_head(snapshot) | head_rev_id = archive_data.snapshot_get_head(snapshot) | ||||
branch_info = None | branch_info = None | ||||
release_info = None | release_info = None | ||||
for branch_name in sorted(snapshot["branches"]): | for branch_name in sorted(snapshot["branches"]): | ||||
target_type = snapshot["branches"][branch_name]["target_type"] | target_type = snapshot["branches"][branch_name]["target_type"] | ||||
Show All 21 Lines | ): | ||||
for snp_ctx_params in ( | for snp_ctx_params in ( | ||||
{}, | {}, | ||||
{"branch_name": branch_info["name"]}, | {"branch_name": branch_info["name"]}, | ||||
{"release_name": release_info["name"]}, | {"release_name": release_info["name"]}, | ||||
{"revision_id": random_rev_id}, | {"revision_id": random_rev_id}, | ||||
): | ): | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
snapshot["id"], origin["url"], **snp_ctx_params | snapshot["id"], origin_url, **snp_ctx_params | ||||
) | ) | ||||
_check_resolved_swhid_browse_url( | _check_resolved_swhid_browse_url( | ||||
ObjectType.SNAPSHOT, snapshot["id"], snapshot_context | ObjectType.SNAPSHOT, snapshot["id"], snapshot_context | ||||
) | ) | ||||
rev = head_rev_id | rev = head_rev_id | ||||
if "branch_name" in snp_ctx_params: | if "branch_name" in snp_ctx_params: | ||||
▲ Show 20 Lines • Show All 249 Lines • Show Last 20 Lines |